blob: 3e1088a1e23c97847e7c01df191e3766ace06ac2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200729static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000731static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000732static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000733static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000734static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000736static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200737#ifdef FEAT_FLOAT
738static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
745static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200748static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200749static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100750static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static 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 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static 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 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200811static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static 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 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200840static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200841static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200843
844#ifdef EBCDIC
845static int compare_func_name __ARGS((const void *s1, const void *s2));
846static void sortFunctions __ARGS(());
847#endif
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
859 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100883 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200921 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100922# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923
924 /* global variables */
925 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000927 /* autoloaded script names */
928 ga_clear_strings(&ga_loaded);
929
Bram Moolenaarcca74132013-09-25 21:00:28 +0200930 /* Script-local variables. First clear all the variables and in a second
931 * loop free the scriptvar_T, because a variable in one script might hold
932 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200936 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001864 expr = skipwhite(argend);
1865 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1866 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001868 /*
1869 * ":let" without "=": list variables
1870 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 if (*arg == '[')
1872 EMSG(_(e_invarg));
1873 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_glob_vars(&first);
1880 list_buf_vars(&first);
1881 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001884#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_script_vars(&first);
1886 list_func_vars(&first);
1887 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 eap->nextcmd = check_nextcmd(arg);
1890 }
1891 else
1892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op[0] = '=';
1894 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1898 op[0] = *expr; /* +=, -= or .= */
1899 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 else
1902 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (eap->skip)
1908 {
1909 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 --emsg_skip;
1912 }
1913 else if (i != FAIL)
1914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 }
1920}
1921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922/*
1923 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1924 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * When "nextchars" is not NULL it points to a string with characters that
1926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1927 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 * Returns OK or FAIL;
1929 */
1930 static int
1931ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1932 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int copy; /* copy values from "tv", don't move */
1935 int semicolon; /* from skip_var_list() */
1936 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938{
1939 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 listitem_T *item;
1943 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944
1945 if (*arg != '[')
1946 {
1947 /*
1948 * ":let var = expr" or ":for var in list"
1949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 return OK;
1953 }
1954
1955 /*
1956 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1957 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 {
1960 EMSG(_(e_listreq));
1961 return FAIL;
1962 }
1963
1964 i = list_len(l);
1965 if (semicolon == 0 && var_count < i)
1966 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001967 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return FAIL;
1969 }
1970 if (var_count - semicolon > i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975
1976 item = l->lv_first;
1977 while (*arg != ']')
1978 {
1979 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 item = item->li_next;
1982 if (arg == NULL)
1983 return FAIL;
1984
1985 arg = skipwhite(arg);
1986 if (*arg == ';')
1987 {
1988 /* Put the rest of the list (may be empty) in the var after ';'.
1989 * Create a new list for this. */
1990 l = list_alloc();
1991 if (l == NULL)
1992 return FAIL;
1993 while (item != NULL)
1994 {
1995 list_append_tv(l, &item->li_tv);
1996 item = item->li_next;
1997 }
1998
1999 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 ltv.vval.v_list = l;
2002 l->lv_refcount = 1;
2003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2005 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 clear_tv(&ltv);
2007 if (arg == NULL)
2008 return FAIL;
2009 break;
2010 }
2011 else if (*arg != ',' && *arg != ']')
2012 {
2013 EMSG2(_(e_intern2), "ex_let_vars()");
2014 return FAIL;
2015 }
2016 }
2017
2018 return OK;
2019}
2020
2021/*
2022 * Skip over assignable variable "var" or list of variables "[var, var]".
2023 * Used for ":let varvar = expr" and ":for varvar in expr".
2024 * For "[var, var]" increment "*var_count" for each variable.
2025 * for "[var, var; var]" set "semicolon".
2026 * Return NULL for an error.
2027 */
2028 static char_u *
2029skip_var_list(arg, var_count, semicolon)
2030 char_u *arg;
2031 int *var_count;
2032 int *semicolon;
2033{
2034 char_u *p, *s;
2035
2036 if (*arg == '[')
2037 {
2038 /* "[var, var]": find the matching ']'. */
2039 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002040 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2043 s = skip_var_one(p);
2044 if (s == p)
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 ++*var_count;
2050
2051 p = skipwhite(s);
2052 if (*p == ']')
2053 break;
2054 else if (*p == ';')
2055 {
2056 if (*semicolon == 1)
2057 {
2058 EMSG(_("Double ; in list of variables"));
2059 return NULL;
2060 }
2061 *semicolon = 1;
2062 }
2063 else if (*p != ',')
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 }
2069 return p + 1;
2070 }
2071 else
2072 return skip_var_one(arg);
2073}
2074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002077 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 static char_u *
2080skip_var_one(arg)
2081 char_u *arg;
2082{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 if (*arg == '@' && arg[1] != NUL)
2084 return arg + 2;
2085 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087}
2088
Bram Moolenaara7043832005-01-21 11:56:39 +00002089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 * List variables for hashtab "ht" with prefix "prefix".
2091 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashitem_T *hi;
2101 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 int todo;
2103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2106 {
2107 if (!HASHITEM_EMPTY(hi))
2108 {
2109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 di = HI2DI(hi);
2111 if (empty || di->di_tv.v_type != VAR_STRING
2112 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 }
2115 }
2116}
2117
2118/*
2119 * List global variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_glob_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List buffer variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_buf_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 char_u numbuf[NUMBUFLEN];
2136
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139
2140 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2142 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
2145/*
2146 * List window variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_win_vars(first)
2150 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154}
2155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156#ifdef FEAT_WINDOWS
2157/*
2158 * List tab page variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_tab_vars(first)
2162 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002164 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166}
2167#endif
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
2170 * List Vim variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_vim_vars(first)
2174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177}
2178
2179/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180 * List script-local variables, if there is a script.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_script_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2188 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
2192 * List function variables, if there is a function.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_func_vars(first)
2196 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197{
2198 if (current_funccal != NULL)
2199 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201}
2202
2203/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * List variables in "arg".
2205 */
2206 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 exarg_T *eap;
2209 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u *name_start;
2216 char_u *arg_subsc;
2217 char_u *tofree;
2218 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 while (!ends_excmd(*arg) && !got_int)
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2226 {
2227 emsg_severe = TRUE;
2228 EMSG(_(e_trailing));
2229 break;
2230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* get_name_len() takes care of expanding curly braces */
2235 name_start = name = arg;
2236 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2237 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* This is mainly to keep test 49 working: when expanding
2240 * curly braces fails overrule the exception error message. */
2241 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 emsg_severe = TRUE;
2244 EMSG2(_(e_invarg2), arg);
2245 break;
2246 }
2247 error = TRUE;
2248 }
2249 else
2250 {
2251 if (tofree != NULL)
2252 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002253 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 else
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* handle d.key, l[idx], f(expr) */
2258 arg_subsc = arg;
2259 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 'g': list_glob_vars(first); break;
2268 case 'b': list_buf_vars(first); break;
2269 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'v': list_vim_vars(first); break;
2274 case 's': list_script_vars(first); break;
2275 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 default:
2277 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
2280 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 {
2282 char_u numbuf[NUMBUFLEN];
2283 char_u *tf;
2284 int c;
2285 char_u *s;
2286
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002287 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 c = *arg;
2289 *arg = NUL;
2290 list_one_var_a((char_u *)"",
2291 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 tv.v_type,
2293 s == NULL ? (char_u *)"" : s,
2294 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 *arg = c;
2296 vim_free(tf);
2297 }
2298 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305
2306 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 return arg;
2310}
2311
2312/*
2313 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2314 * Returns a pointer to the char just after the var name.
2315 * Returns NULL if there is an error.
2316 */
2317 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324{
2325 int c1;
2326 char_u *name;
2327 char_u *p;
2328 char_u *arg_end = NULL;
2329 int len;
2330 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332
2333 /*
2334 * ":let $VAR = expr": Set environment variable.
2335 */
2336 if (*arg == '$')
2337 {
2338 /* Find the end of the name. */
2339 ++arg;
2340 name = arg;
2341 len = get_env_len(&arg);
2342 if (len == 0)
2343 EMSG2(_(e_invarg2), name - 1);
2344 else
2345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 if (op != NULL && (*op == '+' || *op == '-'))
2347 EMSG2(_(e_letwrong), op);
2348 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002351 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 {
2353 c1 = name[len];
2354 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 p = get_tv_string_chk(tv);
2356 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 {
2358 int mustfree = FALSE;
2359 char_u *s = vim_getenv(name, &mustfree);
2360
2361 if (s != NULL)
2362 {
2363 p = tofree = concat_str(s, p);
2364 if (mustfree)
2365 vim_free(s);
2366 }
2367 }
2368 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (STRICMP(name, "HOME") == 0)
2372 init_homedir();
2373 else if (didset_vim && STRICMP(name, "VIM") == 0)
2374 didset_vim = FALSE;
2375 else if (didset_vimruntime
2376 && STRICMP(name, "VIMRUNTIME") == 0)
2377 didset_vimruntime = FALSE;
2378 arg_end = arg;
2379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
2384 }
2385
2386 /*
2387 * ":let &option = expr": Set option value.
2388 * ":let &l:option = expr": Set local option value.
2389 * ":let &g:option = expr": Set global option value.
2390 */
2391 else if (*arg == '&')
2392 {
2393 /* Find the end of the name. */
2394 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002395 if (p == NULL || (endchars != NULL
2396 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 long n;
2401 int opt_type;
2402 long numval;
2403 char_u *stringval = NULL;
2404 char_u *s;
2405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 c1 = *p;
2407 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408
2409 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 s = get_tv_string_chk(tv); /* != NULL if number or string */
2411 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
2413 opt_type = get_option_value(arg, &numval,
2414 &stringval, opt_flags);
2415 if ((opt_type == 1 && *op == '.')
2416 || (opt_type == 0 && *op != '.'))
2417 EMSG2(_(e_letwrong), op);
2418 else
2419 {
2420 if (opt_type == 1) /* number */
2421 {
2422 if (*op == '+')
2423 n = numval + n;
2424 else
2425 n = numval - n;
2426 }
2427 else if (opt_type == 0 && stringval != NULL) /* string */
2428 {
2429 s = concat_str(stringval, s);
2430 vim_free(stringval);
2431 stringval = s;
2432 }
2433 }
2434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (s != NULL)
2436 {
2437 set_option_value(arg, n, s, opt_flags);
2438 arg_end = p;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let @r = expr": Set register contents.
2447 */
2448 else if (*arg == '@')
2449 {
2450 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 if (op != NULL && (*op == '+' || *op == '-'))
2452 EMSG2(_(e_letwrong), op);
2453 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *s;
2460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 p = get_tv_string_chk(tv);
2462 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (s != NULL)
2466 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(s);
2469 }
2470 }
2471 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 arg_end = arg + 1;
2475 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002476 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002488 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501
2502 else
2503 EMSG2(_(e_invarg2), arg);
2504
2505 return arg_end;
2506}
2507
2508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2510 */
2511 static int
2512check_changedtick(arg)
2513 char_u *arg;
2514{
2515 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2516 {
2517 EMSG2(_(e_readonlyvar), arg);
2518 return TRUE;
2519 }
2520 return FALSE;
2521}
2522
2523/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Get an lval: variable, Dict item or List item that can be assigned a value
2525 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2526 * "name.key", "name.key[expr]" etc.
2527 * Indexing only works if "name" is an existing List or Dictionary.
2528 * "name" points to the start of the name.
2529 * If "rettv" is not NULL it points to the value to be assigned.
2530 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2531 * wrong; must end in space or cmd separator.
2532 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533 * flags:
2534 * GLV_QUIET: do not give error messages
2535 * GLV_NO_AUTOLOAD: do not use script autoloading
2536 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002538 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
2541 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
2545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int unlet;
2547 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 char_u *expr_start, *expr_end;
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 dictitem_T *v;
2555 typval_T var1;
2556 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566
2567 if (skip)
2568 {
2569 /* When skipping just find the end of the name. */
2570 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 }
2573
2574 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (expr_start != NULL)
2577 {
2578 /* Don't expand the name when we already know there is an error. */
2579 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2580 && *p != '[' && *p != '.')
2581 {
2582 EMSG(_(e_trailing));
2583 return NULL;
2584 }
2585
2586 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2587 if (lp->ll_exp_name == NULL)
2588 {
2589 /* Report an invalid expression in braces, unless the
2590 * expression evaluation has been cancelled due to an
2591 * aborting error, an interrupt, or an exception. */
2592 if (!aborting() && !quiet)
2593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002594 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 EMSG2(_(e_invarg2), name);
2596 return NULL;
2597 }
2598 }
2599 lp->ll_name = lp->ll_exp_name;
2600 }
2601 else
2602 lp->ll_name = name;
2603
2604 /* Without [idx] or .key we are done. */
2605 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2606 return p;
2607
2608 cc = *p;
2609 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (v == NULL && !quiet)
2612 EMSG2(_(e_undefvar), lp->ll_name);
2613 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 if (v == NULL)
2615 return NULL;
2616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /*
2618 * Loop until no more [idx] or .key is following.
2619 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2624 && !(lp->ll_tv->v_type == VAR_DICT
2625 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E689: Can only index a List or Dictionary"));
2629 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E708: [:] must come last"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 len = -1;
2639 if (*p == '.')
2640 {
2641 key = p + 1;
2642 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2643 ;
2644 if (len == 0)
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_(e_emptykey));
2648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = key + len;
2651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 else
2653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*p == ':')
2657 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 empty1 = FALSE;
2661 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var1) == NULL)
2664 {
2665 /* not a number or string */
2666 clear_tv(&var1);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670
2671 /* Optionally get the second index [ :expr]. */
2672 if (*p == ':')
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (rettv != NULL && (rettv->v_type != VAR_LIST
2683 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = skipwhite(p + 1);
2692 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var2) == NULL)
2704 {
2705 /* not a number or string */
2706 if (!empty1)
2707 clear_tv(&var1);
2708 clear_tv(&var2);
2709 return NULL;
2710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Skip to past ']'. */
2729 ++p;
2730 }
2731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
2734 if (len == -1)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*key == NUL)
2739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_list = NULL;
2747 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 /* When assigning to a scope dictionary check that a function and
2751 * variable name is valid (only variable name unless it is l: or
2752 * g: dictionary). Disallow overwriting a builtin function. */
2753 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 int prevval;
2756 int wrong;
2757
2758 if (len != -1)
2759 {
2760 prevval = key[len];
2761 key[len] = NUL;
2762 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002763 else
2764 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2766 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 || !valid_varname(key);
2769 if (len != -1)
2770 key[len] = prevval;
2771 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 return NULL;
2773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* Can't add "v:" variable. */
2778 if (lp->ll_dict == &vimvardict)
2779 {
2780 EMSG2(_(e_illvar), name);
2781 return NULL;
2782 }
2783
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 p = NULL;
2801 break;
2802 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* existing variable, need to check if it can be changed */
2804 else if (var_check_ro(lp->ll_di->di_flags, name))
2805 return NULL;
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 else
2812 {
2813 /*
2814 * Get the number and item for the only or first index of the List.
2815 */
2816 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
2819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
2822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_list = lp->ll_tv->vval.v_list;
2825 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2826 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (lp->ll_n1 < 0)
2829 {
2830 lp->ll_n1 = 0;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 }
2833 }
2834 if (lp->ll_li == NULL)
2835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
2843 /*
2844 * May need to find the item or absolute index for the second
2845 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 * When no index given: "lp->ll_empty2" is TRUE.
2847 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 {
2858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2866 if (lp->ll_n1 < 0)
2867 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2868 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 {
2870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return p;
2881}
2882
2883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 */
2886 static void
2887clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 vim_free(lp->ll_exp_name);
2891 vim_free(lp->ll_newkey);
2892}
2893
2894/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906{
2907 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 listitem_T *ri;
2909 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 cc = *endp;
2916 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 if (op != NULL && *op != '=')
2918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002923 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 {
2925 if (tv_op(&tv, rettv, op) == OK)
2926 set_var(lp->ll_name, &tv, FALSE);
2927 clear_tv(&tv);
2928 }
2929 }
2930 else
2931 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 else if (tv_check_lock(lp->ll_newkey == NULL
2936 ? lp->ll_tv->v_lock
2937 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2938 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 else if (lp->ll_range)
2940 {
2941 /*
2942 * Assign the List values to the list items.
2943 */
2944 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2948 else
2949 {
2950 clear_tv(&lp->ll_li->li_tv);
2951 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ri = ri->li_next;
2954 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2955 break;
2956 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002959 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 ri = NULL;
2962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_li = lp->ll_li->li_next;
2966 ++lp->ll_n1;
2967 }
2968 if (ri != NULL)
2969 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else if (lp->ll_empty2
2971 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 : lp->ll_n1 != lp->ll_n2)
2973 EMSG(_("E711: List value has not enough items"));
2974 }
2975 else
2976 {
2977 /*
2978 * Assign to a List or Dictionary item.
2979 */
2980 if (lp->ll_newkey != NULL)
2981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
2984 EMSG2(_(e_letwrong), op);
2985 return;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 if (di == NULL)
2991 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2993 {
2994 vim_free(di);
2995 return;
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (op != NULL && *op != '=')
3000 {
3001 tv_op(lp->ll_tv, rettv, op);
3002 return;
3003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /*
3008 * Assign the value to the variable or list item.
3009 */
3010 if (copy)
3011 copy_tv(rettv, lp->ll_tv);
3012 else
3013 {
3014 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003015 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 }
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019}
3020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3023 * Returns OK or FAIL.
3024 */
3025 static int
3026tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T *tv1;
3028 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *op;
3030{
3031 long n;
3032 char_u numbuf[NUMBUFLEN];
3033 char_u *s;
3034
3035 /* Can't do anything with a Funcref or a Dict on the right. */
3036 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3037 {
3038 switch (tv1->v_type)
3039 {
3040 case VAR_DICT:
3041 case VAR_FUNC:
3042 break;
3043
3044 case VAR_LIST:
3045 if (*op != '+' || tv2->v_type != VAR_LIST)
3046 break;
3047 /* List += List */
3048 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3049 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3050 return OK;
3051
3052 case VAR_NUMBER:
3053 case VAR_STRING:
3054 if (tv2->v_type == VAR_LIST)
3055 break;
3056 if (*op == '+' || *op == '-')
3057 {
3058 /* nr += nr or nr -= nr*/
3059 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#ifdef FEAT_FLOAT
3061 if (tv2->v_type == VAR_FLOAT)
3062 {
3063 float_T f = n;
3064
3065 if (*op == '+')
3066 f += tv2->vval.v_float;
3067 else
3068 f -= tv2->vval.v_float;
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_FLOAT;
3071 tv1->vval.v_float = f;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
3075 {
3076 if (*op == '+')
3077 n += get_tv_number(tv2);
3078 else
3079 n -= get_tv_number(tv2);
3080 clear_tv(tv1);
3081 tv1->v_type = VAR_NUMBER;
3082 tv1->vval.v_number = n;
3083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 else
3086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 if (tv2->v_type == VAR_FLOAT)
3088 break;
3089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 /* str .= str */
3091 s = get_tv_string(tv1);
3092 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_STRING;
3095 tv1->vval.v_string = s;
3096 }
3097 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098
3099#ifdef FEAT_FLOAT
3100 case VAR_FLOAT:
3101 {
3102 float_T f;
3103
3104 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3105 && tv2->v_type != VAR_NUMBER
3106 && tv2->v_type != VAR_STRING))
3107 break;
3108 if (tv2->v_type == VAR_FLOAT)
3109 f = tv2->vval.v_float;
3110 else
3111 f = get_tv_number(tv2);
3112 if (*op == '+')
3113 tv1->vval.v_float += f;
3114 else
3115 tv1->vval.v_float -= f;
3116 }
3117 return OK;
3118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 }
3121
3122 EMSG2(_(e_letwrong), op);
3123 return FAIL;
3124}
3125
3126/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 * Add a watcher to a list.
3128 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003129 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
3134 lw->lw_next = l->lv_watch;
3135 l->lv_watch = lw;
3136}
3137
3138/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003139 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 * No warning when it isn't found...
3141 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 lwp = &l->lv_watch;
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 {
3152 if (lw == lwrem)
3153 {
3154 *lwp = lw->lw_next;
3155 break;
3156 }
3157 lwp = &lw->lw_next;
3158 }
3159}
3160
3161/*
3162 * Just before removing an item from a list: advance watchers to the next
3163 * item.
3164 */
3165 static void
3166list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 if (lw->lw_item == item)
3174 lw->lw_item = item->li_next;
3175}
3176
3177/*
3178 * Evaluate the expression used in a ":for var in expr" command.
3179 * "arg" points to "var".
3180 * Set "*errp" to TRUE for an error, FALSE otherwise;
3181 * Return a pointer that holds the info. Null when there is an error.
3182 */
3183 void *
3184eval_for_line(arg, errp, nextcmdp, skip)
3185 char_u *arg;
3186 int *errp;
3187 char_u **nextcmdp;
3188 int skip;
3189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 typval_T tv;
3193 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 *errp = TRUE; /* default: there is an error */
3196
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 if (fi == NULL)
3199 return NULL;
3200
3201 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3202 if (expr == NULL)
3203 return fi;
3204
3205 expr = skipwhite(expr);
3206 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003208 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 return fi;
3210 }
3211
3212 if (skip)
3213 ++emsg_skip;
3214 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3215 {
3216 *errp = FALSE;
3217 if (!skip)
3218 {
3219 l = tv.vval.v_list;
3220 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 clear_tv(&tv);
3224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 else
3226 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003227 /* No need to increment the refcount, it's already set for the
3228 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 fi->fi_list = l;
3230 list_add_watch(l, &fi->fi_lw);
3231 fi->fi_lw.lw_item = l->lv_first;
3232 }
3233 }
3234 }
3235 if (skip)
3236 --emsg_skip;
3237
3238 return fi;
3239}
3240
3241/*
3242 * Use the first item in a ":for" list. Advance to the next.
3243 * Assign the values to the variable (list). "arg" points to the first one.
3244 * Return TRUE when a valid item was found, FALSE when at end of list or
3245 * something wrong.
3246 */
3247 int
3248next_for_item(fi_void, arg)
3249 void *fi_void;
3250 char_u *arg;
3251{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 item = fi->fi_lw.lw_item;
3257 if (item == NULL)
3258 result = FALSE;
3259 else
3260 {
3261 fi->fi_lw.lw_item = item->li_next;
3262 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3263 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3264 }
3265 return result;
3266}
3267
3268/*
3269 * Free the structure used to store info used by ":for".
3270 */
3271 void
3272free_for_info(fi_void)
3273 void *fi_void;
3274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003277 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 list_unref(fi->fi_list);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 vim_free(fi);
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287 void
3288set_context_for_expression(xp, arg, cmdidx)
3289 expand_T *xp;
3290 char_u *arg;
3291 cmdidx_T cmdidx;
3292{
3293 int got_eq = FALSE;
3294 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (cmdidx == CMD_let)
3298 {
3299 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003303 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 {
3305 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 if (vim_iswhite(*p))
3308 break;
3309 }
3310 return;
3311 }
3312 }
3313 else
3314 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3315 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 while ((xp->xp_pattern = vim_strpbrk(arg,
3317 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3318 {
3319 c = *xp->xp_pattern;
3320 if (c == '&')
3321 {
3322 c = xp->xp_pattern[1];
3323 if (c == '&')
3324 {
3325 ++xp->xp_pattern;
3326 xp->xp_context = cmdidx != CMD_let || got_eq
3327 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3328 }
3329 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003332 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3333 xp->xp_pattern += 2;
3334
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (c == '$')
3338 {
3339 /* environment variable */
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 }
3342 else if (c == '=')
3343 {
3344 got_eq = TRUE;
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else if (c == '<'
3348 && xp->xp_context == EXPAND_FUNCTIONS
3349 && vim_strchr(xp->xp_pattern, '(') == NULL)
3350 {
3351 /* Function name can start with "<SNR>" */
3352 break;
3353 }
3354 else if (cmdidx != CMD_let || got_eq)
3355 {
3356 if (c == '"') /* string */
3357 {
3358 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3359 if (c == '\\' && xp->xp_pattern[1] != NUL)
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_NOTHING;
3362 }
3363 else if (c == '\'') /* literal string */
3364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3367 /* skip */ ;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '|')
3371 {
3372 if (xp->xp_pattern[1] == '|')
3373 {
3374 ++xp->xp_pattern;
3375 xp->xp_context = EXPAND_EXPRESSION;
3376 }
3377 else
3378 xp->xp_context = EXPAND_COMMANDS;
3379 }
3380 else
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 /* Doesn't look like something valid, expand as an expression
3385 * anyway. */
3386 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 arg = xp->xp_pattern;
3388 if (*arg != NUL)
3389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3390 /* skip */ ;
3391 }
3392 xp->xp_pattern = arg;
3393}
3394
3395#endif /* FEAT_CMDL_COMPL */
3396
3397/*
3398 * ":1,25call func(arg1, arg2)" function call.
3399 */
3400 void
3401ex_call(eap)
3402 exarg_T *eap;
3403{
3404 char_u *arg = eap->arg;
3405 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 linenr_T lnum;
3411 int doesrange;
3412 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 if (eap->skip)
3416 {
3417 /* trans_function_name() doesn't work well when skipping, use eval0()
3418 * instead to skip to any following command, e.g. for:
3419 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003420 ++emsg_skip;
3421 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3422 clear_tv(&rettv);
3423 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003424 return;
3425 }
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003428 if (fudi.fd_newkey != NULL)
3429 {
3430 /* Still need to give an error message for missing key. */
3431 EMSG2(_(e_dictkey), fudi.fd_newkey);
3432 vim_free(fudi.fd_newkey);
3433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 if (tofree == NULL)
3435 return;
3436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003437 /* Increase refcount on dictionary, it could get deleted when evaluating
3438 * the arguments. */
3439 if (fudi.fd_dict != NULL)
3440 ++fudi.fd_dict->dv_refcount;
3441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003444 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar532c7802005-01-27 14:44:31 +00003446 /* Skip white space to allow ":call func ()". Not good, but required for
3447 * backward compatibility. */
3448 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (*startarg != '(')
3452 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003453 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 goto end;
3455 }
3456
3457 /*
3458 * When skipping, evaluate the function once, to find the end of the
3459 * arguments.
3460 * When the function takes a range, this is discovered after the first
3461 * call, and the loop is broken.
3462 */
3463 if (eap->skip)
3464 {
3465 ++emsg_skip;
3466 lnum = eap->line2; /* do it once, also with an invalid range */
3467 }
3468 else
3469 lnum = eap->line1;
3470 for ( ; lnum <= eap->line2; ++lnum)
3471 {
3472 if (!eap->skip && eap->addr_count > 0)
3473 {
3474 curwin->w_cursor.lnum = lnum;
3475 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003476#ifdef FEAT_VIRTUALEDIT
3477 curwin->w_cursor.coladd = 0;
3478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 eap->line1, eap->line2, &doesrange,
3483 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 failed = TRUE;
3486 break;
3487 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
3489 /* Handle a function returning a Funcref, Dictionary or List. */
3490 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3491 {
3492 failed = TRUE;
3493 break;
3494 }
3495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (doesrange || eap->skip)
3498 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (aborting())
3505 break;
3506 }
3507 if (eap->skip)
3508 --emsg_skip;
3509
3510 if (!failed)
3511 {
3512 /* Check for trailing illegal characters and a following command. */
3513 if (!ends_excmd(*arg))
3514 {
3515 emsg_severe = TRUE;
3516 EMSG(_(e_trailing));
3517 }
3518 else
3519 eap->nextcmd = check_nextcmd(arg);
3520 }
3521
3522end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003524 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * ":unlet[!] var1 ... " command.
3529 */
3530 void
3531ex_unlet(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 ex_unletlock(eap, eap->arg, 0);
3535}
3536
3537/*
3538 * ":lockvar" and ":unlockvar" commands
3539 */
3540 void
3541ex_lockvar(eap)
3542 exarg_T *eap;
3543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 int deep = 2;
3546
3547 if (eap->forceit)
3548 deep = -1;
3549 else if (vim_isdigit(*arg))
3550 {
3551 deep = getdigits(&arg);
3552 arg = skipwhite(arg);
3553 }
3554
3555 ex_unletlock(eap, arg, deep);
3556}
3557
3558/*
3559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3560 */
3561 static void
3562ex_unletlock(eap, argstart, deep)
3563 exarg_T *eap;
3564 char_u *argstart;
3565 int deep;
3566{
3567 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 do
3573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003575 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003576 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 if (lv.ll_name == NULL)
3578 error = TRUE; /* error but continue parsing */
3579 if (name_end == NULL || (!vim_iswhite(*name_end)
3580 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (name_end != NULL)
3583 {
3584 emsg_severe = TRUE;
3585 EMSG(_(e_trailing));
3586 }
3587 if (!(eap->skip || error))
3588 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591
3592 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 {
3594 if (eap->cmdidx == CMD_unlet)
3595 {
3596 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3597 error = TRUE;
3598 }
3599 else
3600 {
3601 if (do_lock_var(&lv, name_end, deep,
3602 eap->cmdidx == CMD_lockvar) == FAIL)
3603 error = TRUE;
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (!eap->skip)
3608 clear_lval(&lv);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(name_end);
3611 } while (!ends_excmd(*arg));
3612
3613 eap->nextcmd = check_nextcmd(arg);
3614}
3615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 int forceit;
3621{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 int ret = OK;
3623 int cc;
3624
3625 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 cc = *name_end;
3628 *name_end = NUL;
3629
3630 /* Normal name or expanded name. */
3631 if (check_changedtick(lp->ll_name))
3632 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3638 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else if (lp->ll_range)
3640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642
3643 /* Delete a range of List items. */
3644 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 li = lp->ll_li->li_next;
3647 listitem_remove(lp->ll_list, lp->ll_li);
3648 lp->ll_li = li;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else
3653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a List item. */
3656 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003659 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 }
3661
3662 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663}
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hashtab_T *ht;
3675 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003677 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 ht = find_var_ht(name, &varname);
3680 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = hash_find(ht, varname);
3683 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003685 di = HI2DI(hi);
3686 if (var_check_fixed(di->di_flags, name)
3687 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 return FAIL;
3689 delete_var(ht, hi);
3690 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 if (forceit)
3694 return OK;
3695 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697}
3698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699/*
3700 * Lock or unlock variable indicated by "lp".
3701 * "deep" is the levels to go (-1 for unlimited);
3702 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3703 */
3704 static int
3705do_lock_var(lp, name_end, deep, lock)
3706 lval_T *lp;
3707 char_u *name_end;
3708 int deep;
3709 int lock;
3710{
3711 int ret = OK;
3712 int cc;
3713 dictitem_T *di;
3714
3715 if (deep == 0) /* nothing to do */
3716 return OK;
3717
3718 if (lp->ll_tv == NULL)
3719 {
3720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003728 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 if (di == NULL)
3730 ret = FAIL;
3731 else
3732 {
3733 if (lock)
3734 di->di_flags |= DI_FLAGS_LOCK;
3735 else
3736 di->di_flags &= ~DI_FLAGS_LOCK;
3737 item_lock(&di->di_tv, deep, lock);
3738 }
3739 }
3740 *name_end = cc;
3741 }
3742 else if (lp->ll_range)
3743 {
3744 listitem_T *li = lp->ll_li;
3745
3746 /* (un)lock a range of List items. */
3747 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3748 {
3749 item_lock(&li->li_tv, deep, lock);
3750 li = li->li_next;
3751 ++lp->ll_n1;
3752 }
3753 }
3754 else if (lp->ll_list != NULL)
3755 /* (un)lock a List item. */
3756 item_lock(&lp->ll_li->li_tv, deep, lock);
3757 else
3758 /* un(lock) a Dictionary item. */
3759 item_lock(&lp->ll_di->di_tv, deep, lock);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Lock or unlock an item. "deep" is nr of levels to go.
3766 */
3767 static void
3768item_lock(tv, deep, lock)
3769 typval_T *tv;
3770 int deep;
3771 int lock;
3772{
3773 static int recurse = 0;
3774 list_T *l;
3775 listitem_T *li;
3776 dict_T *d;
3777 hashitem_T *hi;
3778 int todo;
3779
3780 if (recurse >= DICT_MAXNEST)
3781 {
3782 EMSG(_("E743: variable nested too deep for (un)lock"));
3783 return;
3784 }
3785 if (deep == 0)
3786 return;
3787 ++recurse;
3788
3789 /* lock/unlock the item itself */
3790 if (lock)
3791 tv->v_lock |= VAR_LOCKED;
3792 else
3793 tv->v_lock &= ~VAR_LOCKED;
3794
3795 switch (tv->v_type)
3796 {
3797 case VAR_LIST:
3798 if ((l = tv->vval.v_list) != NULL)
3799 {
3800 if (lock)
3801 l->lv_lock |= VAR_LOCKED;
3802 else
3803 l->lv_lock &= ~VAR_LOCKED;
3804 if (deep < 0 || deep > 1)
3805 /* recursive: lock/unlock the items the List contains */
3806 for (li = l->lv_first; li != NULL; li = li->li_next)
3807 item_lock(&li->li_tv, deep - 1, lock);
3808 }
3809 break;
3810 case VAR_DICT:
3811 if ((d = tv->vval.v_dict) != NULL)
3812 {
3813 if (lock)
3814 d->dv_lock |= VAR_LOCKED;
3815 else
3816 d->dv_lock &= ~VAR_LOCKED;
3817 if (deep < 0 || deep > 1)
3818 {
3819 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
3826 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3827 }
3828 }
3829 }
3830 }
3831 }
3832 --recurse;
3833}
3834
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003836 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3837 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003838 */
3839 static int
3840tv_islocked(tv)
3841 typval_T *tv;
3842{
3843 return (tv->v_lock & VAR_LOCKED)
3844 || (tv->v_type == VAR_LIST
3845 && tv->vval.v_list != NULL
3846 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3847 || (tv->v_type == VAR_DICT
3848 && tv->vval.v_dict != NULL
3849 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3853/*
3854 * Delete all "menutrans_" variables.
3855 */
3856 void
3857del_menutrans_vars()
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3870 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 }
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875#endif
3876
3877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3878
3879/*
3880 * Local string buffer for the next two functions to store a variable name
3881 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3882 * get_user_var_name().
3883 */
3884
3885static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3886
3887static char_u *varnamebuf = NULL;
3888static int varnamebuflen = 0;
3889
3890/*
3891 * Function to concatenate a prefix and a variable name.
3892 */
3893 static char_u *
3894cat_prefix_varname(prefix, name)
3895 int prefix;
3896 char_u *name;
3897{
3898 int len;
3899
3900 len = (int)STRLEN(name) + 3;
3901 if (len > varnamebuflen)
3902 {
3903 vim_free(varnamebuf);
3904 len += 10; /* some additional space */
3905 varnamebuf = alloc(len);
3906 if (varnamebuf == NULL)
3907 {
3908 varnamebuflen = 0;
3909 return NULL;
3910 }
3911 varnamebuflen = len;
3912 }
3913 *varnamebuf = prefix;
3914 varnamebuf[1] = ':';
3915 STRCPY(varnamebuf + 2, name);
3916 return varnamebuf;
3917}
3918
3919/*
3920 * Function given to ExpandGeneric() to obtain the list of user defined
3921 * (global/buffer/window/built-in) variable names.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *
3924get_user_var_name(xp, idx)
3925 expand_T *xp;
3926 int idx;
3927{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 static long_u gdone;
3929 static long_u bdone;
3930 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 static long_u tdone;
3933#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static int vidx;
3935 static hashitem_T *hi;
3936 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941#ifdef FEAT_WINDOWS
3942 tdone = 0;
3943#endif
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* Global variables */
3947 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003951 else
3952 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3956 return cat_prefix_varname('g', hi->hi_key);
3957 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return (char_u *)"b:changedtick";
3976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977
3978 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003979 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003982 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 else
3985 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003993 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 if (tdone < ht->ht_used)
3995 {
3996 if (tdone++ == 0)
3997 hi = ht->ht_array;
3998 else
3999 ++hi;
4000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 return cat_prefix_varname('t', hi->hi_key);
4003 }
4004#endif
4005
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 /* v: variables */
4007 if (vidx < VV_LEN)
4008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 vim_free(varnamebuf);
4011 varnamebuf = NULL;
4012 varnamebuflen = 0;
4013 return NULL;
4014}
4015
4016#endif /* FEAT_CMDL_COMPL */
4017
4018/*
4019 * types for expressions.
4020 */
4021typedef enum
4022{
4023 TYPE_UNKNOWN = 0
4024 , TYPE_EQUAL /* == */
4025 , TYPE_NEQUAL /* != */
4026 , TYPE_GREATER /* > */
4027 , TYPE_GEQUAL /* >= */
4028 , TYPE_SMALLER /* < */
4029 , TYPE_SEQUAL /* <= */
4030 , TYPE_MATCH /* =~ */
4031 , TYPE_NOMATCH /* !~ */
4032} exptype_T;
4033
4034/*
4035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4038 */
4039
4040/*
4041 * Handle zero level expression.
4042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **nextcmd;
4052 int evaluate;
4053{
4054 int ret;
4055 char_u *p;
4056
4057 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (ret == FAIL || !ends_excmd(*p))
4060 {
4061 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Report the invalid expression unless the expression evaluation has
4065 * been cancelled due to an aborting error, an interrupt, or an
4066 * exception.
4067 */
4068 if (!aborting())
4069 EMSG2(_(e_invexpr2), arg);
4070 ret = FAIL;
4071 }
4072 if (nextcmd != NULL)
4073 *nextcmd = check_nextcmd(p);
4074
4075 return ret;
4076}
4077
4078/*
4079 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004080 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * "arg" must point to the first non-white of the expression.
4083 * "arg" is advanced to the next non-white after the recognized expression.
4084 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
4086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
4095 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Get the first variable.
4100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 if ((*arg)[0] == '?')
4105 {
4106 result = FALSE;
4107 if (evaluate)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int error = FALSE;
4110
4111 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Check for the ":".
4127 */
4128 if ((*arg)[0] != ':')
4129 {
4130 EMSG(_("E109: Missing ':' after '?'"));
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 /*
4137 * Get the third variable.
4138 */
4139 *arg = skipwhite(*arg + 1);
4140 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4141 {
4142 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 return OK;
4151}
4152
4153/*
4154 * Handle first level expression:
4155 * expr2 || expr2 || expr2 logical OR
4156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 long result;
4170 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat until there is no following "||".
4181 */
4182 first = TRUE;
4183 result = FALSE;
4184 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4185 {
4186 if (evaluate && first)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 first = FALSE;
4194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 2);
4200 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4201 return FAIL;
4202
4203 /*
4204 * Compute the result.
4205 */
4206 if (evaluate && !result)
4207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (error)
4212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (evaluate)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * Handle second level expression:
4226 * expr3 && expr3 && expr3 logical AND
4227 *
4228 * "arg" must point to the first non-white of the expression.
4229 * "arg" is advanced to the next non-white after the recognized expression.
4230 *
4231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int evaluate;
4238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "&&".
4252 */
4253 first = TRUE;
4254 result = TRUE;
4255 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval4(arg, &var2, evaluate && result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle third level expression:
4297 * var1 == var2
4298 * var1 =~ var2
4299 * var1 != var2
4300 * var1 !~ var2
4301 * var1 > var2
4302 * var1 >= var2
4303 * var1 < var2
4304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 * var1 is var2
4306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *p;
4321 int i;
4322 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int len = 2;
4325 long n1, n2;
4326 char_u *s1, *s2;
4327 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4328 regmatch_T regmatch;
4329 int ic;
4330 char_u *save_cpo;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 p = *arg;
4339 switch (p[0])
4340 {
4341 case '=': if (p[1] == '=')
4342 type = TYPE_EQUAL;
4343 else if (p[1] == '~')
4344 type = TYPE_MATCH;
4345 break;
4346 case '!': if (p[1] == '=')
4347 type = TYPE_NEQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_NOMATCH;
4350 break;
4351 case '>': if (p[1] != '=')
4352 {
4353 type = TYPE_GREATER;
4354 len = 1;
4355 }
4356 else
4357 type = TYPE_GEQUAL;
4358 break;
4359 case '<': if (p[1] != '=')
4360 {
4361 type = TYPE_SMALLER;
4362 len = 1;
4363 }
4364 else
4365 type = TYPE_SEQUAL;
4366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 case 'i': if (p[1] == 's')
4368 {
4369 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4370 len = 5;
4371 if (!vim_isIDc(p[len]))
4372 {
4373 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4374 type_is = TRUE;
4375 }
4376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 if (type != TYPE_UNKNOWN)
4384 {
4385 /* extra question mark appended: ignore case */
4386 if (p[len] == '?')
4387 {
4388 ic = TRUE;
4389 ++len;
4390 }
4391 /* extra '#' appended: match case */
4392 else if (p[len] == '#')
4393 {
4394 ic = FALSE;
4395 ++len;
4396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 ic = p_ic;
4400
4401 /*
4402 * Get the second variable.
4403 */
4404 *arg = skipwhite(p + len);
4405 if (eval5(arg, &var2, evaluate) == FAIL)
4406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410
4411 if (evaluate)
4412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type_is && rettv->v_type != var2.v_type)
4414 {
4415 /* For "is" a different type always means FALSE, for "notis"
4416 * it means TRUE. */
4417 n1 = (type == TYPE_NEQUAL);
4418 }
4419 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4420 {
4421 if (type_is)
4422 {
4423 n1 = (rettv->v_type == var2.v_type
4424 && rettv->vval.v_list == var2.vval.v_list);
4425 if (type == TYPE_NEQUAL)
4426 n1 = !n1;
4427 }
4428 else if (rettv->v_type != var2.v_type
4429 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4430 {
4431 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004434 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 clear_tv(rettv);
4436 clear_tv(&var2);
4437 return FAIL;
4438 }
4439 else
4440 {
4441 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004442 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4443 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_dict == var2.vval.v_dict);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
4462 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4463 else
4464 EMSG(_("E736: Invalid operation for Dictionary"));
4465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4473 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4480 {
4481 if (rettv->v_type != var2.v_type
4482 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4483 {
4484 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 clear_tv(rettv);
4489 clear_tv(&var2);
4490 return FAIL;
4491 }
4492 else
4493 {
4494 /* Compare two Funcrefs for being equal or unequal. */
4495 if (rettv->vval.v_string == NULL
4496 || var2.vval.v_string == NULL)
4497 n1 = FALSE;
4498 else
4499 n1 = STRCMP(rettv->vval.v_string,
4500 var2.vval.v_string) == 0;
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506#ifdef FEAT_FLOAT
4507 /*
4508 * If one of the two variables is a float, compare as a float.
4509 * When using "=~" or "!~", always compare as string.
4510 */
4511 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4512 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4513 {
4514 float_T f1, f2;
4515
4516 if (rettv->v_type == VAR_FLOAT)
4517 f1 = rettv->vval.v_float;
4518 else
4519 f1 = get_tv_number(rettv);
4520 if (var2.v_type == VAR_FLOAT)
4521 f2 = var2.vval.v_float;
4522 else
4523 f2 = get_tv_number(&var2);
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (f1 == f2); break;
4528 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4529 case TYPE_GREATER: n1 = (f1 > f2); break;
4530 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4531 case TYPE_SMALLER: n1 = (f1 < f2); break;
4532 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /*
4541 * If one of the two variables is a number, compare as a number.
4542 * When using "=~" or "!~", always compare as string.
4543 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(rettv);
4548 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (n1 == n2); break;
4552 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4553 case TYPE_GREATER: n1 = (n1 > n2); break;
4554 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4555 case TYPE_SMALLER: n1 = (n1 < n2); break;
4556 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4557 case TYPE_UNKNOWN:
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH: break; /* avoid gcc warning */
4560 }
4561 }
4562 else
4563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 s1 = get_tv_string_buf(rettv, buf1);
4565 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4567 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4568 else
4569 i = 0;
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (i == 0); break;
4574 case TYPE_NEQUAL: n1 = (i != 0); break;
4575 case TYPE_GREATER: n1 = (i > 0); break;
4576 case TYPE_GEQUAL: n1 = (i >= 0); break;
4577 case TYPE_SMALLER: n1 = (i < 0); break;
4578 case TYPE_SEQUAL: n1 = (i <= 0); break;
4579
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH:
4582 /* avoid 'l' flag in 'cpoptions' */
4583 save_cpo = p_cpo;
4584 p_cpo = (char_u *)"";
4585 regmatch.regprog = vim_regcomp(s2,
4586 RE_MAGIC + RE_STRING);
4587 regmatch.rm_ic = ic;
4588 if (regmatch.regprog != NULL)
4589 {
4590 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (type == TYPE_NOMATCH)
4593 n1 = !n1;
4594 }
4595 p_cpo = save_cpo;
4596 break;
4597
4598 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4599 }
4600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 clear_tv(rettv);
4602 clear_tv(&var2);
4603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * Handle fourth level expression:
4613 * + number addition
4614 * - number subtraction
4615 * . string concatenation
4616 *
4617 * "arg" must point to the first non-white of the expression.
4618 * "arg" is advanced to the next non-white after the recognized expression.
4619 *
4620 * Return OK or FAIL.
4621 */
4622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int evaluate;
4627{
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 typval_T var2;
4629 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int op;
4631 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632#ifdef FEAT_FLOAT
4633 float_T f1 = 0, f2 = 0;
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *s1, *s2;
4636 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4637 char_u *p;
4638
4639 /*
4640 * Get the first variable.
4641 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
4644
4645 /*
4646 * Repeat computing, until no '+', '-' or '.' is following.
4647 */
4648 for (;;)
4649 {
4650 op = **arg;
4651 if (op != '+' && op != '-' && op != '.')
4652 break;
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 if ((op != '+' || rettv->v_type != VAR_LIST)
4655#ifdef FEAT_FLOAT
4656 && (op == '.' || rettv->v_type != VAR_FLOAT)
4657#endif
4658 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 {
4660 /* For "list + ...", an illegal use of the first operand as
4661 * a number cannot be determined before evaluating the 2nd
4662 * operand: if this is also a list, all is ok.
4663 * For "something . ...", "something - ..." or "non-list + ...",
4664 * we know that the first operand needs to be a string or number
4665 * without evaluating the 2nd operand. So check before to avoid
4666 * side effects after an error. */
4667 if (evaluate && get_tv_string_chk(rettv) == NULL)
4668 {
4669 clear_tv(rettv);
4670 return FAIL;
4671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
4675 * Get the second variable.
4676 */
4677 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004678 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683
4684 if (evaluate)
4685 {
4686 /*
4687 * Compute the result.
4688 */
4689 if (op == '.')
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4692 s2 = get_tv_string_buf_chk(&var2, buf2);
4693 if (s2 == NULL) /* type error ? */
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 rettv->v_type = VAR_STRING;
4702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 else if (op == '+' && rettv->v_type == VAR_LIST
4705 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 {
4707 /* concatenate Lists */
4708 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4709 &var3) == FAIL)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715 clear_tv(rettv);
4716 *rettv = var3;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 f1 = rettv->vval.v_float;
4726 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 else
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 n1 = get_tv_number_chk(rettv, &error);
4732 if (error)
4733 {
4734 /* This can only happen for "list + non-list". For
4735 * "non-list + ..." or "something - ...", we returned
4736 * before evaluating the 2nd operand. */
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (var2.v_type == VAR_FLOAT)
4742 f1 = n1;
4743#endif
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 {
4748 f2 = var2.vval.v_float;
4749 n2 = 0;
4750 }
4751 else
4752#endif
4753 {
4754 n2 = get_tv_number_chk(&var2, &error);
4755 if (error)
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
4761#ifdef FEAT_FLOAT
4762 if (rettv->v_type == VAR_FLOAT)
4763 f2 = n2;
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767
4768#ifdef FEAT_FLOAT
4769 /* If there is a float on either side the result is a float. */
4770 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4771 {
4772 if (op == '+')
4773 f1 = f1 + f2;
4774 else
4775 f1 = f1 - f2;
4776 rettv->v_type = VAR_FLOAT;
4777 rettv->vval.v_float = f1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#endif
4781 {
4782 if (op == '+')
4783 n1 = n1 + n2;
4784 else
4785 n1 = n1 - n2;
4786 rettv->v_type = VAR_NUMBER;
4787 rettv->vval.v_number = n1;
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793 return OK;
4794}
4795
4796/*
4797 * Handle fifth level expression:
4798 * * number multiplication
4799 * / number division
4800 * % number modulo
4801 *
4802 * "arg" must point to the first non-white of the expression.
4803 * "arg" is advanced to the next non-white after the recognized expression.
4804 *
4805 * Return OK or FAIL.
4806 */
4807 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int op;
4816 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 int use_float = FALSE;
4819 float_T f1 = 0, f2;
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 /*
4824 * Get the first variable.
4825 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 /*
4830 * Repeat computing, until no '*', '/' or '%' is following.
4831 */
4832 for (;;)
4833 {
4834 op = **arg;
4835 if (op != '*' && op != '/' && op != '%')
4836 break;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 {
4843 f1 = rettv->vval.v_float;
4844 use_float = TRUE;
4845 n1 = 0;
4846 }
4847 else
4848#endif
4849 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 if (error)
4852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else
4855 n1 = 0;
4856
4857 /*
4858 * Get the second variable.
4859 */
4860 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 if (evaluate)
4865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (var2.v_type == VAR_FLOAT)
4868 {
4869 if (!use_float)
4870 {
4871 f1 = n1;
4872 use_float = TRUE;
4873 }
4874 f2 = var2.vval.v_float;
4875 n2 = 0;
4876 }
4877 else
4878#endif
4879 {
4880 n2 = get_tv_number_chk(&var2, &error);
4881 clear_tv(&var2);
4882 if (error)
4883 return FAIL;
4884#ifdef FEAT_FLOAT
4885 if (use_float)
4886 f2 = n2;
4887#endif
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 if (op == '*')
4898 f1 = f1 * f2;
4899 else if (op == '/')
4900 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# ifdef VMS
4902 /* VMS crashes on divide by zero, work around it */
4903 if (f2 == 0.0)
4904 {
4905 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004910 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004911 }
4912 else
4913 f1 = f1 / f2;
4914# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 /* We rely on the floating point library to handle divide
4916 * by zero to result in "inf" and not a crash. */
4917 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004922 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 return FAIL;
4924 }
4925 rettv->v_type = VAR_FLOAT;
4926 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 n1 = n1 * n2;
4933 else if (op == '/')
4934 {
4935 if (n2 == 0) /* give an error message? */
4936 {
4937 if (n1 == 0)
4938 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4939 else if (n1 < 0)
4940 n1 = -0x7fffffffL;
4941 else
4942 n1 = 0x7fffffffL;
4943 }
4944 else
4945 n1 = n1 / n2;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
4949 if (n2 == 0) /* give an error message? */
4950 n1 = 0;
4951 else
4952 n1 = n1 % n2;
4953 }
4954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 }
4959
4960 return OK;
4961}
4962
4963/*
4964 * Handle sixth level expression:
4965 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004966 * "string" string constant
4967 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * &option-name option value
4969 * @r register contents
4970 * identifier variable value
4971 * function() function call
4972 * $VAR environment variable
4973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004974 * [expr, expr] List
4975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 *
4977 * Also handle:
4978 * ! in front logical NOT
4979 * - in front unary minus
4980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * trailing [] subscript in String or List
4982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * "arg" must point to the first non-white of the expression.
4985 * "arg" is advanced to the next non-white after the recognized expression.
4986 *
4987 * Return OK or FAIL.
4988 */
4989 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004990eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004994 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 long n;
4997 int len;
4998 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 char_u *start_leader, *end_leader;
5000 int ret = OK;
5001 char_u *alias;
5002
5003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Skip '!' and '-' characters. They are handled later.
5011 */
5012 start_leader = *arg;
5013 while (**arg == '!' || **arg == '-' || **arg == '+')
5014 *arg = skipwhite(*arg + 1);
5015 end_leader = *arg;
5016
5017 switch (**arg)
5018 {
5019 /*
5020 * Number constant.
5021 */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
5029 case '7':
5030 case '8':
5031 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 {
5033#ifdef FEAT_FLOAT
5034 char_u *p = skipdigits(*arg + 1);
5035 int get_float = FALSE;
5036
5037 /* We accept a float when the format matches
5038 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005039 * strict to avoid backwards compatibility problems.
5040 * Don't look for a float after the "." operator, so that
5041 * ":let vers = 1.2.3" doesn't fail. */
5042 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 get_float = TRUE;
5045 p = skipdigits(p + 2);
5046 if (*p == 'e' || *p == 'E')
5047 {
5048 ++p;
5049 if (*p == '-' || *p == '+')
5050 ++p;
5051 if (!vim_isdigit(*p))
5052 get_float = FALSE;
5053 else
5054 p = skipdigits(p + 1);
5055 }
5056 if (ASCII_ISALPHA(*p) || *p == '.')
5057 get_float = FALSE;
5058 }
5059 if (get_float)
5060 {
5061 float_T f;
5062
5063 *arg += string2float(*arg, &f);
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_FLOAT;
5067 rettv->vval.v_float = f;
5068 }
5069 }
5070 else
5071#endif
5072 {
5073 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5074 *arg += len;
5075 if (evaluate)
5076 {
5077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = n;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * String constant: "string".
5086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 break;
5095
5096 /*
5097 * List: [expr, expr]
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Dictionary: {key: val, key: val}
5104 */
5105 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5106 break;
5107
5108 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
5115 * Environment variable: $VAR.
5116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Register contents: @r.
5122 */
5123 case '@': ++*arg;
5124 if (evaluate)
5125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005127 rettv->vval.v_string = get_reg_contents(**arg,
5128 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 if (**arg != NUL)
5131 ++*arg;
5132 break;
5133
5134 /*
5135 * nested expression: (expression).
5136 */
5137 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (**arg == ')')
5140 ++*arg;
5141 else if (ret == OK)
5142 {
5143 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ret = FAIL;
5146 }
5147 break;
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 if (ret == NOTDONE)
5154 {
5155 /*
5156 * Must be a variable or function name.
5157 * Can also be a curly-braces kind of name: {expr}.
5158 */
5159 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (alias != NULL)
5162 s = alias;
5163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 ret = FAIL;
5166 else
5167 {
5168 if (**arg == '(') /* recursive! */
5169 {
5170 /* If "s" is the name of a variable of type VAR_FUNC
5171 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005172 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173
5174 /* Invoke the function. */
5175 ret = get_func_tv(s, len, rettv, arg,
5176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005178
5179 /* If evaluate is FALSE rettv->v_type was not set in
5180 * get_func_tv, but it's needed in handle_subscript() to parse
5181 * what follows. So set it here. */
5182 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5183 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005184 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185 rettv->v_type = VAR_FUNC;
5186 }
5187
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /* Stop the expression evaluation when immediately
5189 * aborting on error, or when an interrupt occurred or
5190 * an exception was thrown but not caught. */
5191 if (aborting())
5192 {
5193 if (ret == OK)
5194 clear_tv(rettv);
5195 ret = FAIL;
5196 }
5197 }
5198 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005199 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005200 else
5201 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005203 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *arg = skipwhite(*arg);
5207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5209 * expr(expr). */
5210 if (ret == OK)
5211 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /*
5214 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5215 */
5216 if (ret == OK && evaluate && end_leader > start_leader)
5217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 int val = 0;
5220#ifdef FEAT_FLOAT
5221 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 if (rettv->v_type == VAR_FLOAT)
5224 f = rettv->vval.v_float;
5225 else
5226#endif
5227 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 clear_tv(rettv);
5231 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else
5234 {
5235 while (end_leader > start_leader)
5236 {
5237 --end_leader;
5238 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 {
5240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 f = !f;
5243 else
5244#endif
5245 val = !val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 {
5249#ifdef FEAT_FLOAT
5250 if (rettv->v_type == VAR_FLOAT)
5251 f = -f;
5252 else
5253#endif
5254 val = -val;
5255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257#ifdef FEAT_FLOAT
5258 if (rettv->v_type == VAR_FLOAT)
5259 {
5260 clear_tv(rettv);
5261 rettv->vval.v_float = f;
5262 }
5263 else
5264#endif
5265 {
5266 clear_tv(rettv);
5267 rettv->v_type = VAR_NUMBER;
5268 rettv->vval.v_number = val;
5269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
5273 return ret;
5274}
5275
5276/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005277 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5278 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5280 */
5281 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
5288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 long len = -1;
5292 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005296 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 return FAIL;
5301 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302#ifdef FEAT_FLOAT
5303 else if (rettv->v_type == VAR_FLOAT)
5304 {
5305 if (verbose)
5306 EMSG(_(e_float_as_string));
5307 return FAIL;
5308 }
5309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 /*
5314 * dict.name
5315 */
5316 key = *arg + 1;
5317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5318 ;
5319 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /*
5326 * something[idx]
5327 *
5328 * Get the (first) variable from inside the [].
5329 */
5330 *arg = skipwhite(*arg + 1);
5331 if (**arg == ':')
5332 empty1 = TRUE;
5333 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5334 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5336 {
5337 /* not a number or string */
5338 clear_tv(&var1);
5339 return FAIL;
5340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 /*
5343 * Get the second variable from inside the [:].
5344 */
5345 if (**arg == ':')
5346 {
5347 range = TRUE;
5348 *arg = skipwhite(*arg + 1);
5349 if (**arg == ']')
5350 empty2 = TRUE;
5351 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (!empty1)
5354 clear_tv(&var1);
5355 return FAIL;
5356 }
5357 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5358 {
5359 /* not a number or string */
5360 if (!empty1)
5361 clear_tv(&var1);
5362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 return FAIL;
5364 }
5365 }
5366
5367 /* Check for the ']'. */
5368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
5377 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 n1 = 0;
5383 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 n1 = get_tv_number(&var1);
5386 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 if (range)
5389 {
5390 if (empty2)
5391 n2 = -1;
5392 else
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 n2 = get_tv_number(&var2);
5395 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
5401 case VAR_NUMBER:
5402 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 len = (long)STRLEN(s);
5405 if (range)
5406 {
5407 /* The resulting variable is a substring. If the indexes
5408 * are out of range the result is empty. */
5409 if (n1 < 0)
5410 {
5411 n1 = len + n1;
5412 if (n1 < 0)
5413 n1 = 0;
5414 }
5415 if (n2 < 0)
5416 n2 = len + n2;
5417 else if (n2 >= len)
5418 n2 = len;
5419 if (n1 >= len || n2 < 0 || n1 > n2)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5423 }
5424 else
5425 {
5426 /* The resulting variable is a string of a single
5427 * character. If the index is too big or negative the
5428 * result is empty. */
5429 if (n1 >= len || n1 < 0)
5430 s = NULL;
5431 else
5432 s = vim_strnsave(s + n1, 1);
5433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 clear_tv(rettv);
5435 rettv->v_type = VAR_STRING;
5436 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 break;
5438
5439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 if (n1 < 0)
5442 n1 = len + n1;
5443 if (!empty1 && (n1 < 0 || n1 >= len))
5444 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005445 /* For a range we allow invalid values and return an empty
5446 * list. A list index out of range is an error. */
5447 if (!range)
5448 {
5449 if (verbose)
5450 EMSGN(_(e_listidx), n1);
5451 return FAIL;
5452 }
5453 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 if (range)
5456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 list_T *l;
5458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459
5460 if (n2 < 0)
5461 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005462 else if (n2 >= len)
5463 n2 = len - 1;
5464 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 l = list_alloc();
5467 if (l == NULL)
5468 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 n1 <= n2; ++n1)
5471 {
5472 if (list_append_tv(l, &item->li_tv) == FAIL)
5473 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005474 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
5476 }
5477 item = item->li_next;
5478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 clear_tv(rettv);
5480 rettv->v_type = VAR_LIST;
5481 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005482 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 else
5485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 clear_tv(rettv);
5488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
5492 case VAR_DICT:
5493 if (range)
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (len == -1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 if (len == -1)
5505 {
5506 key = get_tv_string(&var1);
5507 if (*key == NUL)
5508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005509 if (verbose)
5510 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 }
5515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 if (len == -1)
5521 clear_tv(&var1);
5522 if (item == NULL)
5523 return FAIL;
5524
5525 copy_tv(&item->di_tv, &var1);
5526 clear_tv(rettv);
5527 *rettv = var1;
5528 }
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Get an option value.
5538 * "arg" points to the '&' or '+' before the option name.
5539 * "arg" is advanced to character after the option name.
5540 * Return OK or FAIL.
5541 */
5542 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int evaluate;
5547{
5548 char_u *option_end;
5549 long numval;
5550 char_u *stringval;
5551 int opt_type;
5552 int c;
5553 int working = (**arg == '+'); /* has("+option") */
5554 int ret = OK;
5555 int opt_flags;
5556
5557 /*
5558 * Isolate the option name and find its value.
5559 */
5560 option_end = find_option_end(arg, &opt_flags);
5561 if (option_end == NULL)
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 EMSG2(_("E112: Option name missing: %s"), *arg);
5565 return FAIL;
5566 }
5567
5568 if (!evaluate)
5569 {
5570 *arg = option_end;
5571 return OK;
5572 }
5573
5574 c = *option_end;
5575 *option_end = NUL;
5576 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (opt_type == -3) /* invalid name */
5580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 EMSG2(_("E113: Unknown option: %s"), *arg);
5583 ret = FAIL;
5584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (opt_type == -2) /* hidden string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == -1) /* hidden number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == 1) /* number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else /* string option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 }
5608 else if (working && (opt_type == -2 || opt_type == -1))
5609 ret = FAIL;
5610
5611 *option_end = c; /* put back for error messages */
5612 *arg = option_end;
5613
5614 return ret;
5615}
5616
5617/*
5618 * Allocate a variable for a string constant.
5619 * Return OK or FAIL.
5620 */
5621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int evaluate;
5626{
5627 char_u *p;
5628 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int extra = 0;
5630
5631 /*
5632 * Find the end of the string, skipping backslashed characters.
5633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (*p == '\\' && p[1] != NUL)
5637 {
5638 ++p;
5639 /* A "\<x>" form occupies at least 4 characters, and produces up
5640 * to 6 characters: reserve space for 2 extra */
5641 if (*p == '<')
5642 extra += 2;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
5646 if (*p != '"')
5647 {
5648 EMSG2(_("E114: Missing quote: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 /* If only parsing, set *arg and return here */
5653 if (!evaluate)
5654 {
5655 *arg = p + 1;
5656 return OK;
5657 }
5658
5659 /*
5660 * Copy the string into allocated memory, handling backslashed
5661 * characters.
5662 */
5663 name = alloc((unsigned)(p - *arg + extra));
5664 if (name == NULL)
5665 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 rettv->v_type = VAR_STRING;
5667 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\')
5672 {
5673 switch (*++p)
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case 'b': *name++ = BS; ++p; break;
5676 case 'e': *name++ = ESC; ++p; break;
5677 case 'f': *name++ = FF; ++p; break;
5678 case 'n': *name++ = NL; ++p; break;
5679 case 'r': *name++ = CAR; ++p; break;
5680 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 case 'X': /* hex: "\x1", "\x12" */
5683 case 'x':
5684 case 'u': /* Unicode: "\u0023" */
5685 case 'U':
5686 if (vim_isxdigit(p[1]))
5687 {
5688 int n, nr;
5689 int c = toupper(*p);
5690
5691 if (c == 'X')
5692 n = 2;
5693 else
5694 n = 4;
5695 nr = 0;
5696 while (--n >= 0 && vim_isxdigit(p[1]))
5697 {
5698 ++p;
5699 nr = (nr << 4) + hex2nr(*p);
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#ifdef FEAT_MBYTE
5703 /* For "\u" store the number according to
5704 * 'encoding'. */
5705 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712
5713 /* octal: "\1", "\12", "\123" */
5714 case '0':
5715 case '1':
5716 case '2':
5717 case '3':
5718 case '4':
5719 case '5':
5720 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 case '7': *name = *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = (*name << 3) + *p++ - '0';
5725 if (*p >= '0' && *p <= '7')
5726 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 break;
5730
5731 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (extra != 0)
5734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 /* FALLTHROUGH */
5739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 }
5744 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg = p + 1;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return OK;
5752}
5753
5754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Return OK or FAIL.
5757 */
5758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 int evaluate;
5763{
5764 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 int reduce = 0;
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++reduce;
5778 ++p;
5779 }
5780 }
5781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return FAIL;
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 if (!evaluate)
5790 {
5791 *arg = p + 1;
5792 return OK;
5793 }
5794
5795 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 */
5798 str = alloc((unsigned)((p - *arg) - reduce));
5799 if (str == NULL)
5800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 rettv->v_type = VAR_STRING;
5802 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 break;
5810 ++p;
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 *arg = p + 1;
5816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 return OK;
5818}
5819
5820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 * Allocate a variable for a List and fill it from "*arg".
5822 * Return OK or FAIL.
5823 */
5824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 int evaluate;
5829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 list_T *l = NULL;
5831 typval_T tv;
5832 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
5834 if (evaluate)
5835 {
5836 l = list_alloc();
5837 if (l == NULL)
5838 return FAIL;
5839 }
5840
5841 *arg = skipwhite(*arg + 1);
5842 while (**arg != ']' && **arg != NUL)
5843 {
5844 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5845 goto failret;
5846 if (evaluate)
5847 {
5848 item = listitem_alloc();
5849 if (item != NULL)
5850 {
5851 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 list_append(l, item);
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 else
5856 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
5859 if (**arg == ']')
5860 break;
5861 if (**arg != ',')
5862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 goto failret;
5865 }
5866 *arg = skipwhite(*arg + 1);
5867 }
5868
5869 if (**arg != ']')
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872failret:
5873 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 return FAIL;
5876 }
5877
5878 *arg = skipwhite(*arg + 1);
5879 if (evaluate)
5880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 rettv->v_type = VAR_LIST;
5882 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 ++l->lv_refcount;
5884 }
5885
5886 return OK;
5887}
5888
5889/*
5890 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005891 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005893 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894list_alloc()
5895{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 list_T *l;
5897
5898 l = (list_T *)alloc_clear(sizeof(list_T));
5899 if (l != NULL)
5900 {
5901 /* Prepend the list to the list of lists for garbage collection. */
5902 if (first_list != NULL)
5903 first_list->lv_used_prev = l;
5904 l->lv_used_prev = NULL;
5905 l->lv_used_next = first_list;
5906 first_list = l;
5907 }
5908 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005912 * Allocate an empty list for a return value.
5913 * Returns OK or FAIL.
5914 */
5915 static int
5916rettv_list_alloc(rettv)
5917 typval_T *rettv;
5918{
5919 list_T *l = list_alloc();
5920
5921 if (l == NULL)
5922 return FAIL;
5923
5924 rettv->vval.v_list = l;
5925 rettv->v_type = VAR_LIST;
5926 ++l->lv_refcount;
5927 return OK;
5928}
5929
5930/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 * Unreference a list: decrement the reference count and free it when it
5932 * becomes zero.
5933 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005934 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (l != NULL && --l->lv_refcount <= 0)
5939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940}
5941
5942/*
5943 * Free a list, including all items it points to.
5944 * Ignores the reference count.
5945 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005946 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947list_free(l, recurse)
5948 list_T *l;
5949 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 /* Remove the list from the list of lists for garbage collection. */
5954 if (l->lv_used_prev == NULL)
5955 first_list = l->lv_used_next;
5956 else
5957 l->lv_used_prev->lv_used_next = l->lv_used_next;
5958 if (l->lv_used_next != NULL)
5959 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5960
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it. */
5964 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005965 if (recurse || (item->li_tv.v_type != VAR_LIST
5966 && item->li_tv.v_type != VAR_DICT))
5967 clear_tv(&item->li_tv);
5968 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 }
5970 vim_free(l);
5971}
5972
5973/*
5974 * Allocate a list item.
5975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977listitem_alloc()
5978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980}
5981
5982/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005983 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 vim_free(item);
5991}
5992
5993/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994 * Remove a list item from a List and free it. Also clears the value.
5995 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l;
5999 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000{
6001 list_remove(l, item, item);
6002 listitem_free(item);
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Get the number of items in a list.
6007 */
6008 static long
6009list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 if (l == NULL)
6013 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006014 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * Return TRUE when two lists have exactly the same values.
6019 */
6020 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 list_T *l1;
6023 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006029 if (l1 == NULL || l2 == NULL)
6030 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 if (l1 == l2)
6032 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 if (list_len(l1) != list_len(l2))
6034 return FALSE;
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 for (item1 = l1->lv_first, item2 = l2->lv_first;
6037 item1 != NULL && item2 != NULL;
6038 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return FALSE;
6041 return item1 == NULL && item2 == NULL;
6042}
6043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006044#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6045 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046/*
6047 * Return the dictitem that an entry in a hashtable points to.
6048 */
6049 dictitem_T *
6050dict_lookup(hi)
6051 hashitem_T *hi;
6052{
6053 return HI2DI(hi);
6054}
6055#endif
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 * Return TRUE when two dictionaries have exactly the same key/values.
6059 */
6060 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d1;
6063 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 hashitem_T *hi;
6068 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (d1 == NULL || d2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (d1 == d2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (dict_len(d1) != dict_len(d2))
6076 return FALSE;
6077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006078 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006081 if (!HASHITEM_EMPTY(hi))
6082 {
6083 item2 = dict_find(d2, hi->hi_key, -1);
6084 if (item2 == NULL)
6085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 return FALSE;
6088 --todo;
6089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006090 }
6091 return TRUE;
6092}
6093
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094static int tv_equal_recurse_limit;
6095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 typval_T *tv1;
6104 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 int ic; /* ignore case */
6106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
6108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 * recursiveness to a limit. We guess they are equal then.
6118 * A fixed limit has the problem of still taking an awful long time.
6119 * Reduce the limit every time running into it. That should work fine for
6120 * deeply linked structures that are not recursively linked and catch
6121 * recursiveness quickly. */
6122 if (!recursive)
6123 tv_equal_recurse_limit = 1000;
6124 if (recursive_cnt >= tv_equal_recurse_limit)
6125 {
6126 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 ++recursive_cnt;
6134 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6135 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006136 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_FUNC:
6145 return (tv1->vval.v_string != NULL
6146 && tv2->vval.v_string != NULL
6147 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6148
6149 case VAR_NUMBER:
6150 return tv1->vval.v_number == tv2->vval.v_number;
6151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152#ifdef FEAT_FLOAT
6153 case VAR_FLOAT:
6154 return tv1->vval.v_float == tv2->vval.v_float;
6155#endif
6156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 case VAR_STRING:
6158 s1 = get_tv_string_buf(tv1, buf1);
6159 s2 = get_tv_string_buf(tv2, buf2);
6160 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 return TRUE;
6165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Locate item with index "n" in list "l" and return it.
6169 * A negative index is counted from the end; -1 is the last item.
6170 * Returns NULL when "n" is out of range.
6171 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long n;
6176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 long idx;
6179
6180 if (l == NULL)
6181 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 n = l->lv_len + n;
6186
6187 /* Check for index out of range. */
6188 if (n < 0 || n >= l->lv_len)
6189 return NULL;
6190
6191 /* When there is a cached index may start search from there. */
6192 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_idx / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else if (n > (l->lv_idx + l->lv_len) / 2)
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
6206 else
6207 {
6208 /* closest to the cached index */
6209 item = l->lv_idx_item;
6210 idx = l->lv_idx;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
6213 else
6214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 if (n < l->lv_len / 2)
6216 {
6217 /* closest to the start of the list */
6218 item = l->lv_first;
6219 idx = 0;
6220 }
6221 else
6222 {
6223 /* closest to the end of the list */
6224 item = l->lv_last;
6225 idx = l->lv_len - 1;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228
6229 while (n > idx)
6230 {
6231 /* search forward */
6232 item = item->li_next;
6233 ++idx;
6234 }
6235 while (n < idx)
6236 {
6237 /* search backward */
6238 item = item->li_prev;
6239 --idx;
6240 }
6241
6242 /* cache the used index */
6243 l->lv_idx = idx;
6244 l->lv_idx_item = item;
6245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return item;
6247}
6248
6249/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 * Get list item "l[idx]" as a number.
6251 */
6252 static long
6253list_find_nr(l, idx, errorp)
6254 list_T *l;
6255 long idx;
6256 int *errorp; /* set to TRUE when something wrong */
6257{
6258 listitem_T *li;
6259
6260 li = list_find(l, idx);
6261 if (li == NULL)
6262 {
6263 if (errorp != NULL)
6264 *errorp = TRUE;
6265 return -1L;
6266 }
6267 return get_tv_number_chk(&li->li_tv, errorp);
6268}
6269
6270/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006271 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6272 */
6273 char_u *
6274list_find_str(l, idx)
6275 list_T *l;
6276 long idx;
6277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx - 1);
6281 if (li == NULL)
6282 {
6283 EMSGN(_(e_listidx), idx);
6284 return NULL;
6285 }
6286 return get_tv_string(&li->li_tv);
6287}
6288
6289/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290 * Locate "item" list "l" and return its index.
6291 * Returns -1 when "item" is not in the list.
6292 */
6293 static long
6294list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297{
6298 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006300
6301 if (l == NULL)
6302 return -1;
6303 idx = 0;
6304 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6305 ++idx;
6306 if (li == NULL)
6307 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006308 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Append item "item" to the end of list "l".
6313 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
6319 if (l->lv_last == NULL)
6320 {
6321 /* empty list */
6322 l->lv_first = item;
6323 l->lv_last = item;
6324 item->li_prev = NULL;
6325 }
6326 else
6327 {
6328 l->lv_last->li_next = item;
6329 item->li_prev = l->lv_last;
6330 l->lv_last = item;
6331 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 item->li_next = NULL;
6334}
6335
6336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006340 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l;
6343 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006349 copy_tv(tv, &li->li_tv);
6350 list_append(l, li);
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 * Return FAIL when out of memory.
6357 */
6358 int
6359list_append_dict(list, dict)
6360 list_T *list;
6361 dict_T *dict;
6362{
6363 listitem_T *li = listitem_alloc();
6364
6365 if (li == NULL)
6366 return FAIL;
6367 li->li_tv.v_type = VAR_DICT;
6368 li->li_tv.v_lock = 0;
6369 li->li_tv.vval.v_dict = dict;
6370 list_append(list, li);
6371 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 * Returns FAIL when out of memory.
6379 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 list_T *l;
6383 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385{
6386 listitem_T *li = listitem_alloc();
6387
6388 if (li == NULL)
6389 return FAIL;
6390 list_append(l, li);
6391 li->li_tv.v_type = VAR_STRING;
6392 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 if (str == NULL)
6394 li->li_tv.vval.v_string = NULL;
6395 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006396 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006397 return FAIL;
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006402 * Append "n" to list "l".
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_append_number(l, n)
6407 list_T *l;
6408 varnumber_T n;
6409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l;
6430 typval_T *tv;
6431 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
6443list_insert(l, ni, item)
6444 list_T *l;
6445 listitem_T *ni;
6446 listitem_T *item;
6447{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (item == NULL)
6449 /* Append new item at end of list. */
6450 list_append(l, ni);
6451 else
6452 {
6453 /* Insert new item before existing item. */
6454 ni->li_prev = item->li_prev;
6455 ni->li_next = item;
6456 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 ++l->lv_idx;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 l->lv_idx_item = NULL;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469}
6470
6471/*
6472 * Extend "l1" with "l2".
6473 * If "bef" is NULL append at the end, otherwise insert before this item.
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
6477list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l1;
6479 list_T *l2;
6480 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006485 /* We also quit the loop when we have inserted the original item count of
6486 * the list, avoid a hang when we extend a list with itself. */
6487 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6489 return FAIL;
6490 return OK;
6491}
6492
6493/*
6494 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6495 * Return FAIL when out of memory.
6496 */
6497 static int
6498list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l1;
6500 list_T *l2;
6501 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006505 if (l1 == NULL || l2 == NULL)
6506 return FAIL;
6507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (l == NULL)
6511 return FAIL;
6512 tv->v_type = VAR_LIST;
6513 tv->vval.v_list = l;
6514
6515 /* append all items from the second list */
6516 return list_extend(l, l2, NULL);
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Returns NULL when out of memory.
6524 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *copy;
6532 listitem_T *item;
6533 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 if (orig == NULL)
6536 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 copy = list_alloc();
6539 if (copy != NULL)
6540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (copyID != 0)
6542 {
6543 /* Do this before adding the items, because one of the items may
6544 * refer back to this list. */
6545 orig->lv_copyID = copyID;
6546 orig->lv_copylist = copy;
6547 }
6548 for (item = orig->lv_first; item != NULL && !got_int;
6549 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
6551 ni = listitem_alloc();
6552 if (ni == NULL)
6553 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6557 {
6558 vim_free(ni);
6559 break;
6560 }
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006563 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 list_append(copy, ni);
6565 }
6566 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (item != NULL)
6568 {
6569 list_unref(copy);
6570 copy = NULL;
6571 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 return copy;
6575}
6576
6577/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006581 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006582list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *l;
6584 listitem_T *item;
6585 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 /* notify watchers */
6590 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006592 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 list_fix_watch(l, ip);
6594 if (ip == item2)
6595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597
6598 if (item2->li_next == NULL)
6599 l->lv_last = item->li_prev;
6600 else
6601 item2->li_next->li_prev = item->li_prev;
6602 if (item->li_prev == NULL)
6603 l->lv_first = item2->li_next;
6604 else
6605 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607}
6608
6609/*
6610 * Return an allocated string with the string representation of a list.
6611 * May return NULL.
6612 */
6613 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
6618 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (tv->vval.v_list == NULL)
6621 return NULL;
6622 ga_init2(&ga, (int)sizeof(char), 80);
6623 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 {
6626 vim_free(ga.ga_data);
6627 return NULL;
6628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 ga_append(&ga, ']');
6630 ga_append(&ga, NUL);
6631 return (char_u *)ga.ga_data;
6632}
6633
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006634typedef struct join_S {
6635 char_u *s;
6636 char_u *tofree;
6637} join_T;
6638
6639 static int
6640list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6641 garray_T *gap; /* to store the result in */
6642 list_T *l;
6643 char_u *sep;
6644 int echo_style;
6645 int copyID;
6646 garray_T *join_gap; /* to keep each list item string */
6647{
6648 int i;
6649 join_T *p;
6650 int len;
6651 int sumlen = 0;
6652 int first = TRUE;
6653 char_u *tofree;
6654 char_u numbuf[NUMBUFLEN];
6655 listitem_T *item;
6656 char_u *s;
6657
6658 /* Stringify each item in the list. */
6659 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6660 {
6661 if (echo_style)
6662 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6663 else
6664 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6665 if (s == NULL)
6666 return FAIL;
6667
6668 len = (int)STRLEN(s);
6669 sumlen += len;
6670
6671 ga_grow(join_gap, 1);
6672 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6673 if (tofree != NULL || s != numbuf)
6674 {
6675 p->s = s;
6676 p->tofree = tofree;
6677 }
6678 else
6679 {
6680 p->s = vim_strnsave(s, len);
6681 p->tofree = p->s;
6682 }
6683
6684 line_breakcheck();
6685 }
6686
6687 /* Allocate result buffer with its total size, avoid re-allocation and
6688 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6689 if (join_gap->ga_len >= 2)
6690 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6691 if (ga_grow(gap, sumlen + 2) == FAIL)
6692 return FAIL;
6693
6694 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6695 {
6696 if (first)
6697 first = FALSE;
6698 else
6699 ga_concat(gap, sep);
6700 p = ((join_T *)join_gap->ga_data) + i;
6701
6702 if (p->s != NULL)
6703 ga_concat(gap, p->s);
6704 line_breakcheck();
6705 }
6706
6707 return OK;
6708}
6709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006712 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006713 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006716list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006718 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006720 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006721 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 garray_T join_ga;
6724 int retval;
6725 join_T *p;
6726 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6729 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6730
6731 /* Dispose each item in join_ga. */
6732 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 p = (join_T *)join_ga.ga_data;
6735 for (i = 0; i < join_ga.ga_len; ++i)
6736 {
6737 vim_free(p->tofree);
6738 ++p;
6739 }
6740 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006742
6743 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744}
6745
6746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 * Garbage collection for lists and dictionaries.
6748 *
6749 * We use reference counts to be able to free most items right away when they
6750 * are no longer used. But for composite items it's possible that it becomes
6751 * unused while the reference count is > 0: When there is a recursive
6752 * reference. Example:
6753 * :let l = [1, 2, 3]
6754 * :let d = {9: l}
6755 * :let l[1] = d
6756 *
6757 * Since this is quite unusual we handle this with garbage collection: every
6758 * once in a while find out which lists and dicts are not referenced from any
6759 * variable.
6760 *
6761 * Here is a good reference text about garbage collection (refers to Python
6762 * but it applies to all reference-counting mechanisms):
6763 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 * Do garbage collection for lists and dicts.
6768 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 int
6771garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 buf_T *buf;
6775 win_T *wp;
6776 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006777 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 int did_free;
6779 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006780#ifdef FEAT_WINDOWS
6781 tabpage_T *tp;
6782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006784 /* Only do this once. */
6785 want_garbage_collect = FALSE;
6786 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006787 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006788
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006789 /* We advance by two because we add one for items referenced through
6790 * previous_funccal. */
6791 current_copyID += COPYID_INC;
6792 copyID = current_copyID;
6793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /*
6795 * 1. Go through all accessible variables and mark all lists and dicts
6796 * with copyID.
6797 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798
6799 /* Don't free variables in the previous_funccal list unless they are only
6800 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006801 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006802 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6803 {
6804 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6805 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6806 }
6807
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006808 /* script-local variables */
6809 for (i = 1; i <= ga_scripts.ga_len; ++i)
6810 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6811
6812 /* buffer-local variables */
6813 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006814 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006818 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006819#ifdef FEAT_AUTOCMD
6820 if (aucmd_win != NULL)
6821 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824#ifdef FEAT_WINDOWS
6825 /* tabpage-local variables */
6826 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006827 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828#endif
6829
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 /* global variables */
6831 set_ref_in_ht(&globvarht, copyID);
6832
6833 /* function-local variables */
6834 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6835 {
6836 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6837 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6838 }
6839
Bram Moolenaard812df62008-11-09 12:46:09 +00006840 /* v: vars */
6841 set_ref_in_ht(&vimvarht, copyID);
6842
Bram Moolenaar1dced572012-04-05 16:54:08 +02006843#ifdef FEAT_LUA
6844 set_ref_in_lua(copyID);
6845#endif
6846
Bram Moolenaardb913952012-06-29 12:54:53 +02006847#ifdef FEAT_PYTHON
6848 set_ref_in_python(copyID);
6849#endif
6850
6851#ifdef FEAT_PYTHON3
6852 set_ref_in_python3(copyID);
6853#endif
6854
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006855 /*
6856 * 2. Free lists and dictionaries that are not referenced.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 did_free = free_unref_items(copyID);
6859
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006860 /*
6861 * 3. Check if any funccal can be freed now.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 for (pfc = &previous_funccal; *pfc != NULL; )
6864 {
6865 if (can_free_funccal(*pfc, copyID))
6866 {
6867 fc = *pfc;
6868 *pfc = fc->caller;
6869 free_funccal(fc, TRUE);
6870 did_free = TRUE;
6871 did_free_funccal = TRUE;
6872 }
6873 else
6874 pfc = &(*pfc)->caller;
6875 }
6876 if (did_free_funccal)
6877 /* When a funccal was freed some more items might be garbage
6878 * collected, so run again. */
6879 (void)garbage_collect();
6880
6881 return did_free;
6882}
6883
6884/*
6885 * Free lists and dictionaries that are no longer referenced.
6886 */
6887 static int
6888free_unref_items(copyID)
6889 int copyID;
6890{
6891 dict_T *dd;
6892 list_T *ll;
6893 int did_free = FALSE;
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 */
6898 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006901 /* Free the Dictionary and ordinary items it contains, but don't
6902 * recurse into Lists and Dictionaries, they will be in the list
6903 * of dicts or list of lists. */
6904 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 did_free = TRUE;
6906
6907 /* restart, next dict may also have been freed */
6908 dd = first_dict;
6909 }
6910 else
6911 dd = dd->dv_used_next;
6912
6913 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 * Go through the list of lists and free items without the copyID.
6915 * But don't free a list that has a watcher (used in a for loop), these
6916 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 */
6918 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006919 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6920 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006922 /* Free the List and ordinary items it contains, but don't recurse
6923 * into Lists and Dictionaries, they will be in the list of dicts
6924 * or list of lists. */
6925 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 did_free = TRUE;
6927
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006928 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 ll = first_list;
6930 }
6931 else
6932 ll = ll->lv_used_next;
6933
6934 return did_free;
6935}
6936
6937/*
6938 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6939 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006940 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941set_ref_in_ht(ht, copyID)
6942 hashtab_T *ht;
6943 int copyID;
6944{
6945 int todo;
6946 hashitem_T *hi;
6947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006948 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 for (hi = ht->ht_array; todo > 0; ++hi)
6950 if (!HASHITEM_EMPTY(hi))
6951 {
6952 --todo;
6953 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6954 }
6955}
6956
6957/*
6958 * Mark all lists and dicts referenced through list "l" with "copyID".
6959 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006960 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961set_ref_in_list(l, copyID)
6962 list_T *l;
6963 int copyID;
6964{
6965 listitem_T *li;
6966
6967 for (li = l->lv_first; li != NULL; li = li->li_next)
6968 set_ref_in_item(&li->li_tv, copyID);
6969}
6970
6971/*
6972 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006974 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975set_ref_in_item(tv, copyID)
6976 typval_T *tv;
6977 int copyID;
6978{
6979 dict_T *dd;
6980 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981
6982 switch (tv->v_type)
6983 {
6984 case VAR_DICT:
6985 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this dict yet. */
6989 dd->dv_copyID = copyID;
6990 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993
6994 case VAR_LIST:
6995 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006996 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 /* Didn't see this list yet. */
6999 ll->lv_copyID = copyID;
7000 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005}
7006
7007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 * Allocate an empty header for a dictionary.
7009 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007010 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011dict_alloc()
7012{
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007017 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007018 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 if (first_dict != NULL)
7020 first_dict->dv_used_prev = d;
7021 d->dv_used_next = first_dict;
7022 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007027 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007031 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032}
7033
7034/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007035 * Allocate an empty dict for a return value.
7036 * Returns OK or FAIL.
7037 */
7038 static int
7039rettv_dict_alloc(rettv)
7040 typval_T *rettv;
7041{
7042 dict_T *d = dict_alloc();
7043
7044 if (d == NULL)
7045 return FAIL;
7046
7047 rettv->vval.v_dict = d;
7048 rettv->v_type = VAR_DICT;
7049 ++d->dv_refcount;
7050 return OK;
7051}
7052
7053
7054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 * Unreference a Dictionary: decrement the reference count and free it when it
7056 * becomes zero.
7057 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007058 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007062 if (d != NULL && --d->dv_refcount <= 0)
7063 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064}
7065
7066/*
7067 * Free a Dictionary, including all items it contains.
7068 * Ignores the reference count.
7069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007070 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007071dict_free(d, recurse)
7072 dict_T *d;
7073 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079 /* Remove the dict from the list of dicts for garbage collection. */
7080 if (d->dv_used_prev == NULL)
7081 first_dict = d->dv_used_next;
7082 else
7083 d->dv_used_prev->dv_used_next = d->dv_used_next;
7084 if (d->dv_used_next != NULL)
7085 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7086
7087 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007088 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007089 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (!HASHITEM_EMPTY(hi))
7093 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007094 /* Remove the item before deleting it, just in case there is
7095 * something recursive causing trouble. */
7096 di = HI2DI(hi);
7097 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007098 if (recurse || (di->di_tv.v_type != VAR_LIST
7099 && di->di_tv.v_type != VAR_DICT))
7100 clear_tv(&di->di_tv);
7101 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102 --todo;
7103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 vim_free(d);
7107}
7108
7109/*
7110 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 * The "key" is copied to the new item.
7112 * Note that the value of the item "di_tv" still needs to be initialized!
7113 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007115 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116dictitem_alloc(key)
7117 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118{
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007121 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 di->di_flags = 0;
7126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 * Make a copy of a Dictionary item.
7132 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136{
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007139 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7140 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 if (di != NULL)
7142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007144 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 copy_tv(&org->di_tv, &di->di_tv);
7146 }
7147 return di;
7148}
7149
7150/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 * Remove item "item" from Dictionary "dict" and free it.
7152 */
7153 static void
7154dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 dict_T *dict;
7156 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157{
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 if (HASHITEM_EMPTY(hi))
7162 EMSG2(_(e_intern2), "dictitem_remove()");
7163 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 dictitem_free(item);
7166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Free a dict item. Also clears the value.
7170 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007171 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 clear_tv(&item->di_tv);
7176 vim_free(item);
7177}
7178
7179/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7181 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 * Returns NULL when out of memory.
7184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007188 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 dict_T *copy;
7192 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195
7196 if (orig == NULL)
7197 return NULL;
7198
7199 copy = dict_alloc();
7200 if (copy != NULL)
7201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007202 if (copyID != 0)
7203 {
7204 orig->dv_copyID = copyID;
7205 orig->dv_copydict = copy;
7206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007207 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 --todo;
7213
7214 di = dictitem_alloc(hi->hi_key);
7215 if (di == NULL)
7216 break;
7217 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 {
7219 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7220 copyID) == FAIL)
7221 {
7222 vim_free(di);
7223 break;
7224 }
7225 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 else
7227 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7228 if (dict_add(copy, di) == FAIL)
7229 {
7230 dictitem_free(di);
7231 break;
7232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 if (todo > 0)
7238 {
7239 dict_unref(copy);
7240 copy = NULL;
7241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 }
7243
7244 return copy;
7245}
7246
7247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007249 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007251 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 dict_T *d;
7254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255{
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257}
7258
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007260 * Add a number or string entry to dictionary "d".
7261 * When "str" is NULL use number "nr", otherwise use "str".
7262 * Returns FAIL when out of memory and when key already exists.
7263 */
7264 int
7265dict_add_nr_str(d, key, nr, str)
7266 dict_T *d;
7267 char *key;
7268 long nr;
7269 char_u *str;
7270{
7271 dictitem_T *item;
7272
7273 item = dictitem_alloc((char_u *)key);
7274 if (item == NULL)
7275 return FAIL;
7276 item->di_tv.v_lock = 0;
7277 if (str == NULL)
7278 {
7279 item->di_tv.v_type = VAR_NUMBER;
7280 item->di_tv.vval.v_number = nr;
7281 }
7282 else
7283 {
7284 item->di_tv.v_type = VAR_STRING;
7285 item->di_tv.vval.v_string = vim_strsave(str);
7286 }
7287 if (dict_add(d, item) == FAIL)
7288 {
7289 dictitem_free(item);
7290 return FAIL;
7291 }
7292 return OK;
7293}
7294
7295/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007296 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007297 * Returns FAIL when out of memory and when key already exists.
7298 */
7299 int
7300dict_add_list(d, key, list)
7301 dict_T *d;
7302 char *key;
7303 list_T *list;
7304{
7305 dictitem_T *item;
7306
7307 item = dictitem_alloc((char_u *)key);
7308 if (item == NULL)
7309 return FAIL;
7310 item->di_tv.v_lock = 0;
7311 item->di_tv.v_type = VAR_LIST;
7312 item->di_tv.vval.v_list = list;
7313 if (dict_add(d, item) == FAIL)
7314 {
7315 dictitem_free(item);
7316 return FAIL;
7317 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007318 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007319 return OK;
7320}
7321
7322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Get the number of items in a Dictionary.
7324 */
7325 static long
7326dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (d == NULL)
7330 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007331 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332}
7333
7334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 * Find item "key[len]" in Dictionary "d".
7336 * If "len" is negative use strlen(key).
7337 * Returns NULL when not found.
7338 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007339 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 char_u *key;
7343 int len;
7344{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345#define AKEYLEN 200
7346 char_u buf[AKEYLEN];
7347 char_u *akey;
7348 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 if (len < 0)
7352 akey = key;
7353 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 tofree = akey = vim_strnsave(key, len);
7356 if (akey == NULL)
7357 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 else
7360 {
7361 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007362 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 akey = buf;
7364 }
7365
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 vim_free(tofree);
7368 if (HASHITEM_EMPTY(hi))
7369 return NULL;
7370 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371}
7372
7373/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007374 * Get a string item from a dictionary.
7375 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376 * Returns NULL if the entry doesn't exist or out of memory.
7377 */
7378 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007379get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007380 dict_T *d;
7381 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007383{
7384 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007386
7387 di = dict_find(d, key, -1);
7388 if (di == NULL)
7389 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007390 s = get_tv_string(&di->di_tv);
7391 if (save && s != NULL)
7392 s = vim_strsave(s);
7393 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007394}
7395
7396/*
7397 * Get a number item from a dictionary.
7398 * Returns 0 if the entry doesn't exist or out of memory.
7399 */
7400 long
7401get_dict_number(d, key)
7402 dict_T *d;
7403 char_u *key;
7404{
7405 dictitem_T *di;
7406
7407 di = dict_find(d, key, -1);
7408 if (di == NULL)
7409 return 0;
7410 return get_tv_number(&di->di_tv);
7411}
7412
7413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 * Return an allocated string with the string representation of a Dictionary.
7415 * May return NULL.
7416 */
7417 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007418dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421{
7422 garray_T ga;
7423 int first = TRUE;
7424 char_u *tofree;
7425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 return NULL;
7433 ga_init2(&ga, (int)sizeof(char), 80);
7434 ga_append(&ga, '{');
7435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007436 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 --todo;
7442
7443 if (first)
7444 first = FALSE;
7445 else
7446 ga_concat(&ga, (char_u *)", ");
7447
7448 tofree = string_quote(hi->hi_key, FALSE);
7449 if (tofree != NULL)
7450 {
7451 ga_concat(&ga, tofree);
7452 vim_free(tofree);
7453 }
7454 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 if (s != NULL)
7457 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 if (s == NULL)
7460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007463 if (todo > 0)
7464 {
7465 vim_free(ga.ga_data);
7466 return NULL;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 ga_append(&ga, '}');
7470 ga_append(&ga, NUL);
7471 return (char_u *)ga.ga_data;
7472}
7473
7474/*
7475 * Allocate a variable for a Dictionary and fill it from "*arg".
7476 * Return OK or FAIL. Returns NOTDONE for {expr}.
7477 */
7478 static int
7479get_dict_tv(arg, rettv, evaluate)
7480 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 int evaluate;
7483{
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dict_T *d = NULL;
7485 typval_T tvkey;
7486 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007487 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491
7492 /*
7493 * First check if it's not a curly-braces thing: {expr}.
7494 * Must do this without evaluating, otherwise a function may be called
7495 * twice. Unfortunately this means we need to call eval1() twice for the
7496 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 if (*start != '}')
7500 {
7501 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7502 return FAIL;
7503 if (*start == '}')
7504 return NOTDONE;
7505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
7507 if (evaluate)
7508 {
7509 d = dict_alloc();
7510 if (d == NULL)
7511 return FAIL;
7512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 tvkey.v_type = VAR_UNKNOWN;
7514 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
7516 *arg = skipwhite(*arg + 1);
7517 while (**arg != '}' && **arg != NUL)
7518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 goto failret;
7521 if (**arg != ':')
7522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007523 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 goto failret;
7526 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007527 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 key = get_tv_string_buf_chk(&tvkey, buf);
7530 if (key == NULL || *key == NUL)
7531 {
7532 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7533 if (key != NULL)
7534 EMSG(_(e_emptykey));
7535 clear_tv(&tvkey);
7536 goto failret;
7537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539
7540 *arg = skipwhite(*arg + 1);
7541 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7542 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007543 if (evaluate)
7544 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 if (evaluate)
7548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 if (item != NULL)
7551 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007552 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 clear_tv(&tv);
7555 goto failret;
7556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 item = dictitem_alloc(key);
7558 clear_tv(&tvkey);
7559 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 if (dict_add(d, item) == FAIL)
7564 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 }
7566 }
7567
7568 if (**arg == '}')
7569 break;
7570 if (**arg != ',')
7571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007572 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 goto failret;
7574 }
7575 *arg = skipwhite(*arg + 1);
7576 }
7577
7578 if (**arg != '}')
7579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581failret:
7582 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007583 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 return FAIL;
7585 }
7586
7587 *arg = skipwhite(*arg + 1);
7588 if (evaluate)
7589 {
7590 rettv->v_type = VAR_DICT;
7591 rettv->vval.v_dict = d;
7592 ++d->dv_refcount;
7593 }
7594
7595 return OK;
7596}
7597
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 * Return a string with the string representation of a variable.
7600 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007601 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007602 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007604 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 */
7606 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007610 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 static int recurse = 0;
7614 char_u *r = NULL;
7615
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007618 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 *tofree = NULL;
7620 return NULL;
7621 }
7622 ++recurse;
7623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 switch (tv->v_type)
7625 {
7626 case VAR_FUNC:
7627 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 r = tv->vval.v_string;
7629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632 if (tv->vval.v_list == NULL)
7633 {
7634 *tofree = NULL;
7635 r = NULL;
7636 }
7637 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7638 {
7639 *tofree = NULL;
7640 r = (char_u *)"[...]";
7641 }
7642 else
7643 {
7644 tv->vval.v_list->lv_copyID = copyID;
7645 *tofree = list2string(tv, copyID);
7646 r = *tofree;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 if (tv->vval.v_dict == NULL)
7652 {
7653 *tofree = NULL;
7654 r = NULL;
7655 }
7656 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7657 {
7658 *tofree = NULL;
7659 r = (char_u *)"{...}";
7660 }
7661 else
7662 {
7663 tv->vval.v_dict->dv_copyID = copyID;
7664 *tofree = dict2string(tv, copyID);
7665 r = *tofree;
7666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669 case VAR_STRING:
7670 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 *tofree = NULL;
7672 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 case VAR_FLOAT:
7677 *tofree = NULL;
7678 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7679 r = numbuf;
7680 break;
7681#endif
7682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687
7688 --recurse;
7689 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690}
7691
7692/*
7693 * Return a string with the string representation of a variable.
7694 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7695 * "numbuf" is used for a number.
7696 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007697 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 */
7699 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 char_u **tofree;
7703 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007704 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705{
7706 switch (tv->v_type)
7707 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708 case VAR_FUNC:
7709 *tofree = string_quote(tv->vval.v_string, TRUE);
7710 return *tofree;
7711 case VAR_STRING:
7712 *tofree = string_quote(tv->vval.v_string, FALSE);
7713 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714#ifdef FEAT_FLOAT
7715 case VAR_FLOAT:
7716 *tofree = NULL;
7717 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7718 return numbuf;
7719#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728}
7729
7730/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 * Return string "str" in ' quotes, doubling ' characters.
7732 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007734 */
7735 static char_u *
7736string_quote(str, function)
7737 char_u *str;
7738 int function;
7739{
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 char_u *p, *r, *s;
7742
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 len = (function ? 13 : 3);
7744 if (str != NULL)
7745 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007746 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 for (p = str; *p != NUL; mb_ptr_adv(p))
7748 if (*p == '\'')
7749 ++len;
7750 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007751 s = r = alloc(len);
7752 if (r != NULL)
7753 {
7754 if (function)
7755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757 r += 10;
7758 }
7759 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 if (str != NULL)
7762 for (p = str; *p != NUL; )
7763 {
7764 if (*p == '\'')
7765 *r++ = '\'';
7766 MB_COPY_CHAR(p, r);
7767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 if (function)
7770 *r++ = ')';
7771 *r++ = NUL;
7772 }
7773 return s;
7774}
7775
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
7777/*
7778 * Convert the string "text" to a floating point number.
7779 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7780 * this always uses a decimal point.
7781 * Returns the length of the text that was consumed.
7782 */
7783 static int
7784string2float(text, value)
7785 char_u *text;
7786 float_T *value; /* result stored here */
7787{
7788 char *s = (char *)text;
7789 float_T f;
7790
7791 f = strtod(s, &s);
7792 *value = f;
7793 return (int)((char_u *)s - text);
7794}
7795#endif
7796
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * Get the value of an environment variable.
7799 * "arg" is pointing to the '$'. It is advanced to after the name.
7800 * If the environment variable was not set, silently assume it is empty.
7801 * Always return OK.
7802 */
7803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int evaluate;
7808{
7809 char_u *string = NULL;
7810 int len;
7811 int cc;
7812 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007813 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815 ++*arg;
7816 name = *arg;
7817 len = get_env_len(arg);
7818 if (evaluate)
7819 {
7820 if (len != 0)
7821 {
7822 cc = name[len];
7823 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824 /* first try vim_getenv(), fast for normal environment vars */
7825 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007827 {
7828 if (!mustfree)
7829 string = vim_strsave(string);
7830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 else
7832 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007833 if (mustfree)
7834 vim_free(string);
7835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 /* next try expanding things like $VIM and ${HOME} */
7837 string = expand_env_save(name - 1);
7838 if (string != NULL && *string == '$')
7839 {
7840 vim_free(string);
7841 string = NULL;
7842 }
7843 }
7844 name[len] = cc;
7845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849
7850 return OK;
7851}
7852
7853/*
7854 * Array with names and number of arguments of all internal functions
7855 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7856 */
7857static struct fst
7858{
7859 char *f_name; /* function name */
7860 char f_min_argc; /* minimal number of arguments */
7861 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007863 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864} functions[] =
7865{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
7867 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007868 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007870 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007871 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"append", 2, 2, f_append},
7873 {"argc", 0, 0, f_argc},
7874 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007875 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007877 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007879 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"bufexists", 1, 1, f_bufexists},
7884 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7885 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7886 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7887 {"buflisted", 1, 1, f_buflisted},
7888 {"bufloaded", 1, 1, f_bufloaded},
7889 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007890 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"bufwinnr", 1, 1, f_bufwinnr},
7892 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007893 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007894 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896#ifdef FEAT_FLOAT
7897 {"ceil", 1, 1, f_ceil},
7898#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007899 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007900 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007902 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007904#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007905 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007906 {"complete_add", 1, 1, f_complete_add},
7907 {"complete_check", 0, 0, f_complete_check},
7908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#ifdef FEAT_FLOAT
7912 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007913 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007917 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007918 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"delete", 1, 1, f_delete},
7920 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007921 {"diff_filler", 1, 1, f_diff_filler},
7922 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007923 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007925 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"eventhandler", 0, 0, f_eventhandler},
7927 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007928 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930#ifdef FEAT_FLOAT
7931 {"exp", 1, 1, f_exp},
7932#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007933 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007934 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007935 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7937 {"filereadable", 1, 1, f_filereadable},
7938 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007940 {"finddir", 1, 3, f_finddir},
7941 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"float2nr", 1, 1, f_float2nr},
7944 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007945 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007947 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"fnamemodify", 2, 2, f_fnamemodify},
7949 {"foldclosed", 1, 1, f_foldclosed},
7950 {"foldclosedend", 1, 1, f_foldclosedend},
7951 {"foldlevel", 1, 1, f_foldlevel},
7952 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007953 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007956 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007957 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007958 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007959 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"getchar", 0, 1, f_getchar},
7961 {"getcharmod", 0, 0, f_getcharmod},
7962 {"getcmdline", 0, 0, f_getcmdline},
7963 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007964 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007966 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007967 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getfsize", 1, 1, f_getfsize},
7969 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007971 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007972 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007973 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007974 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007975 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007976 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007977 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007979 {"gettabvar", 2, 3, f_gettabvar},
7980 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getwinposx", 0, 0, f_getwinposx},
7982 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007983 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007984 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007985 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007988 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007989 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7991 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7992 {"histadd", 2, 2, f_histadd},
7993 {"histdel", 1, 2, f_histdel},
7994 {"histget", 1, 2, f_histget},
7995 {"histnr", 1, 1, f_histnr},
7996 {"hlID", 1, 1, f_hlID},
7997 {"hlexists", 1, 1, f_hlexists},
7998 {"hostname", 0, 0, f_hostname},
7999 {"iconv", 3, 3, f_iconv},
8000 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008002 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008004 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"inputrestore", 0, 0, f_inputrestore},
8006 {"inputsave", 0, 0, f_inputsave},
8007 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008011 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008016 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"libcall", 3, 3, f_libcall},
8018 {"libcallnr", 3, 3, f_libcallnr},
8019 {"line", 1, 1, f_line},
8020 {"line2byte", 1, 1, f_line2byte},
8021 {"lispindent", 1, 1, f_lispindent},
8022 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008024 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025 {"log10", 1, 1, f_log10},
8026#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008027#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008028 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008029#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008031 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008032 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008034 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008035 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008036 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008037 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008038 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008039 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008040 {"max", 1, 1, f_max},
8041 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008042#ifdef vim_mkdir
8043 {"mkdir", 1, 3, f_mkdir},
8044#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008046#ifdef FEAT_MZSCHEME
8047 {"mzeval", 1, 1, f_mzeval},
8048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008050 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008051 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008052 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
8054 {"pow", 2, 2, f_pow},
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008057 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008058 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008059#ifdef FEAT_PYTHON3
8060 {"py3eval", 1, 1, f_py3eval},
8061#endif
8062#ifdef FEAT_PYTHON
8063 {"pyeval", 1, 1, f_pyeval},
8064#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008066 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008067 {"reltime", 0, 2, f_reltime},
8068 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"remote_expr", 2, 3, f_remote_expr},
8070 {"remote_foreground", 1, 1, f_remote_foreground},
8071 {"remote_peek", 1, 2, f_remote_peek},
8072 {"remote_read", 1, 1, f_remote_read},
8073 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008076 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008078 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"round", 1, 1, f_round},
8081#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008082 {"screenattr", 2, 2, f_screenattr},
8083 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008084 {"screencol", 0, 0, f_screencol},
8085 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008086 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008087 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008088 {"searchpair", 3, 7, f_searchpair},
8089 {"searchpairpos", 3, 7, f_searchpairpos},
8090 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"server2client", 2, 2, f_server2client},
8092 {"serverlist", 0, 0, f_serverlist},
8093 {"setbufvar", 3, 3, f_setbufvar},
8094 {"setcmdpos", 1, 1, f_setcmdpos},
8095 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008096 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008097 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008098 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008099 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008101 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008102 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008104#ifdef FEAT_CRYPT
8105 {"sha256", 1, 1, f_sha256},
8106#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008107 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008108 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110#ifdef FEAT_FLOAT
8111 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008112 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008114 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008115 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008116 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008117 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008118 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#ifdef FEAT_FLOAT
8120 {"sqrt", 1, 1, f_sqrt},
8121 {"str2float", 1, 1, f_str2float},
8122#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008123 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008125 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#ifdef HAVE_STRFTIME
8127 {"strftime", 1, 2, f_strftime},
8128#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"strlen", 1, 1, f_strlen},
8132 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008133 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008135 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008136 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"substitute", 4, 4, f_substitute},
8138 {"synID", 3, 3, f_synID},
8139 {"synIDattr", 2, 3, f_synIDattr},
8140 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008141 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008142 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008143 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008144 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008145 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008146 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008147 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008148 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008149 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150#ifdef FEAT_FLOAT
8151 {"tan", 1, 1, f_tan},
8152 {"tanh", 1, 1, f_tanh},
8153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008155 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"tolower", 1, 1, f_tolower},
8157 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008158 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159#ifdef FEAT_FLOAT
8160 {"trunc", 1, 1, f_trunc},
8161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008163 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008164 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008165 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008166 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"virtcol", 1, 1, f_virtcol},
8168 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008169 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"winbufnr", 1, 1, f_winbufnr},
8171 {"wincol", 0, 0, f_wincol},
8172 {"winheight", 1, 1, f_winheight},
8173 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008174 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008176 {"winrestview", 1, 1, f_winrestview},
8177 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008179 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008180 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181};
8182
8183#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8184
8185/*
8186 * Function given to ExpandGeneric() to obtain the list of internal
8187 * or user defined function names.
8188 */
8189 char_u *
8190get_function_name(xp, idx)
8191 expand_T *xp;
8192 int idx;
8193{
8194 static int intidx = -1;
8195 char_u *name;
8196
8197 if (idx == 0)
8198 intidx = -1;
8199 if (intidx < 0)
8200 {
8201 name = get_user_func_name(xp, idx);
8202 if (name != NULL)
8203 return name;
8204 }
8205 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8206 {
8207 STRCPY(IObuff, functions[intidx].f_name);
8208 STRCAT(IObuff, "(");
8209 if (functions[intidx].f_max_argc == 0)
8210 STRCAT(IObuff, ")");
8211 return IObuff;
8212 }
8213
8214 return NULL;
8215}
8216
8217/*
8218 * Function given to ExpandGeneric() to obtain the list of internal or
8219 * user defined variable or function names.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 char_u *
8222get_expr_name(xp, idx)
8223 expand_T *xp;
8224 int idx;
8225{
8226 static int intidx = -1;
8227 char_u *name;
8228
8229 if (idx == 0)
8230 intidx = -1;
8231 if (intidx < 0)
8232 {
8233 name = get_function_name(xp, idx);
8234 if (name != NULL)
8235 return name;
8236 }
8237 return get_user_var_name(xp, ++intidx);
8238}
8239
8240#endif /* FEAT_CMDL_COMPL */
8241
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008242#if defined(EBCDIC) || defined(PROTO)
8243/*
8244 * Compare struct fst by function name.
8245 */
8246 static int
8247compare_func_name(s1, s2)
8248 const void *s1;
8249 const void *s2;
8250{
8251 struct fst *p1 = (struct fst *)s1;
8252 struct fst *p2 = (struct fst *)s2;
8253
8254 return STRCMP(p1->f_name, p2->f_name);
8255}
8256
8257/*
8258 * Sort the function table by function name.
8259 * The sorting of the table above is ASCII dependant.
8260 * On machines using EBCDIC we have to sort it.
8261 */
8262 static void
8263sortFunctions()
8264{
8265 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8266
8267 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8268}
8269#endif
8270
8271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272/*
8273 * Find internal function in table above.
8274 * Return index, or -1 if not found
8275 */
8276 static int
8277find_internal_func(name)
8278 char_u *name; /* name of the function */
8279{
8280 int first = 0;
8281 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8282 int cmp;
8283 int x;
8284
8285 /*
8286 * Find the function name in the table. Binary search.
8287 */
8288 while (first <= last)
8289 {
8290 x = first + ((unsigned)(last - first) >> 1);
8291 cmp = STRCMP(name, functions[x].f_name);
8292 if (cmp < 0)
8293 last = x - 1;
8294 else if (cmp > 0)
8295 first = x + 1;
8296 else
8297 return x;
8298 }
8299 return -1;
8300}
8301
8302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8304 * name it contains, otherwise return "name".
8305 */
8306 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008307deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 char_u *name;
8309 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311{
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 int cc;
8314
8315 cc = name[*lenp];
8316 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008317 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 {
8323 *lenp = 0;
8324 return (char_u *)""; /* just in case */
8325 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008326 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 }
8329
8330 return name;
8331}
8332
8333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * Allocate a variable for the result of a function.
8335 * Return OK or FAIL.
8336 */
8337 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8339 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 char_u *name; /* name of the function */
8341 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u **arg; /* argument, pointing to the '(' */
8344 linenr_T firstline; /* first line of range */
8345 linenr_T lastline; /* last line of range */
8346 int *doesrange; /* return: function handled range */
8347 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 char_u *argp;
8351 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008352 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int argcount = 0; /* number of arguments found */
8354
8355 /*
8356 * Get the arguments.
8357 */
8358 argp = *arg;
8359 while (argcount < MAX_FUNC_ARGS)
8360 {
8361 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8362 if (*argp == ')' || *argp == ',' || *argp == NUL)
8363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8365 {
8366 ret = FAIL;
8367 break;
8368 }
8369 ++argcount;
8370 if (*argp != ',')
8371 break;
8372 }
8373 if (*argp == ')')
8374 ++argp;
8375 else
8376 ret = FAIL;
8377
8378 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 {
8383 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008384 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008386 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 *arg = skipwhite(argp);
8393 return ret;
8394}
8395
8396
8397/*
8398 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008399 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008400 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 */
8402 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008403call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008404 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008405 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008409 typval_T *argvars; /* vars for arguments, must have "argcount"
8410 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 linenr_T firstline; /* first line of range */
8412 linenr_T lastline; /* last line of range */
8413 int *doesrange; /* return: function handled range */
8414 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#define ERROR_UNKNOWN 0
8419#define ERROR_TOOMANY 1
8420#define ERROR_TOOFEW 2
8421#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422#define ERROR_DICT 4
8423#define ERROR_NONE 5
8424#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 int error = ERROR_NONE;
8426 int i;
8427 int llen;
8428 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429#define FLEN_FIXED 40
8430 char_u fname_buf[FLEN_FIXED + 1];
8431 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008432 char_u *name;
8433
8434 /* Make a copy of the name, if it comes from a funcref variable it could
8435 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008436 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008437 if (name == NULL)
8438 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
8440 /*
8441 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8442 * Change <SNR>123_name() to K_SNR 123_name().
8443 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 llen = eval_fname_script(name);
8446 if (llen > 0)
8447 {
8448 fname_buf[0] = K_SPECIAL;
8449 fname_buf[1] = KS_EXTRA;
8450 fname_buf[2] = (int)KE_SNR;
8451 i = 3;
8452 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8453 {
8454 if (current_SID <= 0)
8455 error = ERROR_SCRIPT;
8456 else
8457 {
8458 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8459 i = (int)STRLEN(fname_buf);
8460 }
8461 }
8462 if (i + STRLEN(name + llen) < FLEN_FIXED)
8463 {
8464 STRCPY(fname_buf + i, name + llen);
8465 fname = fname_buf;
8466 }
8467 else
8468 {
8469 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8470 if (fname == NULL)
8471 error = ERROR_OTHER;
8472 else
8473 {
8474 mch_memmove(fname, fname_buf, (size_t)i);
8475 STRCPY(fname + i, name + llen);
8476 }
8477 }
8478 }
8479 else
8480 fname = name;
8481
8482 *doesrange = FALSE;
8483
8484
8485 /* execute the function if no errors detected and executing */
8486 if (evaluate && error == ERROR_NONE)
8487 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008488 char_u *rfname = fname;
8489
8490 /* Ignore "g:" before a function name. */
8491 if (fname[0] == 'g' && fname[1] == ':')
8492 rfname = fname + 2;
8493
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008494 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8495 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 error = ERROR_UNKNOWN;
8497
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008498 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {
8500 /*
8501 * User defined function.
8502 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008503 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008506 /* Trigger FuncUndefined event, may load the function. */
8507 if (fp == NULL
8508 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008509 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008510 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008512 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008513 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 }
8515#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008516 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008517 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008518 {
8519 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008520 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008521 }
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (fp != NULL)
8524 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008525 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008527 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008529 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008531 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 else
8534 {
8535 /*
8536 * Call the user function.
8537 * Save and restore search patterns, script variables and
8538 * redo buffer.
8539 */
8540 save_search_patterns();
8541 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008542 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008543 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008544 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008545 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8546 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8547 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008548 /* Function was unreferenced while being used, free it
8549 * now. */
8550 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 restoreRedobuff();
8552 restore_search_patterns();
8553 error = ERROR_NONE;
8554 }
8555 }
8556 }
8557 else
8558 {
8559 /*
8560 * Find the function name in the table, call its implementation.
8561 */
8562 i = find_internal_func(fname);
8563 if (i >= 0)
8564 {
8565 if (argcount < functions[i].f_min_argc)
8566 error = ERROR_TOOFEW;
8567 else if (argcount > functions[i].f_max_argc)
8568 error = ERROR_TOOMANY;
8569 else
8570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008571 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 error = ERROR_NONE;
8574 }
8575 }
8576 }
8577 /*
8578 * The function call (or "FuncUndefined" autocommand sequence) might
8579 * have been aborted by an error, an interrupt, or an explicitly thrown
8580 * exception that has not been caught so far. This situation can be
8581 * tested for by calling aborting(). For an error in an internal
8582 * function or for the "E132" error in call_user_func(), however, the
8583 * throw point at which the "force_abort" flag (temporarily reset by
8584 * emsg()) is normally updated has not been reached yet. We need to
8585 * update that flag first to make aborting() reliable.
8586 */
8587 update_force_abort();
8588 }
8589 if (error == ERROR_NONE)
8590 ret = OK;
8591
8592 /*
8593 * Report an error unless the argument evaluation or function call has been
8594 * cancelled due to an aborting error, an interrupt, or an exception.
8595 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008596 if (!aborting())
8597 {
8598 switch (error)
8599 {
8600 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008601 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008602 break;
8603 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008604 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008605 break;
8606 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008607 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008608 name);
8609 break;
8610 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008611 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008612 name);
8613 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008614 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008615 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616 name);
8617 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008618 }
8619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 if (fname != name && fname != fname_buf)
8622 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008623 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 return ret;
8626}
8627
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008628/*
8629 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008630 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008631 */
8632 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008633emsg_funcname(ermsg, name)
8634 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008635 char_u *name;
8636{
8637 char_u *p;
8638
8639 if (*name == K_SPECIAL)
8640 p = concat_str((char_u *)"<SNR>", name + 3);
8641 else
8642 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008643 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008644 if (p != name)
8645 vim_free(p);
8646}
8647
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008648/*
8649 * Return TRUE for a non-zero Number and a non-empty String.
8650 */
8651 static int
8652non_zero_arg(argvars)
8653 typval_T *argvars;
8654{
8655 return ((argvars[0].v_type == VAR_NUMBER
8656 && argvars[0].vval.v_number != 0)
8657 || (argvars[0].v_type == VAR_STRING
8658 && argvars[0].vval.v_string != NULL
8659 && *argvars[0].vval.v_string != NUL));
8660}
8661
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662/*********************************************
8663 * Implementation of the built-in functions
8664 */
8665
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008666#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008667static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8668
8669/*
8670 * Get the float value of "argvars[0]" into "f".
8671 * Returns FAIL when the argument is not a Number or Float.
8672 */
8673 static int
8674get_float_arg(argvars, f)
8675 typval_T *argvars;
8676 float_T *f;
8677{
8678 if (argvars[0].v_type == VAR_FLOAT)
8679 {
8680 *f = argvars[0].vval.v_float;
8681 return OK;
8682 }
8683 if (argvars[0].v_type == VAR_NUMBER)
8684 {
8685 *f = (float_T)argvars[0].vval.v_number;
8686 return OK;
8687 }
8688 EMSG(_("E808: Number or Float required"));
8689 return FAIL;
8690}
8691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692/*
8693 * "abs(expr)" function
8694 */
8695 static void
8696f_abs(argvars, rettv)
8697 typval_T *argvars;
8698 typval_T *rettv;
8699{
8700 if (argvars[0].v_type == VAR_FLOAT)
8701 {
8702 rettv->v_type = VAR_FLOAT;
8703 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8704 }
8705 else
8706 {
8707 varnumber_T n;
8708 int error = FALSE;
8709
8710 n = get_tv_number_chk(&argvars[0], &error);
8711 if (error)
8712 rettv->vval.v_number = -1;
8713 else if (n > 0)
8714 rettv->vval.v_number = n;
8715 else
8716 rettv->vval.v_number = -n;
8717 }
8718}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008719
8720/*
8721 * "acos()" function
8722 */
8723 static void
8724f_acos(argvars, rettv)
8725 typval_T *argvars;
8726 typval_T *rettv;
8727{
8728 float_T f;
8729
8730 rettv->v_type = VAR_FLOAT;
8731 if (get_float_arg(argvars, &f) == OK)
8732 rettv->vval.v_float = acos(f);
8733 else
8734 rettv->vval.v_float = 0.0;
8735}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008736#endif
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 */
8741 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008743 typval_T *argvars;
8744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008751 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008752 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008753 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008755 }
8756 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757 EMSG(_(e_listreq));
8758}
8759
8760/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008761 * "and(expr, expr)" function
8762 */
8763 static void
8764f_and(argvars, rettv)
8765 typval_T *argvars;
8766 typval_T *rettv;
8767{
8768 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8769 & get_tv_number_chk(&argvars[1], NULL);
8770}
8771
8772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773 * "append(lnum, string/list)" function
8774 */
8775 static void
8776f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 typval_T *argvars;
8778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008779{
8780 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 list_T *l = NULL;
8783 listitem_T *li = NULL;
8784 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008785 long added = 0;
8786
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008787 /* When coming here from Insert mode, sync undo, so that this can be
8788 * undone separately from what was previously inserted. */
8789 if (u_sync_once == 2)
8790 {
8791 u_sync_once = 1; /* notify that u_sync() was called */
8792 u_sync(TRUE);
8793 }
8794
Bram Moolenaar0d660222005-01-07 21:51:51 +00008795 lnum = get_tv_lnum(argvars);
8796 if (lnum >= 0
8797 && lnum <= curbuf->b_ml.ml_line_count
8798 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008799 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008800 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008801 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008802 l = argvars[1].vval.v_list;
8803 if (l == NULL)
8804 return;
8805 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008806 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008807 for (;;)
8808 {
8809 if (l == NULL)
8810 tv = &argvars[1]; /* append a string */
8811 else if (li == NULL)
8812 break; /* end of list */
8813 else
8814 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008815 line = get_tv_string_chk(tv);
8816 if (line == NULL) /* type error */
8817 {
8818 rettv->vval.v_number = 1; /* Failed */
8819 break;
8820 }
8821 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008822 ++added;
8823 if (l == NULL)
8824 break;
8825 li = li->li_next;
8826 }
8827
8828 appended_lines_mark(lnum, added);
8829 if (curwin->w_cursor.lnum > lnum)
8830 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008832 else
8833 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834}
8835
8836/*
8837 * "argc()" function
8838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008841 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
8846
8847/*
8848 * "argidx()" function
8849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
8858/*
8859 * "argv(nr)" function
8860 */
8861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *argvars;
8864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 int idx;
8867
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008868 if (argvars[0].v_type != VAR_UNKNOWN)
8869 {
8870 idx = get_tv_number_chk(&argvars[0], NULL);
8871 if (idx >= 0 && idx < ARGCOUNT)
8872 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8873 else
8874 rettv->vval.v_string = NULL;
8875 rettv->v_type = VAR_STRING;
8876 }
8877 else if (rettv_list_alloc(rettv) == OK)
8878 for (idx = 0; idx < ARGCOUNT; ++idx)
8879 list_append_string(rettv->vval.v_list,
8880 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881}
8882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008883#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008884/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008885 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008887 static void
8888f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008890 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008891{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008892 float_T f;
8893
8894 rettv->v_type = VAR_FLOAT;
8895 if (get_float_arg(argvars, &f) == OK)
8896 rettv->vval.v_float = asin(f);
8897 else
8898 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899}
8900
8901/*
8902 * "atan()" function
8903 */
8904 static void
8905f_atan(argvars, rettv)
8906 typval_T *argvars;
8907 typval_T *rettv;
8908{
8909 float_T f;
8910
8911 rettv->v_type = VAR_FLOAT;
8912 if (get_float_arg(argvars, &f) == OK)
8913 rettv->vval.v_float = atan(f);
8914 else
8915 rettv->vval.v_float = 0.0;
8916}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008917
8918/*
8919 * "atan2()" function
8920 */
8921 static void
8922f_atan2(argvars, rettv)
8923 typval_T *argvars;
8924 typval_T *rettv;
8925{
8926 float_T fx, fy;
8927
8928 rettv->v_type = VAR_FLOAT;
8929 if (get_float_arg(argvars, &fx) == OK
8930 && get_float_arg(&argvars[1], &fy) == OK)
8931 rettv->vval.v_float = atan2(fx, fy);
8932 else
8933 rettv->vval.v_float = 0.0;
8934}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008935#endif
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937/*
8938 * "browse(save, title, initdir, default)" function
8939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008942 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
8945#ifdef FEAT_BROWSE
8946 int save;
8947 char_u *title;
8948 char_u *initdir;
8949 char_u *defname;
8950 char_u buf[NUMBUFLEN];
8951 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 save = get_tv_number_chk(&argvars[0], &error);
8955 title = get_tv_string_chk(&argvars[1]);
8956 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8957 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008959 if (error || title == NULL || initdir == NULL || defname == NULL)
8960 rettv->vval.v_string = NULL;
8961 else
8962 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963 do_browse(save ? BROWSE_SAVE : 0,
8964 title, defname, NULL, initdir, NULL, curbuf);
8965#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008969}
8970
8971/*
8972 * "browsedir(title, initdir)" function
8973 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008976 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008978{
8979#ifdef FEAT_BROWSE
8980 char_u *title;
8981 char_u *initdir;
8982 char_u buf[NUMBUFLEN];
8983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 title = get_tv_string_chk(&argvars[0]);
8985 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 if (title == NULL || initdir == NULL)
8988 rettv->vval.v_string = NULL;
8989 else
8990 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008991 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
Bram Moolenaar33570922005-01-25 22:26:29 +00008998static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*
9001 * Find a buffer by number or exact name.
9002 */
9003 static buf_T *
9004find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009009 if (avar->v_type == VAR_NUMBER)
9010 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009011 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009014 if (buf == NULL)
9015 {
9016 /* No full path name match, try a match with a URL or a "nofile"
9017 * buffer, these don't use the full path. */
9018 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9019 if (buf->b_fname != NULL
9020 && (path_with_url(buf->b_fname)
9021#ifdef FEAT_QUICKFIX
9022 || bt_nofile(buf)
9023#endif
9024 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009025 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009026 break;
9027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 }
9029 return buf;
9030}
9031
9032/*
9033 * "bufexists(expr)" function
9034 */
9035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041}
9042
9043/*
9044 * "buflisted(expr)" function
9045 */
9046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 typval_T *argvars;
9049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051 buf_T *buf;
9052
9053 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
9057/*
9058 * "bufloaded(expr)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 buf_T *buf;
9066
9067 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009071static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073/*
9074 * Get buffer by number or pattern.
9075 */
9076 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009077get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009079 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 int save_magic;
9083 char_u *save_cpo;
9084 buf_T *buf;
9085
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 if (tv->v_type == VAR_NUMBER)
9087 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009088 if (tv->v_type != VAR_STRING)
9089 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 if (name == NULL || *name == NUL)
9091 return curbuf;
9092 if (name[0] == '$' && name[1] == NUL)
9093 return lastbuf;
9094
9095 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9096 save_magic = p_magic;
9097 p_magic = TRUE;
9098 save_cpo = p_cpo;
9099 p_cpo = (char_u *)"";
9100
9101 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009102 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103
9104 p_magic = save_magic;
9105 p_cpo = save_cpo;
9106
9107 /* If not found, try expanding the name, like done for bufexists(). */
9108 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
9111 return buf;
9112}
9113
9114/*
9115 * "bufname(expr)" function
9116 */
9117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *argvars;
9120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 buf_T *buf;
9123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009124 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009126 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 --emsg_off;
9133}
9134
9135/*
9136 * "bufnr(expr)" function
9137 */
9138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 typval_T *argvars;
9141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
9143 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009144 int error = FALSE;
9145 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009147 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009149 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009150 --emsg_off;
9151
9152 /* If the buffer isn't found and the second argument is not zero create a
9153 * new buffer. */
9154 if (buf == NULL
9155 && argvars[1].v_type != VAR_UNKNOWN
9156 && get_tv_number_chk(&argvars[1], &error) != 0
9157 && !error
9158 && (name = get_tv_string_chk(&argvars[0])) != NULL
9159 && !error)
9160 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166}
9167
9168/*
9169 * "bufwinnr(nr)" function
9170 */
9171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T *argvars;
9174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176#ifdef FEAT_WINDOWS
9177 win_T *wp;
9178 int winnr = 0;
9179#endif
9180 buf_T *buf;
9181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009184 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#ifdef FEAT_WINDOWS
9186 for (wp = firstwin; wp; wp = wp->w_next)
9187 {
9188 ++winnr;
9189 if (wp->w_buffer == buf)
9190 break;
9191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195#endif
9196 --emsg_off;
9197}
9198
9199/*
9200 * "byte2line(byte)" function
9201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009204 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209#else
9210 long boff = 0;
9211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 (linenr_T)0, &boff);
9218#endif
9219}
9220
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009221 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009222byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009223 typval_T *argvars;
9224 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009225 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009226{
9227#ifdef FEAT_MBYTE
9228 char_u *t;
9229#endif
9230 char_u *str;
9231 long idx;
9232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 str = get_tv_string_chk(&argvars[0]);
9234 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009237 return;
9238
9239#ifdef FEAT_MBYTE
9240 t = str;
9241 for ( ; idx > 0; idx--)
9242 {
9243 if (*t == NUL) /* EOL reached */
9244 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009245 if (enc_utf8 && comp)
9246 t += utf_ptr2len(t);
9247 else
9248 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009249 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009250 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009251#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009252 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254#endif
9255}
9256
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009257/*
9258 * "byteidx()" function
9259 */
9260 static void
9261f_byteidx(argvars, rettv)
9262 typval_T *argvars;
9263 typval_T *rettv;
9264{
9265 byteidx(argvars, rettv, FALSE);
9266}
9267
9268/*
9269 * "byteidxcomp()" function
9270 */
9271 static void
9272f_byteidxcomp(argvars, rettv)
9273 typval_T *argvars;
9274 typval_T *rettv;
9275{
9276 byteidx(argvars, rettv, TRUE);
9277}
9278
Bram Moolenaardb913952012-06-29 12:54:53 +02009279 int
9280func_call(name, args, selfdict, rettv)
9281 char_u *name;
9282 typval_T *args;
9283 dict_T *selfdict;
9284 typval_T *rettv;
9285{
9286 listitem_T *item;
9287 typval_T argv[MAX_FUNC_ARGS + 1];
9288 int argc = 0;
9289 int dummy;
9290 int r = 0;
9291
9292 for (item = args->vval.v_list->lv_first; item != NULL;
9293 item = item->li_next)
9294 {
9295 if (argc == MAX_FUNC_ARGS)
9296 {
9297 EMSG(_("E699: Too many arguments"));
9298 break;
9299 }
9300 /* Make a copy of each argument. This is needed to be able to set
9301 * v_lock to VAR_FIXED in the copy without changing the original list.
9302 */
9303 copy_tv(&item->li_tv, &argv[argc++]);
9304 }
9305
9306 if (item == NULL)
9307 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9308 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9309 &dummy, TRUE, selfdict);
9310
9311 /* Free the arguments. */
9312 while (argc > 0)
9313 clear_tv(&argv[--argc]);
9314
9315 return r;
9316}
9317
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009318/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319 * "call(func, arglist)" function
9320 */
9321 static void
9322f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 typval_T *argvars;
9324 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009325{
9326 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009329 if (argvars[1].v_type != VAR_LIST)
9330 {
9331 EMSG(_(e_listreq));
9332 return;
9333 }
9334 if (argvars[1].vval.v_list == NULL)
9335 return;
9336
9337 if (argvars[0].v_type == VAR_FUNC)
9338 func = argvars[0].vval.v_string;
9339 else
9340 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 if (*func == NUL)
9342 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009343
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 if (argvars[2].v_type != VAR_UNKNOWN)
9345 {
9346 if (argvars[2].v_type != VAR_DICT)
9347 {
9348 EMSG(_(e_dictreq));
9349 return;
9350 }
9351 selfdict = argvars[2].vval.v_dict;
9352 }
9353
Bram Moolenaardb913952012-06-29 12:54:53 +02009354 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009355}
9356
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009357#ifdef FEAT_FLOAT
9358/*
9359 * "ceil({float})" function
9360 */
9361 static void
9362f_ceil(argvars, rettv)
9363 typval_T *argvars;
9364 typval_T *rettv;
9365{
9366 float_T f;
9367
9368 rettv->v_type = VAR_FLOAT;
9369 if (get_float_arg(argvars, &f) == OK)
9370 rettv->vval.v_float = ceil(f);
9371 else
9372 rettv->vval.v_float = 0.0;
9373}
9374#endif
9375
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009376/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009377 * "changenr()" function
9378 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009379 static void
9380f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009381 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009382 typval_T *rettv;
9383{
9384 rettv->vval.v_number = curbuf->b_u_seq_cur;
9385}
9386
9387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 * "char2nr(string)" function
9389 */
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *argvars;
9393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
9395#ifdef FEAT_MBYTE
9396 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009397 {
9398 int utf8 = 0;
9399
9400 if (argvars[1].v_type != VAR_UNKNOWN)
9401 utf8 = get_tv_number_chk(&argvars[1], NULL);
9402
9403 if (utf8)
9404 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9405 else
9406 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 else
9409#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411}
9412
9413/*
9414 * "cindent(lnum)" function
9415 */
9416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009418 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421#ifdef FEAT_CINDENT
9422 pos_T pos;
9423 linenr_T lnum;
9424
9425 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9428 {
9429 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 curwin->w_cursor = pos;
9432 }
9433 else
9434#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436}
9437
9438/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009439 * "clearmatches()" function
9440 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009441 static void
9442f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009443 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009444 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009445{
9446#ifdef FEAT_SEARCH_EXTRA
9447 clear_matches(curwin);
9448#endif
9449}
9450
9451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 * "col(string)" function
9453 */
9454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 typval_T *argvars;
9457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459 colnr_T col = 0;
9460 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009461 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009463 fp = var2fpos(&argvars[0], FALSE, &fnum);
9464 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 {
9466 if (fp->col == MAXCOL)
9467 {
9468 /* '> can be MAXCOL, get the length of the line then */
9469 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009470 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 else
9472 col = MAXCOL;
9473 }
9474 else
9475 {
9476 col = fp->col + 1;
9477#ifdef FEAT_VIRTUALEDIT
9478 /* col(".") when the cursor is on the NUL at the end of the line
9479 * because of "coladd" can be seen as an extra column. */
9480 if (virtual_active() && fp == &curwin->w_cursor)
9481 {
9482 char_u *p = ml_get_cursor();
9483
9484 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9485 curwin->w_virtcol - curwin->w_cursor.coladd))
9486 {
9487# ifdef FEAT_MBYTE
9488 int l;
9489
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009490 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 col += l;
9492# else
9493 if (*p != NUL && p[1] == NUL)
9494 ++col;
9495# endif
9496 }
9497 }
9498#endif
9499 }
9500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503
Bram Moolenaar572cb562005-08-05 21:35:02 +00009504#if defined(FEAT_INS_EXPAND)
9505/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009506 * "complete()" function
9507 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009508 static void
9509f_complete(argvars, rettv)
9510 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009511 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009512{
9513 int startcol;
9514
9515 if ((State & INSERT) == 0)
9516 {
9517 EMSG(_("E785: complete() can only be used in Insert mode"));
9518 return;
9519 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009520
9521 /* Check for undo allowed here, because if something was already inserted
9522 * the line was already saved for undo and this check isn't done. */
9523 if (!undo_allowed())
9524 return;
9525
Bram Moolenaarade00832006-03-10 21:46:58 +00009526 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9527 {
9528 EMSG(_(e_invarg));
9529 return;
9530 }
9531
9532 startcol = get_tv_number_chk(&argvars[0], NULL);
9533 if (startcol <= 0)
9534 return;
9535
9536 set_completion(startcol - 1, argvars[1].vval.v_list);
9537}
9538
9539/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009540 * "complete_add()" function
9541 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009542 static void
9543f_complete_add(argvars, rettv)
9544 typval_T *argvars;
9545 typval_T *rettv;
9546{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009547 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009548}
9549
9550/*
9551 * "complete_check()" function
9552 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009553 static void
9554f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009555 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009556 typval_T *rettv;
9557{
9558 int saved = RedrawingDisabled;
9559
9560 RedrawingDisabled = 0;
9561 ins_compl_check_keys(0);
9562 rettv->vval.v_number = compl_interrupted;
9563 RedrawingDisabled = saved;
9564}
9565#endif
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567/*
9568 * "confirm(message, buttons[, default [, type]])" function
9569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009572 typval_T *argvars UNUSED;
9573 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9576 char_u *message;
9577 char_u *buttons = NULL;
9578 char_u buf[NUMBUFLEN];
9579 char_u buf2[NUMBUFLEN];
9580 int def = 1;
9581 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 char_u *typestr;
9583 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 message = get_tv_string_chk(&argvars[0]);
9586 if (message == NULL)
9587 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009588 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9591 if (buttons == NULL)
9592 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009593 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009595 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9599 if (typestr == NULL)
9600 error = TRUE;
9601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 switch (TOUPPER_ASC(*typestr))
9604 {
9605 case 'E': type = VIM_ERROR; break;
9606 case 'Q': type = VIM_QUESTION; break;
9607 case 'I': type = VIM_INFO; break;
9608 case 'W': type = VIM_WARNING; break;
9609 case 'G': type = VIM_GENERIC; break;
9610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 }
9612 }
9613 }
9614 }
9615
9616 if (buttons == NULL || *buttons == NUL)
9617 buttons = (char_u *)_("&Ok");
9618
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009619 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009621 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622#endif
9623}
9624
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625/*
9626 * "copy()" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009632{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009633 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009636#ifdef FEAT_FLOAT
9637/*
9638 * "cos()" function
9639 */
9640 static void
9641f_cos(argvars, rettv)
9642 typval_T *argvars;
9643 typval_T *rettv;
9644{
9645 float_T f;
9646
9647 rettv->v_type = VAR_FLOAT;
9648 if (get_float_arg(argvars, &f) == OK)
9649 rettv->vval.v_float = cos(f);
9650 else
9651 rettv->vval.v_float = 0.0;
9652}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009653
9654/*
9655 * "cosh()" function
9656 */
9657 static void
9658f_cosh(argvars, rettv)
9659 typval_T *argvars;
9660 typval_T *rettv;
9661{
9662 float_T f;
9663
9664 rettv->v_type = VAR_FLOAT;
9665 if (get_float_arg(argvars, &f) == OK)
9666 rettv->vval.v_float = cosh(f);
9667 else
9668 rettv->vval.v_float = 0.0;
9669}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009670#endif
9671
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673 * "count()" function
9674 */
9675 static void
9676f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009679{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009680 long n = 0;
9681 int ic = FALSE;
9682
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 listitem_T *li;
9686 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009688
Bram Moolenaare9a41262005-01-15 22:18:47 +00009689 if ((l = argvars[0].vval.v_list) != NULL)
9690 {
9691 li = l->lv_first;
9692 if (argvars[2].v_type != VAR_UNKNOWN)
9693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 int error = FALSE;
9695
9696 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 if (argvars[3].v_type != VAR_UNKNOWN)
9698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 idx = get_tv_number_chk(&argvars[3], &error);
9700 if (!error)
9701 {
9702 li = list_find(l, idx);
9703 if (li == NULL)
9704 EMSGN(_(e_listidx), idx);
9705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009707 if (error)
9708 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009709 }
9710
9711 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009712 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009713 ++n;
9714 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 else if (argvars[0].v_type == VAR_DICT)
9717 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 int todo;
9719 dict_T *d;
9720 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009721
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009722 if ((d = argvars[0].vval.v_dict) != NULL)
9723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724 int error = FALSE;
9725
Bram Moolenaare9a41262005-01-15 22:18:47 +00009726 if (argvars[2].v_type != VAR_UNKNOWN)
9727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009729 if (argvars[3].v_type != VAR_UNKNOWN)
9730 EMSG(_(e_invarg));
9731 }
9732
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009733 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009735 {
9736 if (!HASHITEM_EMPTY(hi))
9737 {
9738 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009739 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009740 ++n;
9741 }
9742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009743 }
9744 }
9745 else
9746 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009747 rettv->vval.v_number = n;
9748}
9749
9750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9752 *
9753 * Checks the existence of a cscope connection.
9754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009757 typval_T *argvars UNUSED;
9758 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760#ifdef FEAT_CSCOPE
9761 int num = 0;
9762 char_u *dbpath = NULL;
9763 char_u *prepend = NULL;
9764 char_u buf[NUMBUFLEN];
9765
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009766 if (argvars[0].v_type != VAR_UNKNOWN
9767 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 num = (int)get_tv_number(&argvars[0]);
9770 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 }
9774
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776#endif
9777}
9778
9779/*
9780 * "cursor(lnum, col)" function
9781 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009782 * Moves the cursor to the specified line and column.
9783 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009791#ifdef FEAT_VIRTUALEDIT
9792 long coladd = 0;
9793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009795 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009796 if (argvars[1].v_type == VAR_UNKNOWN)
9797 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009798 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009799
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009800 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009801 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009802 line = pos.lnum;
9803 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009804#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009805 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009806#endif
9807 }
9808 else
9809 {
9810 line = get_tv_lnum(argvars);
9811 col = get_tv_number_chk(&argvars[1], NULL);
9812#ifdef FEAT_VIRTUALEDIT
9813 if (argvars[2].v_type != VAR_UNKNOWN)
9814 coladd = get_tv_number_chk(&argvars[2], NULL);
9815#endif
9816 }
9817 if (line < 0 || col < 0
9818#ifdef FEAT_VIRTUALEDIT
9819 || coladd < 0
9820#endif
9821 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 if (line > 0)
9824 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (col > 0)
9826 curwin->w_cursor.col = col - 1;
9827#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009828 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829#endif
9830
9831 /* Make sure the cursor is in a valid position. */
9832 check_cursor();
9833#ifdef FEAT_MBYTE
9834 /* Correct cursor for multi-byte character. */
9835 if (has_mbyte)
9836 mb_adjust_cursor();
9837#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009838
9839 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009840 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841}
9842
9843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009844 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 */
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009851 int noref = 0;
9852
9853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009855 if (noref < 0 || noref > 1)
9856 EMSG(_(e_invarg));
9857 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009858 {
9859 current_copyID += COPYID_INC;
9860 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "delete()" function
9866 */
9867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876}
9877
9878/*
9879 * "did_filetype()" function
9880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009883 typval_T *argvars UNUSED;
9884 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888#endif
9889}
9890
9891/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892 * "diff_filler()" function
9893 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009896 typval_T *argvars UNUSED;
9897 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009898{
9899#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901#endif
9902}
9903
9904/*
9905 * "diff_hlID()" function
9906 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009909 typval_T *argvars UNUSED;
9910 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009911{
9912#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914 static linenr_T prev_lnum = 0;
9915 static int changedtick = 0;
9916 static int fnum = 0;
9917 static int change_start = 0;
9918 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009919 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009920 int filler_lines;
9921 int col;
9922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 if (lnum < 0) /* ignore type error in {lnum} arg */
9924 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009925 if (lnum != prev_lnum
9926 || changedtick != curbuf->b_changedtick
9927 || fnum != curbuf->b_fnum)
9928 {
9929 /* New line, buffer, change: need to get the values. */
9930 filler_lines = diff_check(curwin, lnum);
9931 if (filler_lines < 0)
9932 {
9933 if (filler_lines == -1)
9934 {
9935 change_start = MAXCOL;
9936 change_end = -1;
9937 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9938 hlID = HLF_ADD; /* added line */
9939 else
9940 hlID = HLF_CHD; /* changed line */
9941 }
9942 else
9943 hlID = HLF_ADD; /* added line */
9944 }
9945 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009946 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009947 prev_lnum = lnum;
9948 changedtick = curbuf->b_changedtick;
9949 fnum = curbuf->b_fnum;
9950 }
9951
9952 if (hlID == HLF_CHD || hlID == HLF_TXD)
9953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009955 if (col >= change_start && col <= change_end)
9956 hlID = HLF_TXD; /* changed text */
9957 else
9958 hlID = HLF_CHD; /* changed line */
9959 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009960 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009961#endif
9962}
9963
9964/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009965 * "empty({expr})" function
9966 */
9967 static void
9968f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *argvars;
9970 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009971{
9972 int n;
9973
9974 switch (argvars[0].v_type)
9975 {
9976 case VAR_STRING:
9977 case VAR_FUNC:
9978 n = argvars[0].vval.v_string == NULL
9979 || *argvars[0].vval.v_string == NUL;
9980 break;
9981 case VAR_NUMBER:
9982 n = argvars[0].vval.v_number == 0;
9983 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984#ifdef FEAT_FLOAT
9985 case VAR_FLOAT:
9986 n = argvars[0].vval.v_float == 0.0;
9987 break;
9988#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009989 case VAR_LIST:
9990 n = argvars[0].vval.v_list == NULL
9991 || argvars[0].vval.v_list->lv_first == NULL;
9992 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 case VAR_DICT:
9994 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009997 default:
9998 EMSG2(_(e_intern2), "f_empty()");
9999 n = 0;
10000 }
10001
10002 rettv->vval.v_number = n;
10003}
10004
10005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 * "escape({string}, {chars})" function
10007 */
10008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 char_u buf[NUMBUFLEN];
10014
Bram Moolenaar758711c2005-02-02 23:11:38 +000010015 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10016 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018}
10019
10020/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010021 * "eval()" function
10022 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010023 static void
10024f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010025 typval_T *argvars;
10026 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010027{
10028 char_u *s;
10029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030 s = get_tv_string_chk(&argvars[0]);
10031 if (s != NULL)
10032 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010034 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10035 {
10036 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010037 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010039 else if (*s != NUL)
10040 EMSG(_(e_trailing));
10041}
10042
10043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 * "eventhandler()" function
10045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054/*
10055 * "executable()" function
10056 */
10057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010059 typval_T *argvars;
10060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010062 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10063}
10064
10065/*
10066 * "exepath()" function
10067 */
10068 static void
10069f_exepath(argvars, rettv)
10070 typval_T *argvars;
10071 typval_T *rettv;
10072{
10073 char_u *p = NULL;
10074
10075 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10076 rettv->v_type = VAR_STRING;
10077 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
10081 * "exists()" function
10082 */
10083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 char_u *p;
10089 char_u *name;
10090 int n = FALSE;
10091 int len = 0;
10092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (*p == '$') /* environment variable */
10095 {
10096 /* first try "normal" environment variables (fast) */
10097 if (mch_getenv(p + 1) != NULL)
10098 n = TRUE;
10099 else
10100 {
10101 /* try expanding things like $VIM and ${HOME} */
10102 p = expand_env_save(p);
10103 if (p != NULL && *p != '$')
10104 n = TRUE;
10105 vim_free(p);
10106 }
10107 }
10108 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010111 if (*skipwhite(p) != NUL)
10112 n = FALSE; /* trailing garbage */
10113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 else if (*p == '*') /* internal or user defined function */
10115 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010116 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 }
10118 else if (*p == ':')
10119 {
10120 n = cmd_exists(p + 1);
10121 }
10122 else if (*p == '#')
10123 {
10124#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010125 if (p[1] == '#')
10126 n = autocmd_supported(p + 2);
10127 else
10128 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129#endif
10130 }
10131 else /* internal variable */
10132 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010133 char_u *tofree;
10134 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010136 /* get_name_len() takes care of expanding curly braces */
10137 name = p;
10138 len = get_name_len(&p, &tofree, TRUE, FALSE);
10139 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010141 if (tofree != NULL)
10142 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010143 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010144 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010146 /* handle d.key, l[idx], f(expr) */
10147 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10148 if (n)
10149 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
10151 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010152 if (*p != NUL)
10153 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010155 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159}
10160
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010161#ifdef FEAT_FLOAT
10162/*
10163 * "exp()" function
10164 */
10165 static void
10166f_exp(argvars, rettv)
10167 typval_T *argvars;
10168 typval_T *rettv;
10169{
10170 float_T f;
10171
10172 rettv->v_type = VAR_FLOAT;
10173 if (get_float_arg(argvars, &f) == OK)
10174 rettv->vval.v_float = exp(f);
10175 else
10176 rettv->vval.v_float = 0.0;
10177}
10178#endif
10179
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180/*
10181 * "expand()" function
10182 */
10183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 typval_T *argvars;
10186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
10188 char_u *s;
10189 int len;
10190 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010191 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010194 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010197 if (argvars[1].v_type != VAR_UNKNOWN
10198 && argvars[2].v_type != VAR_UNKNOWN
10199 && get_tv_number_chk(&argvars[2], &error)
10200 && !error)
10201 {
10202 rettv->v_type = VAR_LIST;
10203 rettv->vval.v_list = NULL;
10204 }
10205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 if (*s == '%' || *s == '#' || *s == '<')
10208 {
10209 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010210 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010212 if (rettv->v_type == VAR_LIST)
10213 {
10214 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10215 list_append_string(rettv->vval.v_list, result, -1);
10216 else
10217 vim_free(result);
10218 }
10219 else
10220 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 }
10222 else
10223 {
10224 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010225 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 if (argvars[1].v_type != VAR_UNKNOWN
10227 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010228 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 if (!error)
10230 {
10231 ExpandInit(&xpc);
10232 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010233 if (p_wic)
10234 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010235 if (rettv->v_type == VAR_STRING)
10236 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10237 options, WILD_ALL);
10238 else if (rettv_list_alloc(rettv) != FAIL)
10239 {
10240 int i;
10241
10242 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10243 for (i = 0; i < xpc.xp_numfiles; i++)
10244 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10245 ExpandCleanup(&xpc);
10246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 }
10248 else
10249 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 }
10251}
10252
10253/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010254 * Go over all entries in "d2" and add them to "d1".
10255 * When "action" is "error" then a duplicate key is an error.
10256 * When "action" is "force" then a duplicate key is overwritten.
10257 * Otherwise duplicate keys are ignored ("action" is "keep").
10258 */
10259 void
10260dict_extend(d1, d2, action)
10261 dict_T *d1;
10262 dict_T *d2;
10263 char_u *action;
10264{
10265 dictitem_T *di1;
10266 hashitem_T *hi2;
10267 int todo;
10268
10269 todo = (int)d2->dv_hashtab.ht_used;
10270 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10271 {
10272 if (!HASHITEM_EMPTY(hi2))
10273 {
10274 --todo;
10275 di1 = dict_find(d1, hi2->hi_key, -1);
10276 if (d1->dv_scope != 0)
10277 {
10278 /* Disallow replacing a builtin function in l: and g:.
10279 * Check the key to be valid when adding to any
10280 * scope. */
10281 if (d1->dv_scope == VAR_DEF_SCOPE
10282 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10283 && var_check_func_name(hi2->hi_key,
10284 di1 == NULL))
10285 break;
10286 if (!valid_varname(hi2->hi_key))
10287 break;
10288 }
10289 if (di1 == NULL)
10290 {
10291 di1 = dictitem_copy(HI2DI(hi2));
10292 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10293 dictitem_free(di1);
10294 }
10295 else if (*action == 'e')
10296 {
10297 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10298 break;
10299 }
10300 else if (*action == 'f' && HI2DI(hi2) != di1)
10301 {
10302 clear_tv(&di1->di_tv);
10303 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10304 }
10305 }
10306 }
10307}
10308
10309/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010310 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010312 */
10313 static void
10314f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010317{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010318 char *arg_errmsg = N_("extend() argument");
10319
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010321 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010322 list_T *l1, *l2;
10323 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010324 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010326
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 l1 = argvars[0].vval.v_list;
10328 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010329 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010330 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010331 {
10332 if (argvars[2].v_type != VAR_UNKNOWN)
10333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 before = get_tv_number_chk(&argvars[2], &error);
10335 if (error)
10336 return; /* type error; errmsg already given */
10337
Bram Moolenaar758711c2005-02-02 23:11:38 +000010338 if (before == l1->lv_len)
10339 item = NULL;
10340 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010342 item = list_find(l1, before);
10343 if (item == NULL)
10344 {
10345 EMSGN(_(e_listidx), before);
10346 return;
10347 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 }
10349 }
10350 else
10351 item = NULL;
10352 list_extend(l1, l2, item);
10353
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 copy_tv(&argvars[0], rettv);
10355 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010356 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10358 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010359 dict_T *d1, *d2;
10360 char_u *action;
10361 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362
10363 d1 = argvars[0].vval.v_dict;
10364 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010365 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010366 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010367 {
10368 /* Check the third argument. */
10369 if (argvars[2].v_type != VAR_UNKNOWN)
10370 {
10371 static char *(av[]) = {"keep", "force", "error"};
10372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 action = get_tv_string_chk(&argvars[2]);
10374 if (action == NULL)
10375 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010376 for (i = 0; i < 3; ++i)
10377 if (STRCMP(action, av[i]) == 0)
10378 break;
10379 if (i == 3)
10380 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010381 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010382 return;
10383 }
10384 }
10385 else
10386 action = (char_u *)"force";
10387
Bram Moolenaara9922d62013-05-30 13:01:18 +020010388 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010389
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390 copy_tv(&argvars[0], rettv);
10391 }
10392 }
10393 else
10394 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010395}
10396
10397/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010398 * "feedkeys()" function
10399 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010400 static void
10401f_feedkeys(argvars, rettv)
10402 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010403 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010404{
10405 int remap = TRUE;
10406 char_u *keys, *flags;
10407 char_u nbuf[NUMBUFLEN];
10408 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010409 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010410
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010411 /* This is not allowed in the sandbox. If the commands would still be
10412 * executed in the sandbox it would be OK, but it probably happens later,
10413 * when "sandbox" is no longer set. */
10414 if (check_secure())
10415 return;
10416
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010417 keys = get_tv_string(&argvars[0]);
10418 if (*keys != NUL)
10419 {
10420 if (argvars[1].v_type != VAR_UNKNOWN)
10421 {
10422 flags = get_tv_string_buf(&argvars[1], nbuf);
10423 for ( ; *flags != NUL; ++flags)
10424 {
10425 switch (*flags)
10426 {
10427 case 'n': remap = FALSE; break;
10428 case 'm': remap = TRUE; break;
10429 case 't': typed = TRUE; break;
10430 }
10431 }
10432 }
10433
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010434 /* Need to escape K_SPECIAL and CSI before putting the string in the
10435 * typeahead buffer. */
10436 keys_esc = vim_strsave_escape_csi(keys);
10437 if (keys_esc != NULL)
10438 {
10439 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010440 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010441 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010442 if (vgetc_busy)
10443 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010444 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010445 }
10446}
10447
10448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 * "filereadable()" function
10450 */
10451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T *argvars;
10454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010456 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 char_u *p;
10458 int n;
10459
Bram Moolenaarc236c162008-07-13 17:41:49 +000010460#ifndef O_NONBLOCK
10461# define O_NONBLOCK 0
10462#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010464 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10465 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 {
10467 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010468 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 }
10470 else
10471 n = FALSE;
10472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474}
10475
10476/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010477 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 * rights to write into.
10479 */
10480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010482 typval_T *argvars;
10483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010485 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486}
10487
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010488static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010489
10490 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010491findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010493 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010494 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010495{
10496#ifdef FEAT_SEARCHPATH
10497 char_u *fname;
10498 char_u *fresult = NULL;
10499 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10500 char_u *p;
10501 char_u pathbuf[NUMBUFLEN];
10502 int count = 1;
10503 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010504 int error = FALSE;
10505#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010506
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010507 rettv->vval.v_string = NULL;
10508 rettv->v_type = VAR_STRING;
10509
10510#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10516 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010517 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 else
10519 {
10520 if (*p != NUL)
10521 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010524 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010526 }
10527
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010528 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10529 error = TRUE;
10530
10531 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 do
10534 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010535 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010536 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 fresult = find_file_in_path_option(first ? fname : NULL,
10538 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010539 0, first, path,
10540 find_what,
10541 curbuf->b_ffname,
10542 find_what == FINDFILE_DIR
10543 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010545
10546 if (fresult != NULL && rettv->v_type == VAR_LIST)
10547 list_append_string(rettv->vval.v_list, fresult, -1);
10548
10549 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010551
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010552 if (rettv->v_type == VAR_STRING)
10553 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010554#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010555}
10556
Bram Moolenaar33570922005-01-25 22:26:29 +000010557static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10558static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010559
10560/*
10561 * Implementation of map() and filter().
10562 */
10563 static void
10564filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567 int map;
10568{
10569 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 listitem_T *li, *nli;
10572 list_T *l = NULL;
10573 dictitem_T *di;
10574 hashtab_T *ht;
10575 hashitem_T *hi;
10576 dict_T *d = NULL;
10577 typval_T save_val;
10578 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010580 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010581 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10582 char *arg_errmsg = (map ? N_("map() argument")
10583 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010584 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010585 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010586
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010589 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010590 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 return;
10592 }
10593 else if (argvars[0].v_type == VAR_DICT)
10594 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010595 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010596 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 return;
10598 }
10599 else
10600 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010601 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 return;
10603 }
10604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 expr = get_tv_string_buf_chk(&argvars[1], buf);
10606 /* On type errors, the preceding call has already displayed an error
10607 * message. Avoid a misleading error message for an empty string that
10608 * was not passed as argument. */
10609 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 prepare_vimvar(VV_VAL, &save_val);
10612 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010613
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010614 /* We reset "did_emsg" to be able to detect whether an error
10615 * occurred during evaluation of the expression. */
10616 save_did_emsg = did_emsg;
10617 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010618
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010619 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 vimvars[VV_KEY].vv_type = VAR_STRING;
10623
10624 ht = &d->dv_hashtab;
10625 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010626 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 if (!HASHITEM_EMPTY(hi))
10630 {
10631 --todo;
10632 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010633 if (tv_check_lock(di->di_tv.v_lock,
10634 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 break;
10636 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010637 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010638 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 break;
10640 if (!map && rem)
10641 dictitem_remove(d, di);
10642 clear_tv(&vimvars[VV_KEY].vv_tv);
10643 }
10644 }
10645 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 }
10647 else
10648 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010649 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010651 for (li = l->lv_first; li != NULL; li = nli)
10652 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010653 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010654 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010656 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010657 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010658 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010659 break;
10660 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010662 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010663 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010665
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010666 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010668
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010669 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010671
10672 copy_tv(&argvars[0], rettv);
10673}
10674
10675 static int
10676filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 char_u *expr;
10679 int map;
10680 int *remp;
10681{
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010683 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010684 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685
Bram Moolenaar33570922005-01-25 22:26:29 +000010686 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010687 s = expr;
10688 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010689 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010690 if (*s != NUL) /* check for trailing chars after expr */
10691 {
10692 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010693 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010694 }
10695 if (map)
10696 {
10697 /* map(): replace the list item value */
10698 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010699 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010700 *tv = rettv;
10701 }
10702 else
10703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 int error = FALSE;
10705
Bram Moolenaare9a41262005-01-15 22:18:47 +000010706 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010708 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010709 /* On type error, nothing has been removed; return FAIL to stop the
10710 * loop. The error message was given by get_tv_number_chk(). */
10711 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010712 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010713 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010714 retval = OK;
10715theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010717 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010718}
10719
10720/*
10721 * "filter()" function
10722 */
10723 static void
10724f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010727{
10728 filter_map(argvars, rettv, FALSE);
10729}
10730
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010732 * "finddir({fname}[, {path}[, {count}]])" function
10733 */
10734 static void
10735f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010738{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010739 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740}
10741
10742/*
10743 * "findfile({fname}[, {path}[, {count}]])" function
10744 */
10745 static void
10746f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010747 typval_T *argvars;
10748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010749{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010750 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010751}
10752
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010753#ifdef FEAT_FLOAT
10754/*
10755 * "float2nr({float})" function
10756 */
10757 static void
10758f_float2nr(argvars, rettv)
10759 typval_T *argvars;
10760 typval_T *rettv;
10761{
10762 float_T f;
10763
10764 if (get_float_arg(argvars, &f) == OK)
10765 {
10766 if (f < -0x7fffffff)
10767 rettv->vval.v_number = -0x7fffffff;
10768 else if (f > 0x7fffffff)
10769 rettv->vval.v_number = 0x7fffffff;
10770 else
10771 rettv->vval.v_number = (varnumber_T)f;
10772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010773}
10774
10775/*
10776 * "floor({float})" function
10777 */
10778 static void
10779f_floor(argvars, rettv)
10780 typval_T *argvars;
10781 typval_T *rettv;
10782{
10783 float_T f;
10784
10785 rettv->v_type = VAR_FLOAT;
10786 if (get_float_arg(argvars, &f) == OK)
10787 rettv->vval.v_float = floor(f);
10788 else
10789 rettv->vval.v_float = 0.0;
10790}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010791
10792/*
10793 * "fmod()" function
10794 */
10795 static void
10796f_fmod(argvars, rettv)
10797 typval_T *argvars;
10798 typval_T *rettv;
10799{
10800 float_T fx, fy;
10801
10802 rettv->v_type = VAR_FLOAT;
10803 if (get_float_arg(argvars, &fx) == OK
10804 && get_float_arg(&argvars[1], &fy) == OK)
10805 rettv->vval.v_float = fmod(fx, fy);
10806 else
10807 rettv->vval.v_float = 0.0;
10808}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010809#endif
10810
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010812 * "fnameescape({string})" function
10813 */
10814 static void
10815f_fnameescape(argvars, rettv)
10816 typval_T *argvars;
10817 typval_T *rettv;
10818{
10819 rettv->vval.v_string = vim_strsave_fnameescape(
10820 get_tv_string(&argvars[0]), FALSE);
10821 rettv->v_type = VAR_STRING;
10822}
10823
10824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 * "fnamemodify({fname}, {mods})" function
10826 */
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832 char_u *fname;
10833 char_u *mods;
10834 int usedlen = 0;
10835 int len;
10836 char_u *fbuf = NULL;
10837 char_u buf[NUMBUFLEN];
10838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 fname = get_tv_string_chk(&argvars[0]);
10840 mods = get_tv_string_buf_chk(&argvars[1], buf);
10841 if (fname == NULL || mods == NULL)
10842 fname = NULL;
10843 else
10844 {
10845 len = (int)STRLEN(fname);
10846 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 vim_free(fbuf);
10855}
10856
Bram Moolenaar33570922005-01-25 22:26:29 +000010857static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858
10859/*
10860 * "foldclosed()" function
10861 */
10862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010866 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867{
10868#ifdef FEAT_FOLDING
10869 linenr_T lnum;
10870 linenr_T first, last;
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10874 {
10875 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10876 {
10877 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 return;
10882 }
10883 }
10884#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886}
10887
10888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010889 * "foldclosed()" function
10890 */
10891 static void
10892f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895{
10896 foldclosed_both(argvars, rettv, FALSE);
10897}
10898
10899/*
10900 * "foldclosedend()" function
10901 */
10902 static void
10903f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *argvars;
10905 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906{
10907 foldclosed_both(argvars, rettv, TRUE);
10908}
10909
10910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 * "foldlevel()" function
10912 */
10913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010915 typval_T *argvars UNUSED;
10916 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
10918#ifdef FEAT_FOLDING
10919 linenr_T lnum;
10920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925}
10926
10927/*
10928 * "foldtext()" function
10929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935#ifdef FEAT_FOLDING
10936 linenr_T lnum;
10937 char_u *s;
10938 char_u *r;
10939 int len;
10940 char *txt;
10941#endif
10942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 rettv->v_type = VAR_STRING;
10944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10947 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10948 <= curbuf->b_ml.ml_line_count
10949 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 {
10951 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10953 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 {
10955 if (!linewhite(lnum))
10956 break;
10957 ++lnum;
10958 }
10959
10960 /* Find interesting text in this line. */
10961 s = skipwhite(ml_get(lnum));
10962 /* skip C comment-start */
10963 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010966 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010968 {
10969 s = skipwhite(ml_get(lnum + 1));
10970 if (*s == '*')
10971 s = skipwhite(s + 1);
10972 }
10973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 txt = _("+-%s%3ld lines: ");
10975 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 + 20 /* for %3ld */
10978 + STRLEN(s))); /* concatenated */
10979 if (r != NULL)
10980 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10982 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10983 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 len = (int)STRLEN(r);
10985 STRCAT(r, s);
10986 /* remove 'foldmarker' and 'commentstring' */
10987 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 }
10990 }
10991#endif
10992}
10993
10994/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010995 * "foldtextresult(lnum)" function
10996 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011000 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011001{
11002#ifdef FEAT_FOLDING
11003 linenr_T lnum;
11004 char_u *text;
11005 char_u buf[51];
11006 foldinfo_T foldinfo;
11007 int fold_count;
11008#endif
11009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->v_type = VAR_STRING;
11011 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011012#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011014 /* treat illegal types and illegal string values for {lnum} the same */
11015 if (lnum < 0)
11016 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011017 fold_count = foldedCount(curwin, lnum, &foldinfo);
11018 if (fold_count > 0)
11019 {
11020 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11021 &foldinfo, buf);
11022 if (text == buf)
11023 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011025 }
11026#endif
11027}
11028
11029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 * "foreground()" function
11031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011034 typval_T *argvars UNUSED;
11035 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037#ifdef FEAT_GUI
11038 if (gui.in_use)
11039 gui_mch_set_foreground();
11040#else
11041# ifdef WIN32
11042 win32_set_foreground();
11043# endif
11044#endif
11045}
11046
11047/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048 * "function()" function
11049 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *argvars;
11053 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054{
11055 char_u *s;
11056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011058 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011059 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011060 /* Don't check an autoload name for existence here. */
11061 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011062 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011063 else
11064 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011065 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011066 {
11067 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011068 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011069
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011070 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11071 * also be called from another script. Using trans_function_name()
11072 * would also work, but some plugins depend on the name being
11073 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011074 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011075 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011076 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011077 if (rettv->vval.v_string != NULL)
11078 {
11079 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011080 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011081 }
11082 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011083 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011084 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 }
11087}
11088
11089/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011090 * "garbagecollect()" function
11091 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011092 static void
11093f_garbagecollect(argvars, rettv)
11094 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011095 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011096{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011097 /* This is postponed until we are back at the toplevel, because we may be
11098 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11099 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011100
11101 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11102 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011103}
11104
11105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106 * "get()" function
11107 */
11108 static void
11109f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *argvars;
11111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112{
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 listitem_T *li;
11114 list_T *l;
11115 dictitem_T *di;
11116 dict_T *d;
11117 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 int error = FALSE;
11124
11125 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11126 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 else if (argvars[0].v_type == VAR_DICT)
11131 {
11132 if ((d = argvars[0].vval.v_dict) != NULL)
11133 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011134 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 if (di != NULL)
11136 tv = &di->di_tv;
11137 }
11138 }
11139 else
11140 EMSG2(_(e_listdictarg), "get()");
11141
11142 if (tv == NULL)
11143 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011145 copy_tv(&argvars[2], rettv);
11146 }
11147 else
11148 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149}
11150
Bram Moolenaar342337a2005-07-21 21:11:17 +000011151static 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 +000011152
11153/*
11154 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011155 * Return a range (from start to end) of lines in rettv from the specified
11156 * buffer.
11157 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011158 */
11159 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011160get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011161 buf_T *buf;
11162 linenr_T start;
11163 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011164 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011165 typval_T *rettv;
11166{
11167 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011168
Bram Moolenaar959a1432013-12-14 12:17:38 +010011169 rettv->v_type = VAR_STRING;
11170 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011171 if (retlist && rettv_list_alloc(rettv) == FAIL)
11172 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011173
11174 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11175 return;
11176
11177 if (!retlist)
11178 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011179 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11180 p = ml_get_buf(buf, start, FALSE);
11181 else
11182 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011183 rettv->vval.v_string = vim_strsave(p);
11184 }
11185 else
11186 {
11187 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011188 return;
11189
11190 if (start < 1)
11191 start = 1;
11192 if (end > buf->b_ml.ml_line_count)
11193 end = buf->b_ml.ml_line_count;
11194 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011195 if (list_append_string(rettv->vval.v_list,
11196 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011197 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011198 }
11199}
11200
11201/*
11202 * "getbufline()" function
11203 */
11204 static void
11205f_getbufline(argvars, rettv)
11206 typval_T *argvars;
11207 typval_T *rettv;
11208{
11209 linenr_T lnum;
11210 linenr_T end;
11211 buf_T *buf;
11212
11213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11214 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011215 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011216 --emsg_off;
11217
Bram Moolenaar661b1822005-07-28 22:36:45 +000011218 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011219 if (argvars[2].v_type == VAR_UNKNOWN)
11220 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011221 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011222 end = get_tv_lnum_buf(&argvars[2], buf);
11223
Bram Moolenaar342337a2005-07-21 21:11:17 +000011224 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011225}
11226
Bram Moolenaar0d660222005-01-07 21:51:51 +000011227/*
11228 * "getbufvar()" function
11229 */
11230 static void
11231f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 typval_T *argvars;
11233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234{
11235 buf_T *buf;
11236 buf_T *save_curbuf;
11237 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011239 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11242 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011244 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011246 rettv->v_type = VAR_STRING;
11247 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248
11249 if (buf != NULL && varname != NULL)
11250 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011251 /* set curbuf to be our buf, temporarily */
11252 save_curbuf = curbuf;
11253 curbuf = buf;
11254
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011256 {
11257 if (get_option_tv(&varname, rettv, TRUE) == OK)
11258 done = TRUE;
11259 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011260 else if (STRCMP(varname, "changedtick") == 0)
11261 {
11262 rettv->v_type = VAR_NUMBER;
11263 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011264 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 else
11267 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011268 /* Look up the variable. */
11269 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11270 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11271 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011272 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011274 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011275 done = TRUE;
11276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011278
11279 /* restore previous notion of curbuf */
11280 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011281 }
11282
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011283 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11284 /* use the default value */
11285 copy_tv(&argvars[2], rettv);
11286
Bram Moolenaar0d660222005-01-07 21:51:51 +000011287 --emsg_off;
11288}
11289
11290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 * "getchar()" function
11292 */
11293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 typval_T *argvars;
11296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
11298 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011301 /* Position the cursor. Needed after a message that ends in a space. */
11302 windgoto(msg_row, msg_col);
11303
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 ++no_mapping;
11305 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011306 for (;;)
11307 {
11308 if (argvars[0].v_type == VAR_UNKNOWN)
11309 /* getchar(): blocking wait. */
11310 n = safe_vgetc();
11311 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11312 /* getchar(1): only check if char avail */
11313 n = vpeekc();
11314 else if (error || vpeekc() == NUL)
11315 /* illegal argument or getchar(0) and no char avail: return zero */
11316 n = 0;
11317 else
11318 /* getchar(0) and char avail: return char */
11319 n = safe_vgetc();
11320 if (n == K_IGNORE)
11321 continue;
11322 break;
11323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 --no_mapping;
11325 --allow_keys;
11326
Bram Moolenaar219b8702006-11-01 14:32:36 +000011327 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11328 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11329 vimvars[VV_MOUSE_COL].vv_nr = 0;
11330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 if (IS_SPECIAL(n) || mod_mask != 0)
11333 {
11334 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11335 int i = 0;
11336
11337 /* Turn a special key into three bytes, plus modifier. */
11338 if (mod_mask != 0)
11339 {
11340 temp[i++] = K_SPECIAL;
11341 temp[i++] = KS_MODIFIER;
11342 temp[i++] = mod_mask;
11343 }
11344 if (IS_SPECIAL(n))
11345 {
11346 temp[i++] = K_SPECIAL;
11347 temp[i++] = K_SECOND(n);
11348 temp[i++] = K_THIRD(n);
11349 }
11350#ifdef FEAT_MBYTE
11351 else if (has_mbyte)
11352 i += (*mb_char2bytes)(n, temp + i);
11353#endif
11354 else
11355 temp[i++] = n;
11356 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011359
11360#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011361 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011362 {
11363 int row = mouse_row;
11364 int col = mouse_col;
11365 win_T *win;
11366 linenr_T lnum;
11367# ifdef FEAT_WINDOWS
11368 win_T *wp;
11369# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011370 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011371
11372 if (row >= 0 && col >= 0)
11373 {
11374 /* Find the window at the mouse coordinates and compute the
11375 * text position. */
11376 win = mouse_find_win(&row, &col);
11377 (void)mouse_comp_pos(win, &row, &col, &lnum);
11378# ifdef FEAT_WINDOWS
11379 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011380 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011381# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011382 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011383 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11384 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11385 }
11386 }
11387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 }
11389}
11390
11391/*
11392 * "getcharmod()" function
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400}
11401
11402/*
11403 * "getcmdline()" function
11404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->v_type = VAR_STRING;
11411 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
11414/*
11415 * "getcmdpos()" function
11416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423}
11424
11425/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011426 * "getcmdtype()" function
11427 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011428 static void
11429f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011430 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011431 typval_T *rettv;
11432{
11433 rettv->v_type = VAR_STRING;
11434 rettv->vval.v_string = alloc(2);
11435 if (rettv->vval.v_string != NULL)
11436 {
11437 rettv->vval.v_string[0] = get_cmdline_type();
11438 rettv->vval.v_string[1] = NUL;
11439 }
11440}
11441
11442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 * "getcwd()" function
11444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011447 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011450 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011453 rettv->vval.v_string = NULL;
11454 cwd = alloc(MAXPATHL);
11455 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011457 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11458 {
11459 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011461 if (rettv->vval.v_string != NULL)
11462 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011464 }
11465 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 }
11467}
11468
11469/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011470 * "getfontname()" function
11471 */
11472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011475 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_STRING;
11478 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011479#ifdef FEAT_GUI
11480 if (gui.in_use)
11481 {
11482 GuiFont font;
11483 char_u *name = NULL;
11484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011486 {
11487 /* Get the "Normal" font. Either the name saved by
11488 * hl_set_font_name() or from the font ID. */
11489 font = gui.norm_font;
11490 name = hl_get_font_name();
11491 }
11492 else
11493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011495 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11496 return;
11497 font = gui_mch_get_font(name, FALSE);
11498 if (font == NOFONT)
11499 return; /* Invalid font name, return empty string. */
11500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011502 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011503 gui_mch_free_font(font);
11504 }
11505#endif
11506}
11507
11508/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011509 * "getfperm({fname})" function
11510 */
11511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011515{
11516 char_u *fname;
11517 struct stat st;
11518 char_u *perm = NULL;
11519 char_u flags[] = "rwx";
11520 int i;
11521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011525 if (mch_stat((char *)fname, &st) >= 0)
11526 {
11527 perm = vim_strsave((char_u *)"---------");
11528 if (perm != NULL)
11529 {
11530 for (i = 0; i < 9; i++)
11531 {
11532 if (st.st_mode & (1 << (8 - i)))
11533 perm[i] = flags[i % 3];
11534 }
11535 }
11536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011538}
11539
11540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 * "getfsize({fname})" function
11542 */
11543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011545 typval_T *argvars;
11546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547{
11548 char_u *fname;
11549 struct stat st;
11550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554
11555 if (mch_stat((char *)fname, &st) >= 0)
11556 {
11557 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011562
11563 /* non-perfect check for overflow */
11564 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11565 rettv->vval.v_number = -2;
11566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 }
11568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570}
11571
11572/*
11573 * "getftime({fname})" function
11574 */
11575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 typval_T *argvars;
11578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579{
11580 char_u *fname;
11581 struct stat st;
11582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584
11585 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589}
11590
11591/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011592 * "getftype({fname})" function
11593 */
11594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T *argvars;
11597 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011598{
11599 char_u *fname;
11600 struct stat st;
11601 char_u *type = NULL;
11602 char *t;
11603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011607 if (mch_lstat((char *)fname, &st) >= 0)
11608 {
11609#ifdef S_ISREG
11610 if (S_ISREG(st.st_mode))
11611 t = "file";
11612 else if (S_ISDIR(st.st_mode))
11613 t = "dir";
11614# ifdef S_ISLNK
11615 else if (S_ISLNK(st.st_mode))
11616 t = "link";
11617# endif
11618# ifdef S_ISBLK
11619 else if (S_ISBLK(st.st_mode))
11620 t = "bdev";
11621# endif
11622# ifdef S_ISCHR
11623 else if (S_ISCHR(st.st_mode))
11624 t = "cdev";
11625# endif
11626# ifdef S_ISFIFO
11627 else if (S_ISFIFO(st.st_mode))
11628 t = "fifo";
11629# endif
11630# ifdef S_ISSOCK
11631 else if (S_ISSOCK(st.st_mode))
11632 t = "fifo";
11633# endif
11634 else
11635 t = "other";
11636#else
11637# ifdef S_IFMT
11638 switch (st.st_mode & S_IFMT)
11639 {
11640 case S_IFREG: t = "file"; break;
11641 case S_IFDIR: t = "dir"; break;
11642# ifdef S_IFLNK
11643 case S_IFLNK: t = "link"; break;
11644# endif
11645# ifdef S_IFBLK
11646 case S_IFBLK: t = "bdev"; break;
11647# endif
11648# ifdef S_IFCHR
11649 case S_IFCHR: t = "cdev"; break;
11650# endif
11651# ifdef S_IFIFO
11652 case S_IFIFO: t = "fifo"; break;
11653# endif
11654# ifdef S_IFSOCK
11655 case S_IFSOCK: t = "socket"; break;
11656# endif
11657 default: t = "other";
11658 }
11659# else
11660 if (mch_isdir(fname))
11661 t = "dir";
11662 else
11663 t = "file";
11664# endif
11665#endif
11666 type = vim_strsave((char_u *)t);
11667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011669}
11670
11671/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011672 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673 */
11674 static void
11675f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 typval_T *argvars;
11677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678{
11679 linenr_T lnum;
11680 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682
11683 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011684 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011685 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011686 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011687 retlist = FALSE;
11688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011689 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011690 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011692 retlist = TRUE;
11693 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011694
Bram Moolenaar342337a2005-07-21 21:11:17 +000011695 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696}
11697
11698/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011699 * "getmatches()" function
11700 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011701 static void
11702f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011703 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011704 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011705{
11706#ifdef FEAT_SEARCH_EXTRA
11707 dict_T *dict;
11708 matchitem_T *cur = curwin->w_match_head;
11709
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011710 if (rettv_list_alloc(rettv) == OK)
11711 {
11712 while (cur != NULL)
11713 {
11714 dict = dict_alloc();
11715 if (dict == NULL)
11716 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011717 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11718 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11719 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11720 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11721 list_append_dict(rettv->vval.v_list, dict);
11722 cur = cur->next;
11723 }
11724 }
11725#endif
11726}
11727
11728/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011729 * "getpid()" function
11730 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011731 static void
11732f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011733 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011734 typval_T *rettv;
11735{
11736 rettv->vval.v_number = mch_get_pid();
11737}
11738
11739/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011740 * "getpos(string)" function
11741 */
11742 static void
11743f_getpos(argvars, rettv)
11744 typval_T *argvars;
11745 typval_T *rettv;
11746{
11747 pos_T *fp;
11748 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011749 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011750
11751 if (rettv_list_alloc(rettv) == OK)
11752 {
11753 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011754 fp = var2fpos(&argvars[0], TRUE, &fnum);
11755 if (fnum != -1)
11756 list_append_number(l, (varnumber_T)fnum);
11757 else
11758 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011759 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11760 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011761 list_append_number(l, (fp != NULL)
11762 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011763 : (varnumber_T)0);
11764 list_append_number(l,
11765#ifdef FEAT_VIRTUALEDIT
11766 (fp != NULL) ? (varnumber_T)fp->coladd :
11767#endif
11768 (varnumber_T)0);
11769 }
11770 else
11771 rettv->vval.v_number = FALSE;
11772}
11773
11774/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011775 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011776 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011777 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011778f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011779 typval_T *argvars UNUSED;
11780 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011781{
11782#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011783 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011784#endif
11785
Bram Moolenaar2641f772005-03-25 21:58:17 +000011786#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011787 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011788 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011789 wp = NULL;
11790 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11791 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011792 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011793 if (wp == NULL)
11794 return;
11795 }
11796
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011797 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011798 }
11799#endif
11800}
11801
11802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 * "getreg()" function
11804 */
11805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011807 typval_T *argvars;
11808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
11810 char_u *strregname;
11811 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011812 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011813 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011816 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 strregname = get_tv_string_chk(&argvars[0]);
11819 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011820 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011823 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11824 return_list = get_tv_number_chk(&argvars[2], &error);
11825 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011828 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011829
11830 if (error)
11831 return;
11832
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 regname = (strregname == NULL ? '"' : *strregname);
11834 if (regname == 0)
11835 regname = '"';
11836
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011837 if (return_list)
11838 {
11839 rettv->v_type = VAR_LIST;
11840 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11841 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11842 }
11843 else
11844 {
11845 rettv->v_type = VAR_STRING;
11846 rettv->vval.v_string = get_reg_contents(regname,
11847 arg2 ? GREG_EXPR_SRC : 0);
11848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849}
11850
11851/*
11852 * "getregtype()" function
11853 */
11854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *argvars;
11857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
11859 char_u *strregname;
11860 int regname;
11861 char_u buf[NUMBUFLEN + 2];
11862 long reglen = 0;
11863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011864 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 {
11866 strregname = get_tv_string_chk(&argvars[0]);
11867 if (strregname == NULL) /* type error; errmsg already given */
11868 {
11869 rettv->v_type = VAR_STRING;
11870 rettv->vval.v_string = NULL;
11871 return;
11872 }
11873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 else
11875 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877
11878 regname = (strregname == NULL ? '"' : *strregname);
11879 if (regname == 0)
11880 regname = '"';
11881
11882 buf[0] = NUL;
11883 buf[1] = NUL;
11884 switch (get_reg_type(regname, &reglen))
11885 {
11886 case MLINE: buf[0] = 'V'; break;
11887 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 case MBLOCK:
11889 buf[0] = Ctrl_V;
11890 sprintf((char *)buf + 1, "%ld", reglen + 1);
11891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 rettv->v_type = VAR_STRING;
11894 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011898 * "gettabvar()" function
11899 */
11900 static void
11901f_gettabvar(argvars, rettv)
11902 typval_T *argvars;
11903 typval_T *rettv;
11904{
11905 tabpage_T *tp;
11906 dictitem_T *v;
11907 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011908 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011909
11910 rettv->v_type = VAR_STRING;
11911 rettv->vval.v_string = NULL;
11912
11913 varname = get_tv_string_chk(&argvars[1]);
11914 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11915 if (tp != NULL && varname != NULL)
11916 {
11917 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011918 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011919 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011920 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011921 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011922 done = TRUE;
11923 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011924 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011925
11926 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011927 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011928}
11929
11930/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931 * "gettabwinvar()" function
11932 */
11933 static void
11934f_gettabwinvar(argvars, rettv)
11935 typval_T *argvars;
11936 typval_T *rettv;
11937{
11938 getwinvar(argvars, rettv, 1);
11939}
11940
11941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 * "getwinposx()" function
11943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011946 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950#ifdef FEAT_GUI
11951 if (gui.in_use)
11952 {
11953 int x, y;
11954
11955 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958#endif
11959}
11960
11961/*
11962 * "getwinposy()" function
11963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970#ifdef FEAT_GUI
11971 if (gui.in_use)
11972 {
11973 int x, y;
11974
11975 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 }
11978#endif
11979}
11980
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011981/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011982 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011983 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011984 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011985find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011986 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011987 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011988{
11989#ifdef FEAT_WINDOWS
11990 win_T *wp;
11991#endif
11992 int nr;
11993
11994 nr = get_tv_number_chk(vp, NULL);
11995
11996#ifdef FEAT_WINDOWS
11997 if (nr < 0)
11998 return NULL;
11999 if (nr == 0)
12000 return curwin;
12001
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012002 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12003 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012004 if (--nr <= 0)
12005 break;
12006 return wp;
12007#else
12008 if (nr == 0 || nr == 1)
12009 return curwin;
12010 return NULL;
12011#endif
12012}
12013
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014/*
12015 * "getwinvar()" function
12016 */
12017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012019 typval_T *argvars;
12020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012022 getwinvar(argvars, rettv, 0);
12023}
12024
12025/*
12026 * getwinvar() and gettabwinvar()
12027 */
12028 static void
12029getwinvar(argvars, rettv, off)
12030 typval_T *argvars;
12031 typval_T *rettv;
12032 int off; /* 1 for gettabwinvar() */
12033{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 win_T *win, *oldcurwin;
12035 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012036 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012037 tabpage_T *tp = NULL;
12038 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012039 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012041#ifdef FEAT_WINDOWS
12042 if (off == 1)
12043 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12044 else
12045 tp = curtab;
12046#endif
12047 win = find_win_by_nr(&argvars[off], tp);
12048 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012051 rettv->v_type = VAR_STRING;
12052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
12054 if (win != NULL && varname != NULL)
12055 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012056 /* Set curwin to be our win, temporarily. Also set the tabpage,
12057 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012058 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012059
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012061 {
12062 if (get_option_tv(&varname, rettv, 1) == OK)
12063 done = TRUE;
12064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 else
12066 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012067 /* Look up the variable. */
12068 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12069 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012073 done = TRUE;
12074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012076
12077 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012078 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 }
12080
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012081 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12082 /* use the default return value */
12083 copy_tv(&argvars[off + 2], rettv);
12084
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 --emsg_off;
12086}
12087
12088/*
12089 * "glob()" function
12090 */
12091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *argvars;
12094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012096 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012098 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012100 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012101 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012102 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012103 if (argvars[1].v_type != VAR_UNKNOWN)
12104 {
12105 if (get_tv_number_chk(&argvars[1], &error))
12106 options |= WILD_KEEP_ALL;
12107 if (argvars[2].v_type != VAR_UNKNOWN
12108 && get_tv_number_chk(&argvars[2], &error))
12109 {
12110 rettv->v_type = VAR_LIST;
12111 rettv->vval.v_list = NULL;
12112 }
12113 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012114 if (!error)
12115 {
12116 ExpandInit(&xpc);
12117 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012118 if (p_wic)
12119 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012120 if (rettv->v_type == VAR_STRING)
12121 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012122 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012123 else if (rettv_list_alloc(rettv) != FAIL)
12124 {
12125 int i;
12126
12127 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12128 NULL, options, WILD_ALL_KEEP);
12129 for (i = 0; i < xpc.xp_numfiles; i++)
12130 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12131
12132 ExpandCleanup(&xpc);
12133 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012134 }
12135 else
12136 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137}
12138
12139/*
12140 * "globpath()" function
12141 */
12142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012144 typval_T *argvars;
12145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012147 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012150 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012152 /* When the optional second argument is non-zero, don't remove matches
12153 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12154 if (argvars[2].v_type != VAR_UNKNOWN
12155 && get_tv_number_chk(&argvars[2], &error))
12156 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012158 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012159 rettv->vval.v_string = NULL;
12160 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012161 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12162 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163}
12164
12165/*
12166 * "has()" function
12167 */
12168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012170 typval_T *argvars;
12171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
12173 int i;
12174 char_u *name;
12175 int n = FALSE;
12176 static char *(has_list[]) =
12177 {
12178#ifdef AMIGA
12179 "amiga",
12180# ifdef FEAT_ARP
12181 "arp",
12182# endif
12183#endif
12184#ifdef __BEOS__
12185 "beos",
12186#endif
12187#ifdef MSDOS
12188# ifdef DJGPP
12189 "dos32",
12190# else
12191 "dos16",
12192# endif
12193#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012194#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 "mac",
12196#endif
12197#if defined(MACOS_X_UNIX)
12198 "macunix",
12199#endif
12200#ifdef OS2
12201 "os2",
12202#endif
12203#ifdef __QNX__
12204 "qnx",
12205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#ifdef UNIX
12207 "unix",
12208#endif
12209#ifdef VMS
12210 "vms",
12211#endif
12212#ifdef WIN16
12213 "win16",
12214#endif
12215#ifdef WIN32
12216 "win32",
12217#endif
12218#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12219 "win32unix",
12220#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012221#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 "win64",
12223#endif
12224#ifdef EBCDIC
12225 "ebcdic",
12226#endif
12227#ifndef CASE_INSENSITIVE_FILENAME
12228 "fname_case",
12229#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012230#ifdef HAVE_ACL
12231 "acl",
12232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233#ifdef FEAT_ARABIC
12234 "arabic",
12235#endif
12236#ifdef FEAT_AUTOCMD
12237 "autocmd",
12238#endif
12239#ifdef FEAT_BEVAL
12240 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012241# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12242 "balloon_multiline",
12243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244#endif
12245#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12246 "builtin_terms",
12247# ifdef ALL_BUILTIN_TCAPS
12248 "all_builtin_terms",
12249# endif
12250#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012251#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12252 || defined(FEAT_GUI_W32) \
12253 || defined(FEAT_GUI_MOTIF))
12254 "browsefilter",
12255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256#ifdef FEAT_BYTEOFF
12257 "byte_offset",
12258#endif
12259#ifdef FEAT_CINDENT
12260 "cindent",
12261#endif
12262#ifdef FEAT_CLIENTSERVER
12263 "clientserver",
12264#endif
12265#ifdef FEAT_CLIPBOARD
12266 "clipboard",
12267#endif
12268#ifdef FEAT_CMDL_COMPL
12269 "cmdline_compl",
12270#endif
12271#ifdef FEAT_CMDHIST
12272 "cmdline_hist",
12273#endif
12274#ifdef FEAT_COMMENTS
12275 "comments",
12276#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012277#ifdef FEAT_CONCEAL
12278 "conceal",
12279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280#ifdef FEAT_CRYPT
12281 "cryptv",
12282#endif
12283#ifdef FEAT_CSCOPE
12284 "cscope",
12285#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012286#ifdef FEAT_CURSORBIND
12287 "cursorbind",
12288#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012289#ifdef CURSOR_SHAPE
12290 "cursorshape",
12291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef DEBUG
12293 "debug",
12294#endif
12295#ifdef FEAT_CON_DIALOG
12296 "dialog_con",
12297#endif
12298#ifdef FEAT_GUI_DIALOG
12299 "dialog_gui",
12300#endif
12301#ifdef FEAT_DIFF
12302 "diff",
12303#endif
12304#ifdef FEAT_DIGRAPHS
12305 "digraphs",
12306#endif
12307#ifdef FEAT_DND
12308 "dnd",
12309#endif
12310#ifdef FEAT_EMACS_TAGS
12311 "emacs_tags",
12312#endif
12313 "eval", /* always present, of course! */
12314#ifdef FEAT_EX_EXTRA
12315 "ex_extra",
12316#endif
12317#ifdef FEAT_SEARCH_EXTRA
12318 "extra_search",
12319#endif
12320#ifdef FEAT_FKMAP
12321 "farsi",
12322#endif
12323#ifdef FEAT_SEARCHPATH
12324 "file_in_path",
12325#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012326#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012327 "filterpipe",
12328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_FIND_ID
12330 "find_in_path",
12331#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012332#ifdef FEAT_FLOAT
12333 "float",
12334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#ifdef FEAT_FOLDING
12336 "folding",
12337#endif
12338#ifdef FEAT_FOOTER
12339 "footer",
12340#endif
12341#if !defined(USE_SYSTEM) && defined(UNIX)
12342 "fork",
12343#endif
12344#ifdef FEAT_GETTEXT
12345 "gettext",
12346#endif
12347#ifdef FEAT_GUI
12348 "gui",
12349#endif
12350#ifdef FEAT_GUI_ATHENA
12351# ifdef FEAT_GUI_NEXTAW
12352 "gui_neXtaw",
12353# else
12354 "gui_athena",
12355# endif
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef FEAT_GUI_GTK
12358 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012361#ifdef FEAT_GUI_GNOME
12362 "gui_gnome",
12363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364#ifdef FEAT_GUI_MAC
12365 "gui_mac",
12366#endif
12367#ifdef FEAT_GUI_MOTIF
12368 "gui_motif",
12369#endif
12370#ifdef FEAT_GUI_PHOTON
12371 "gui_photon",
12372#endif
12373#ifdef FEAT_GUI_W16
12374 "gui_win16",
12375#endif
12376#ifdef FEAT_GUI_W32
12377 "gui_win32",
12378#endif
12379#ifdef FEAT_HANGULIN
12380 "hangul_input",
12381#endif
12382#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12383 "iconv",
12384#endif
12385#ifdef FEAT_INS_EXPAND
12386 "insert_expand",
12387#endif
12388#ifdef FEAT_JUMPLIST
12389 "jumplist",
12390#endif
12391#ifdef FEAT_KEYMAP
12392 "keymap",
12393#endif
12394#ifdef FEAT_LANGMAP
12395 "langmap",
12396#endif
12397#ifdef FEAT_LIBCALL
12398 "libcall",
12399#endif
12400#ifdef FEAT_LINEBREAK
12401 "linebreak",
12402#endif
12403#ifdef FEAT_LISP
12404 "lispindent",
12405#endif
12406#ifdef FEAT_LISTCMDS
12407 "listcmds",
12408#endif
12409#ifdef FEAT_LOCALMAP
12410 "localmap",
12411#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012412#ifdef FEAT_LUA
12413# ifndef DYNAMIC_LUA
12414 "lua",
12415# endif
12416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417#ifdef FEAT_MENU
12418 "menu",
12419#endif
12420#ifdef FEAT_SESSION
12421 "mksession",
12422#endif
12423#ifdef FEAT_MODIFY_FNAME
12424 "modify_fname",
12425#endif
12426#ifdef FEAT_MOUSE
12427 "mouse",
12428#endif
12429#ifdef FEAT_MOUSESHAPE
12430 "mouseshape",
12431#endif
12432#if defined(UNIX) || defined(VMS)
12433# ifdef FEAT_MOUSE_DEC
12434 "mouse_dec",
12435# endif
12436# ifdef FEAT_MOUSE_GPM
12437 "mouse_gpm",
12438# endif
12439# ifdef FEAT_MOUSE_JSB
12440 "mouse_jsbterm",
12441# endif
12442# ifdef FEAT_MOUSE_NET
12443 "mouse_netterm",
12444# endif
12445# ifdef FEAT_MOUSE_PTERM
12446 "mouse_pterm",
12447# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012448# ifdef FEAT_MOUSE_SGR
12449 "mouse_sgr",
12450# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012451# ifdef FEAT_SYSMOUSE
12452 "mouse_sysmouse",
12453# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012454# ifdef FEAT_MOUSE_URXVT
12455 "mouse_urxvt",
12456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457# ifdef FEAT_MOUSE_XTERM
12458 "mouse_xterm",
12459# endif
12460#endif
12461#ifdef FEAT_MBYTE
12462 "multi_byte",
12463#endif
12464#ifdef FEAT_MBYTE_IME
12465 "multi_byte_ime",
12466#endif
12467#ifdef FEAT_MULTI_LANG
12468 "multi_lang",
12469#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012470#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012471#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012472 "mzscheme",
12473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475#ifdef FEAT_OLE
12476 "ole",
12477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478#ifdef FEAT_PATH_EXTRA
12479 "path_extra",
12480#endif
12481#ifdef FEAT_PERL
12482#ifndef DYNAMIC_PERL
12483 "perl",
12484#endif
12485#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012486#ifdef FEAT_PERSISTENT_UNDO
12487 "persistent_undo",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_PYTHON
12490#ifndef DYNAMIC_PYTHON
12491 "python",
12492#endif
12493#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012494#ifdef FEAT_PYTHON3
12495#ifndef DYNAMIC_PYTHON3
12496 "python3",
12497#endif
12498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499#ifdef FEAT_POSTSCRIPT
12500 "postscript",
12501#endif
12502#ifdef FEAT_PRINTER
12503 "printer",
12504#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012505#ifdef FEAT_PROFILE
12506 "profile",
12507#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012508#ifdef FEAT_RELTIME
12509 "reltime",
12510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511#ifdef FEAT_QUICKFIX
12512 "quickfix",
12513#endif
12514#ifdef FEAT_RIGHTLEFT
12515 "rightleft",
12516#endif
12517#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12518 "ruby",
12519#endif
12520#ifdef FEAT_SCROLLBIND
12521 "scrollbind",
12522#endif
12523#ifdef FEAT_CMDL_INFO
12524 "showcmd",
12525 "cmdline_info",
12526#endif
12527#ifdef FEAT_SIGNS
12528 "signs",
12529#endif
12530#ifdef FEAT_SMARTINDENT
12531 "smartindent",
12532#endif
12533#ifdef FEAT_SNIFF
12534 "sniff",
12535#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012536#ifdef STARTUPTIME
12537 "startuptime",
12538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539#ifdef FEAT_STL_OPT
12540 "statusline",
12541#endif
12542#ifdef FEAT_SUN_WORKSHOP
12543 "sun_workshop",
12544#endif
12545#ifdef FEAT_NETBEANS_INTG
12546 "netbeans_intg",
12547#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012548#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012549 "spell",
12550#endif
12551#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 "syntax",
12553#endif
12554#if defined(USE_SYSTEM) || !defined(UNIX)
12555 "system",
12556#endif
12557#ifdef FEAT_TAG_BINS
12558 "tag_binary",
12559#endif
12560#ifdef FEAT_TAG_OLDSTATIC
12561 "tag_old_static",
12562#endif
12563#ifdef FEAT_TAG_ANYWHITE
12564 "tag_any_white",
12565#endif
12566#ifdef FEAT_TCL
12567# ifndef DYNAMIC_TCL
12568 "tcl",
12569# endif
12570#endif
12571#ifdef TERMINFO
12572 "terminfo",
12573#endif
12574#ifdef FEAT_TERMRESPONSE
12575 "termresponse",
12576#endif
12577#ifdef FEAT_TEXTOBJ
12578 "textobjects",
12579#endif
12580#ifdef HAVE_TGETENT
12581 "tgetent",
12582#endif
12583#ifdef FEAT_TITLE
12584 "title",
12585#endif
12586#ifdef FEAT_TOOLBAR
12587 "toolbar",
12588#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012589#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12590 "unnamedplus",
12591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef FEAT_USR_CMDS
12593 "user-commands", /* was accidentally included in 5.4 */
12594 "user_commands",
12595#endif
12596#ifdef FEAT_VIMINFO
12597 "viminfo",
12598#endif
12599#ifdef FEAT_VERTSPLIT
12600 "vertsplit",
12601#endif
12602#ifdef FEAT_VIRTUALEDIT
12603 "virtualedit",
12604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606#ifdef FEAT_VISUALEXTRA
12607 "visualextra",
12608#endif
12609#ifdef FEAT_VREPLACE
12610 "vreplace",
12611#endif
12612#ifdef FEAT_WILDIGN
12613 "wildignore",
12614#endif
12615#ifdef FEAT_WILDMENU
12616 "wildmenu",
12617#endif
12618#ifdef FEAT_WINDOWS
12619 "windows",
12620#endif
12621#ifdef FEAT_WAK
12622 "winaltkeys",
12623#endif
12624#ifdef FEAT_WRITEBACKUP
12625 "writebackup",
12626#endif
12627#ifdef FEAT_XIM
12628 "xim",
12629#endif
12630#ifdef FEAT_XFONTSET
12631 "xfontset",
12632#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012633#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012634 "xpm",
12635 "xpm_w32", /* for backward compatibility */
12636#else
12637# if defined(HAVE_XPM)
12638 "xpm",
12639# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef USE_XSMP
12642 "xsmp",
12643#endif
12644#ifdef USE_XSMP_INTERACT
12645 "xsmp_interact",
12646#endif
12647#ifdef FEAT_XCLIPBOARD
12648 "xterm_clipboard",
12649#endif
12650#ifdef FEAT_XTERM_SAVE
12651 "xterm_save",
12652#endif
12653#if defined(UNIX) && defined(FEAT_X11)
12654 "X11",
12655#endif
12656 NULL
12657 };
12658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 for (i = 0; has_list[i] != NULL; ++i)
12661 if (STRICMP(name, has_list[i]) == 0)
12662 {
12663 n = TRUE;
12664 break;
12665 }
12666
12667 if (n == FALSE)
12668 {
12669 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012670 {
12671 if (name[5] == '-'
12672 && STRLEN(name) > 11
12673 && vim_isdigit(name[6])
12674 && vim_isdigit(name[8])
12675 && vim_isdigit(name[10]))
12676 {
12677 int major = atoi((char *)name + 6);
12678 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012679
12680 /* Expect "patch-9.9.01234". */
12681 n = (major < VIM_VERSION_MAJOR
12682 || (major == VIM_VERSION_MAJOR
12683 && (minor < VIM_VERSION_MINOR
12684 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012685 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012686 }
12687 else
12688 n = has_patch(atoi((char *)name + 5));
12689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 else if (STRICMP(name, "vim_starting") == 0)
12691 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012692#ifdef FEAT_MBYTE
12693 else if (STRICMP(name, "multi_byte_encoding") == 0)
12694 n = has_mbyte;
12695#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012696#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12697 else if (STRICMP(name, "balloon_multiline") == 0)
12698 n = multiline_balloon_available();
12699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700#ifdef DYNAMIC_TCL
12701 else if (STRICMP(name, "tcl") == 0)
12702 n = tcl_enabled(FALSE);
12703#endif
12704#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12705 else if (STRICMP(name, "iconv") == 0)
12706 n = iconv_enabled(FALSE);
12707#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012708#ifdef DYNAMIC_LUA
12709 else if (STRICMP(name, "lua") == 0)
12710 n = lua_enabled(FALSE);
12711#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012712#ifdef DYNAMIC_MZSCHEME
12713 else if (STRICMP(name, "mzscheme") == 0)
12714 n = mzscheme_enabled(FALSE);
12715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716#ifdef DYNAMIC_RUBY
12717 else if (STRICMP(name, "ruby") == 0)
12718 n = ruby_enabled(FALSE);
12719#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012720#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721#ifdef DYNAMIC_PYTHON
12722 else if (STRICMP(name, "python") == 0)
12723 n = python_enabled(FALSE);
12724#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012725#endif
12726#ifdef FEAT_PYTHON3
12727#ifdef DYNAMIC_PYTHON3
12728 else if (STRICMP(name, "python3") == 0)
12729 n = python3_enabled(FALSE);
12730#endif
12731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732#ifdef DYNAMIC_PERL
12733 else if (STRICMP(name, "perl") == 0)
12734 n = perl_enabled(FALSE);
12735#endif
12736#ifdef FEAT_GUI
12737 else if (STRICMP(name, "gui_running") == 0)
12738 n = (gui.in_use || gui.starting);
12739# ifdef FEAT_GUI_W32
12740 else if (STRICMP(name, "gui_win32s") == 0)
12741 n = gui_is_win32s();
12742# endif
12743# ifdef FEAT_BROWSE
12744 else if (STRICMP(name, "browse") == 0)
12745 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12746# endif
12747#endif
12748#ifdef FEAT_SYN_HL
12749 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012750 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751#endif
12752#if defined(WIN3264)
12753 else if (STRICMP(name, "win95") == 0)
12754 n = mch_windows95();
12755#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012756#ifdef FEAT_NETBEANS_INTG
12757 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012758 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 }
12761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763}
12764
12765/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012766 * "has_key()" function
12767 */
12768 static void
12769f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012770 typval_T *argvars;
12771 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012772{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012773 if (argvars[0].v_type != VAR_DICT)
12774 {
12775 EMSG(_(e_dictreq));
12776 return;
12777 }
12778 if (argvars[0].vval.v_dict == NULL)
12779 return;
12780
12781 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012782 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012783}
12784
12785/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012786 * "haslocaldir()" function
12787 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012788 static void
12789f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012790 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012791 typval_T *rettv;
12792{
12793 rettv->vval.v_number = (curwin->w_localdir != NULL);
12794}
12795
12796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 * "hasmapto()" function
12798 */
12799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012801 typval_T *argvars;
12802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803{
12804 char_u *name;
12805 char_u *mode;
12806 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012807 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 mode = (char_u *)"nvo";
12812 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012815 if (argvars[2].v_type != VAR_UNKNOWN)
12816 abbr = get_tv_number(&argvars[2]);
12817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818
Bram Moolenaar2c932302006-03-18 21:42:09 +000012819 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823}
12824
12825/*
12826 * "histadd()" function
12827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832{
12833#ifdef FEAT_CMDHIST
12834 int histype;
12835 char_u *str;
12836 char_u buf[NUMBUFLEN];
12837#endif
12838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 if (check_restricted() || check_secure())
12841 return;
12842#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12844 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 if (histype >= 0)
12846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 if (*str != NUL)
12849 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012850 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 return;
12854 }
12855 }
12856#endif
12857}
12858
12859/*
12860 * "histdel()" function
12861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012864 typval_T *argvars UNUSED;
12865 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
12867#ifdef FEAT_CMDHIST
12868 int n;
12869 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012870 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012872 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12873 if (str == NULL)
12874 n = 0;
12875 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012877 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012878 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 else
12883 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012884 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 get_tv_string_buf(&argvars[1], buf));
12886 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887#endif
12888}
12889
12890/*
12891 * "histget()" function
12892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897{
12898#ifdef FEAT_CMDHIST
12899 int type;
12900 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12904 if (str == NULL)
12905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 {
12908 type = get_histtype(str);
12909 if (argvars[1].v_type == VAR_UNKNOWN)
12910 idx = get_history_idx(type);
12911 else
12912 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12913 /* -1 on type error */
12914 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920}
12921
12922/*
12923 * "histnr()" function
12924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929{
12930 int i;
12931
12932#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 char_u *history = get_tv_string_chk(&argvars[0]);
12934
12935 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 if (i >= HIST_CMD && i < HIST_COUNT)
12937 i = get_history_idx(i);
12938 else
12939#endif
12940 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942}
12943
12944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 * "highlightID(name)" function
12946 */
12947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 typval_T *argvars;
12950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953}
12954
12955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 * "highlight_exists()" function
12957 */
12958 static void
12959f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 typval_T *argvars;
12961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012962{
12963 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12964}
12965
12966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 * "hostname()" function
12968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973{
12974 char_u hostname[256];
12975
12976 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv->v_type = VAR_STRING;
12978 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979}
12980
12981/*
12982 * iconv() function
12983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988{
12989#ifdef FEAT_MBYTE
12990 char_u buf1[NUMBUFLEN];
12991 char_u buf2[NUMBUFLEN];
12992 char_u *from, *to, *str;
12993 vimconv_T vimconv;
12994#endif
12995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->v_type = VAR_STRING;
12997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998
12999#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 str = get_tv_string(&argvars[0]);
13001 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13002 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 vimconv.vc_type = CONV_NONE;
13004 convert_setup(&vimconv, from, to);
13005
13006 /* If the encodings are equal, no conversion needed. */
13007 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013008 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011
13012 convert_setup(&vimconv, NULL, NULL);
13013 vim_free(from);
13014 vim_free(to);
13015#endif
13016}
13017
13018/*
13019 * "indent()" function
13020 */
13021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
13026 linenr_T lnum;
13027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033}
13034
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013035/*
13036 * "index()" function
13037 */
13038 static void
13039f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 typval_T *argvars;
13041 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013042{
Bram Moolenaar33570922005-01-25 22:26:29 +000013043 list_T *l;
13044 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013045 long idx = 0;
13046 int ic = FALSE;
13047
13048 rettv->vval.v_number = -1;
13049 if (argvars[0].v_type != VAR_LIST)
13050 {
13051 EMSG(_(e_listreq));
13052 return;
13053 }
13054 l = argvars[0].vval.v_list;
13055 if (l != NULL)
13056 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013057 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 int error = FALSE;
13061
Bram Moolenaar758711c2005-02-02 23:11:38 +000013062 /* Start at specified item. Use the cached index that list_find()
13063 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013065 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013066 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 ic = get_tv_number_chk(&argvars[3], &error);
13068 if (error)
13069 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013070 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013071
Bram Moolenaar758711c2005-02-02 23:11:38 +000013072 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013073 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013074 {
13075 rettv->vval.v_number = idx;
13076 break;
13077 }
13078 }
13079}
13080
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081static int inputsecret_flag = 0;
13082
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013083static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13084
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013086 * This function is used by f_input() and f_inputdialog() functions. The third
13087 * argument to f_input() specifies the type of completion to use at the
13088 * prompt. The third argument to f_inputdialog() specifies the value to return
13089 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 */
13091 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013092get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *argvars;
13094 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013095 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098 char_u *p = NULL;
13099 int c;
13100 char_u buf[NUMBUFLEN];
13101 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013103 int xp_type = EXPAND_NOTHING;
13104 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108
13109#ifdef NO_CONSOLE_INPUT
13110 /* While starting up, there is no place to enter text. */
13111 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113#endif
13114
13115 cmd_silent = FALSE; /* Want to see the prompt. */
13116 if (prompt != NULL)
13117 {
13118 /* Only the part of the message after the last NL is considered as
13119 * prompt for the command line */
13120 p = vim_strrchr(prompt, '\n');
13121 if (p == NULL)
13122 p = prompt;
13123 else
13124 {
13125 ++p;
13126 c = *p;
13127 *p = NUL;
13128 msg_start();
13129 msg_clr_eos();
13130 msg_puts_attr(prompt, echo_attr);
13131 msg_didout = FALSE;
13132 msg_starthere();
13133 *p = c;
13134 }
13135 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013137 if (argvars[1].v_type != VAR_UNKNOWN)
13138 {
13139 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13140 if (defstr != NULL)
13141 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013143 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013144 {
13145 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013146 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013147 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013148
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013149 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013150 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013151
Bram Moolenaar4463f292005-09-25 22:20:24 +000013152 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13153 if (xp_name == NULL)
13154 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013156 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013157
Bram Moolenaar4463f292005-09-25 22:20:24 +000013158 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13159 &xp_arg) == FAIL)
13160 return;
13161 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013162 }
13163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013165 {
13166# ifdef FEAT_EX_EXTRA
13167 int save_ex_normal_busy = ex_normal_busy;
13168 ex_normal_busy = 0;
13169# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013171 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13172 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013173# ifdef FEAT_EX_EXTRA
13174 ex_normal_busy = save_ex_normal_busy;
13175# endif
13176 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013177 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013178 && argvars[1].v_type != VAR_UNKNOWN
13179 && argvars[2].v_type != VAR_UNKNOWN)
13180 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13181 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013182
13183 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 /* since the user typed this, no need to wait for return */
13186 need_wait_return = FALSE;
13187 msg_didout = FALSE;
13188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 cmd_silent = cmd_silent_save;
13190}
13191
13192/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013193 * "input()" function
13194 * Also handles inputsecret() when inputsecret is set.
13195 */
13196 static void
13197f_input(argvars, rettv)
13198 typval_T *argvars;
13199 typval_T *rettv;
13200{
13201 get_user_input(argvars, rettv, FALSE);
13202}
13203
13204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 * "inputdialog()" function
13206 */
13207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013209 typval_T *argvars;
13210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211{
13212#if defined(FEAT_GUI_TEXTDIALOG)
13213 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13214 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13215 {
13216 char_u *message;
13217 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 message = get_tv_string_chk(&argvars[0]);
13221 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013222 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013223 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 else
13225 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 if (message != NULL && defstr != NULL
13227 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013228 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 else
13231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 if (message != NULL && defstr != NULL
13233 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_string = vim_strsave(
13236 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 }
13242 else
13243#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013244 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245}
13246
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013247/*
13248 * "inputlist()" function
13249 */
13250 static void
13251f_inputlist(argvars, rettv)
13252 typval_T *argvars;
13253 typval_T *rettv;
13254{
13255 listitem_T *li;
13256 int selected;
13257 int mouse_used;
13258
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013259#ifdef NO_CONSOLE_INPUT
13260 /* While starting up, there is no place to enter text. */
13261 if (no_console_input())
13262 return;
13263#endif
13264 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13265 {
13266 EMSG2(_(e_listarg), "inputlist()");
13267 return;
13268 }
13269
13270 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013271 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013272 lines_left = Rows; /* avoid more prompt */
13273 msg_scroll = TRUE;
13274 msg_clr_eos();
13275
13276 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13277 {
13278 msg_puts(get_tv_string(&li->li_tv));
13279 msg_putchar('\n');
13280 }
13281
13282 /* Ask for choice. */
13283 selected = prompt_for_number(&mouse_used);
13284 if (mouse_used)
13285 selected -= lines_left;
13286
13287 rettv->vval.v_number = selected;
13288}
13289
13290
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13292
13293/*
13294 * "inputrestore()" function
13295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013298 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300{
13301 if (ga_userinput.ga_len > 0)
13302 {
13303 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13305 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013306 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 }
13308 else if (p_verbose > 1)
13309 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013310 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 }
13313}
13314
13315/*
13316 * "inputsave()" function
13317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013323 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 if (ga_grow(&ga_userinput, 1) == OK)
13325 {
13326 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13327 + ga_userinput.ga_len);
13328 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013329 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 }
13331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333}
13334
13335/*
13336 * "inputsecret()" function
13337 */
13338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 typval_T *argvars;
13341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
13343 ++cmdline_star;
13344 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 --cmdline_star;
13347 --inputsecret_flag;
13348}
13349
13350/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351 * "insert()" function
13352 */
13353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *argvars;
13356 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013357{
13358 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 listitem_T *item;
13360 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013361 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362
13363 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013364 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013365 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013366 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013367 {
13368 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013369 before = get_tv_number_chk(&argvars[2], &error);
13370 if (error)
13371 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013372
Bram Moolenaar758711c2005-02-02 23:11:38 +000013373 if (before == l->lv_len)
13374 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013375 else
13376 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013377 item = list_find(l, before);
13378 if (item == NULL)
13379 {
13380 EMSGN(_(e_listidx), before);
13381 l = NULL;
13382 }
13383 }
13384 if (l != NULL)
13385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013386 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013387 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013388 }
13389 }
13390}
13391
13392/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013393 * "invert(expr)" function
13394 */
13395 static void
13396f_invert(argvars, rettv)
13397 typval_T *argvars;
13398 typval_T *rettv;
13399{
13400 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13401}
13402
13403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 * "isdirectory()" function
13405 */
13406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013408 typval_T *argvars;
13409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412}
13413
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013414/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013415 * "islocked()" function
13416 */
13417 static void
13418f_islocked(argvars, rettv)
13419 typval_T *argvars;
13420 typval_T *rettv;
13421{
13422 lval_T lv;
13423 char_u *end;
13424 dictitem_T *di;
13425
13426 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013427 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13428 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013429 if (end != NULL && lv.ll_name != NULL)
13430 {
13431 if (*end != NUL)
13432 EMSG(_(e_trailing));
13433 else
13434 {
13435 if (lv.ll_tv == NULL)
13436 {
13437 if (check_changedtick(lv.ll_name))
13438 rettv->vval.v_number = 1; /* always locked */
13439 else
13440 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013441 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013442 if (di != NULL)
13443 {
13444 /* Consider a variable locked when:
13445 * 1. the variable itself is locked
13446 * 2. the value of the variable is locked.
13447 * 3. the List or Dict value is locked.
13448 */
13449 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13450 || tv_islocked(&di->di_tv));
13451 }
13452 }
13453 }
13454 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013455 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013456 else if (lv.ll_newkey != NULL)
13457 EMSG2(_(e_dictkey), lv.ll_newkey);
13458 else if (lv.ll_list != NULL)
13459 /* List item. */
13460 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13461 else
13462 /* Dictionary item. */
13463 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13464 }
13465 }
13466
13467 clear_lval(&lv);
13468}
13469
Bram Moolenaar33570922005-01-25 22:26:29 +000013470static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013471
13472/*
13473 * Turn a dict into a list:
13474 * "what" == 0: list of keys
13475 * "what" == 1: list of values
13476 * "what" == 2: list of items
13477 */
13478 static void
13479dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 typval_T *argvars;
13481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013482 int what;
13483{
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 list_T *l2;
13485 dictitem_T *di;
13486 hashitem_T *hi;
13487 listitem_T *li;
13488 listitem_T *li2;
13489 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013490 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013491
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492 if (argvars[0].v_type != VAR_DICT)
13493 {
13494 EMSG(_(e_dictreq));
13495 return;
13496 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013497 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013498 return;
13499
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013500 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013501 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013502
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013503 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013506 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013508 --todo;
13509 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013510
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013511 li = listitem_alloc();
13512 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013513 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013514 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013516 if (what == 0)
13517 {
13518 /* keys() */
13519 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013520 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013521 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13522 }
13523 else if (what == 1)
13524 {
13525 /* values() */
13526 copy_tv(&di->di_tv, &li->li_tv);
13527 }
13528 else
13529 {
13530 /* items() */
13531 l2 = list_alloc();
13532 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013533 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013534 li->li_tv.vval.v_list = l2;
13535 if (l2 == NULL)
13536 break;
13537 ++l2->lv_refcount;
13538
13539 li2 = listitem_alloc();
13540 if (li2 == NULL)
13541 break;
13542 list_append(l2, li2);
13543 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013544 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013545 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13546
13547 li2 = listitem_alloc();
13548 if (li2 == NULL)
13549 break;
13550 list_append(l2, li2);
13551 copy_tv(&di->di_tv, &li2->li_tv);
13552 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013553 }
13554 }
13555}
13556
13557/*
13558 * "items(dict)" function
13559 */
13560 static void
13561f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013562 typval_T *argvars;
13563 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013564{
13565 dict_list(argvars, rettv, 2);
13566}
13567
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569 * "join()" function
13570 */
13571 static void
13572f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *argvars;
13574 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013575{
13576 garray_T ga;
13577 char_u *sep;
13578
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013579 if (argvars[0].v_type != VAR_LIST)
13580 {
13581 EMSG(_(e_listreq));
13582 return;
13583 }
13584 if (argvars[0].vval.v_list == NULL)
13585 return;
13586 if (argvars[1].v_type == VAR_UNKNOWN)
13587 sep = (char_u *)" ";
13588 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013590
13591 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592
13593 if (sep != NULL)
13594 {
13595 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013596 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 ga_append(&ga, NUL);
13598 rettv->vval.v_string = (char_u *)ga.ga_data;
13599 }
13600 else
13601 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013602}
13603
13604/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013605 * "keys()" function
13606 */
13607 static void
13608f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013609 typval_T *argvars;
13610 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013611{
13612 dict_list(argvars, rettv, 0);
13613}
13614
13615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 * "last_buffer_nr()" function.
13617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
13623 int n = 0;
13624 buf_T *buf;
13625
13626 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13627 if (n < buf->b_fnum)
13628 n = buf->b_fnum;
13629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013631}
13632
13633/*
13634 * "len()" function
13635 */
13636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013640{
13641 switch (argvars[0].v_type)
13642 {
13643 case VAR_STRING:
13644 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_number = (varnumber_T)STRLEN(
13646 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013647 break;
13648 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013650 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013651 case VAR_DICT:
13652 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13653 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013654 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013655 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013656 break;
13657 }
13658}
13659
Bram Moolenaar33570922005-01-25 22:26:29 +000013660static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013661
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013666 int type;
13667{
13668#ifdef FEAT_LIBCALL
13669 char_u *string_in;
13670 char_u **string_result;
13671 int nr_result;
13672#endif
13673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013675 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013677
13678 if (check_restricted() || check_secure())
13679 return;
13680
13681#ifdef FEAT_LIBCALL
13682 /* The first two args must be strings, otherwise its meaningless */
13683 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13684 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013685 string_in = NULL;
13686 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013687 string_in = argvars[2].vval.v_string;
13688 if (type == VAR_NUMBER)
13689 string_result = NULL;
13690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013692 if (mch_libcall(argvars[0].vval.v_string,
13693 argvars[1].vval.v_string,
13694 string_in,
13695 argvars[2].vval.v_number,
13696 string_result,
13697 &nr_result) == OK
13698 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013700 }
13701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013705 * "libcall()" function
13706 */
13707 static void
13708f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013709 typval_T *argvars;
13710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013711{
13712 libcall_common(argvars, rettv, VAR_STRING);
13713}
13714
13715/*
13716 * "libcallnr()" function
13717 */
13718 static void
13719f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013720 typval_T *argvars;
13721 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722{
13723 libcall_common(argvars, rettv, VAR_NUMBER);
13724}
13725
13726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 * "line(string)" function
13728 */
13729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
13734 linenr_T lnum = 0;
13735 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013736 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013738 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 if (fp != NULL)
13740 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742}
13743
13744/*
13745 * "line2byte(lnum)" function
13746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751{
13752#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#else
13755 linenr_T lnum;
13756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13762 if (rettv->vval.v_number >= 0)
13763 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#endif
13765}
13766
13767/*
13768 * "lispindent(lnum)" function
13769 */
13770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013771f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774{
13775#ifdef FEAT_LISP
13776 pos_T pos;
13777 linenr_T lnum;
13778
13779 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13782 {
13783 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 curwin->w_cursor = pos;
13786 }
13787 else
13788#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790}
13791
13792/*
13793 * "localtime()" function
13794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013797 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801}
13802
Bram Moolenaar33570922005-01-25 22:26:29 +000013803static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804
13805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 int exact;
13810{
13811 char_u *keys;
13812 char_u *which;
13813 char_u buf[NUMBUFLEN];
13814 char_u *keys_buf = NULL;
13815 char_u *rhs;
13816 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013817 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013818 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013819 mapblock_T *mp;
13820 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821
13822 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013823 rettv->v_type = VAR_STRING;
13824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 if (*keys == NUL)
13828 return;
13829
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013834 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013835 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013836 if (argvars[3].v_type != VAR_UNKNOWN)
13837 get_dict = get_tv_number(&argvars[3]);
13838 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 else
13841 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 if (which == NULL)
13843 return;
13844
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 mode = get_map_mode(&which, 0);
13846
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013847 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013848 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013850
13851 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013853 /* Return a string. */
13854 if (rhs != NULL)
13855 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856
Bram Moolenaarbd743252010-10-20 21:23:33 +020013857 }
13858 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13859 {
13860 /* Return a dictionary. */
13861 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13862 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13863 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864
Bram Moolenaarbd743252010-10-20 21:23:33 +020013865 dict_add_nr_str(dict, "lhs", 0L, lhs);
13866 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13867 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13868 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13869 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13870 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13871 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013872 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013873 dict_add_nr_str(dict, "mode", 0L, mapmode);
13874
13875 vim_free(lhs);
13876 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 }
13878}
13879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013880#ifdef FEAT_FLOAT
13881/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013882 * "log()" function
13883 */
13884 static void
13885f_log(argvars, rettv)
13886 typval_T *argvars;
13887 typval_T *rettv;
13888{
13889 float_T f;
13890
13891 rettv->v_type = VAR_FLOAT;
13892 if (get_float_arg(argvars, &f) == OK)
13893 rettv->vval.v_float = log(f);
13894 else
13895 rettv->vval.v_float = 0.0;
13896}
13897
13898/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013899 * "log10()" function
13900 */
13901 static void
13902f_log10(argvars, rettv)
13903 typval_T *argvars;
13904 typval_T *rettv;
13905{
13906 float_T f;
13907
13908 rettv->v_type = VAR_FLOAT;
13909 if (get_float_arg(argvars, &f) == OK)
13910 rettv->vval.v_float = log10(f);
13911 else
13912 rettv->vval.v_float = 0.0;
13913}
13914#endif
13915
Bram Moolenaar1dced572012-04-05 16:54:08 +020013916#ifdef FEAT_LUA
13917/*
13918 * "luaeval()" function
13919 */
13920 static void
13921f_luaeval(argvars, rettv)
13922 typval_T *argvars;
13923 typval_T *rettv;
13924{
13925 char_u *str;
13926 char_u buf[NUMBUFLEN];
13927
13928 str = get_tv_string_buf(&argvars[0], buf);
13929 do_luaeval(str, argvars + 1, rettv);
13930}
13931#endif
13932
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013934 * "map()" function
13935 */
13936 static void
13937f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013938 typval_T *argvars;
13939 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940{
13941 filter_map(argvars, rettv, TRUE);
13942}
13943
13944/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013945 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 */
13947 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013948f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013949 typval_T *argvars;
13950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013952 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953}
13954
13955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 */
13958 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *argvars;
13961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013963 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964}
13965
Bram Moolenaar33570922005-01-25 22:26:29 +000013966static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967
13968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013970 typval_T *argvars;
13971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 int type;
13973{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013974 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013975 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013976 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 char_u *pat;
13978 regmatch_T regmatch;
13979 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013980 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 char_u *save_cpo;
13982 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013983 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013984 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013985 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013986 list_T *l = NULL;
13987 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013988 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013989 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990
13991 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13992 save_cpo = p_cpo;
13993 p_cpo = (char_u *)"";
13994
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013995 rettv->vval.v_number = -1;
13996 if (type == 3)
13997 {
13998 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013999 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014000 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014001 }
14002 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004 rettv->v_type = VAR_STRING;
14005 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008 if (argvars[0].v_type == VAR_LIST)
14009 {
14010 if ((l = argvars[0].vval.v_list) == NULL)
14011 goto theend;
14012 li = l->lv_first;
14013 }
14014 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014015 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014017 len = (long)STRLEN(str);
14018 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14021 if (pat == NULL)
14022 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 int error = FALSE;
14027
14028 start = get_tv_number_chk(&argvars[2], &error);
14029 if (error)
14030 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014031 if (l != NULL)
14032 {
14033 li = list_find(l, start);
14034 if (li == NULL)
14035 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014036 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014037 }
14038 else
14039 {
14040 if (start < 0)
14041 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014042 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014043 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014044 /* When "count" argument is there ignore matches before "start",
14045 * otherwise skip part of the string. Differs when pattern is "^"
14046 * or "\<". */
14047 if (argvars[3].v_type != VAR_UNKNOWN)
14048 startcol = start;
14049 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014050 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014051 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014052 len -= start;
14053 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014054 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 nth = get_tv_number_chk(&argvars[3], &error);
14058 if (error)
14059 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 }
14061
14062 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14063 if (regmatch.regprog != NULL)
14064 {
14065 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014066
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014067 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014068 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014069 if (l != NULL)
14070 {
14071 if (li == NULL)
14072 {
14073 match = FALSE;
14074 break;
14075 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014076 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014077 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014078 if (str == NULL)
14079 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080 }
14081
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014082 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014083
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014085 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014086 if (l == NULL && !match)
14087 break;
14088
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014089 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014090 if (l != NULL)
14091 {
14092 li = li->li_next;
14093 ++idx;
14094 }
14095 else
14096 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014097#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014098 startcol = (colnr_T)(regmatch.startp[0]
14099 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014100#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014101 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014102#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014103 if (startcol > (colnr_T)len
14104 || str + startcol <= regmatch.startp[0])
14105 {
14106 match = FALSE;
14107 break;
14108 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014109 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014110 }
14111
14112 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014114 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014115 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014116 int i;
14117
14118 /* return list with matched string and submatches */
14119 for (i = 0; i < NSUBEXP; ++i)
14120 {
14121 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014122 {
14123 if (list_append_string(rettv->vval.v_list,
14124 (char_u *)"", 0) == FAIL)
14125 break;
14126 }
14127 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014128 regmatch.startp[i],
14129 (int)(regmatch.endp[i] - regmatch.startp[i]))
14130 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014131 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014132 }
14133 }
14134 else if (type == 2)
14135 {
14136 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014137 if (l != NULL)
14138 copy_tv(&li->li_tv, rettv);
14139 else
14140 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014142 }
14143 else if (l != NULL)
14144 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 else
14146 {
14147 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 (varnumber_T)(regmatch.startp[0] - str);
14150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014153 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 }
14155 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014156 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 }
14158
14159theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014160 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 p_cpo = save_cpo;
14162}
14163
14164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165 * "match()" function
14166 */
14167 static void
14168f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *argvars;
14170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171{
14172 find_some_match(argvars, rettv, 1);
14173}
14174
14175/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014176 * "matchadd()" function
14177 */
14178 static void
14179f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014180 typval_T *argvars UNUSED;
14181 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014182{
14183#ifdef FEAT_SEARCH_EXTRA
14184 char_u buf[NUMBUFLEN];
14185 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14186 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14187 int prio = 10; /* default priority */
14188 int id = -1;
14189 int error = FALSE;
14190
14191 rettv->vval.v_number = -1;
14192
14193 if (grp == NULL || pat == NULL)
14194 return;
14195 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014197 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014198 if (argvars[3].v_type != VAR_UNKNOWN)
14199 id = get_tv_number_chk(&argvars[3], &error);
14200 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014201 if (error == TRUE)
14202 return;
14203 if (id >= 1 && id <= 3)
14204 {
14205 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14206 return;
14207 }
14208
14209 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14210#endif
14211}
14212
14213/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014214 * "matcharg()" function
14215 */
14216 static void
14217f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014218 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014219 typval_T *rettv;
14220{
14221 if (rettv_list_alloc(rettv) == OK)
14222 {
14223#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014224 int id = get_tv_number(&argvars[0]);
14225 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014226
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014227 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014228 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014229 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14230 {
14231 list_append_string(rettv->vval.v_list,
14232 syn_id2name(m->hlg_id), -1);
14233 list_append_string(rettv->vval.v_list, m->pattern, -1);
14234 }
14235 else
14236 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014237 list_append_string(rettv->vval.v_list, NULL, -1);
14238 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014239 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014240 }
14241#endif
14242 }
14243}
14244
14245/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014246 * "matchdelete()" function
14247 */
14248 static void
14249f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014250 typval_T *argvars UNUSED;
14251 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014252{
14253#ifdef FEAT_SEARCH_EXTRA
14254 rettv->vval.v_number = match_delete(curwin,
14255 (int)get_tv_number(&argvars[0]), TRUE);
14256#endif
14257}
14258
14259/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014260 * "matchend()" function
14261 */
14262 static void
14263f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014266{
14267 find_some_match(argvars, rettv, 0);
14268}
14269
14270/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014271 * "matchlist()" function
14272 */
14273 static void
14274f_matchlist(argvars, rettv)
14275 typval_T *argvars;
14276 typval_T *rettv;
14277{
14278 find_some_match(argvars, rettv, 3);
14279}
14280
14281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282 * "matchstr()" function
14283 */
14284 static void
14285f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288{
14289 find_some_match(argvars, rettv, 2);
14290}
14291
Bram Moolenaar33570922005-01-25 22:26:29 +000014292static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014293
14294 static void
14295max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 typval_T *argvars;
14297 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014298 int domax;
14299{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014300 long n = 0;
14301 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014303
14304 if (argvars[0].v_type == VAR_LIST)
14305 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014306 list_T *l;
14307 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014308
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014309 l = argvars[0].vval.v_list;
14310 if (l != NULL)
14311 {
14312 li = l->lv_first;
14313 if (li != NULL)
14314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014316 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014317 {
14318 li = li->li_next;
14319 if (li == NULL)
14320 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014322 if (domax ? i > n : i < n)
14323 n = i;
14324 }
14325 }
14326 }
14327 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328 else if (argvars[0].v_type == VAR_DICT)
14329 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014331 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014333 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014334
14335 d = argvars[0].vval.v_dict;
14336 if (d != NULL)
14337 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014338 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014341 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014343 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014345 if (first)
14346 {
14347 n = i;
14348 first = FALSE;
14349 }
14350 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014351 n = i;
14352 }
14353 }
14354 }
14355 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014356 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014357 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014359}
14360
14361/*
14362 * "max()" function
14363 */
14364 static void
14365f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014368{
14369 max_min(argvars, rettv, TRUE);
14370}
14371
14372/*
14373 * "min()" function
14374 */
14375 static void
14376f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014377 typval_T *argvars;
14378 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014379{
14380 max_min(argvars, rettv, FALSE);
14381}
14382
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014383static int mkdir_recurse __ARGS((char_u *dir, int prot));
14384
14385/*
14386 * Create the directory in which "dir" is located, and higher levels when
14387 * needed.
14388 */
14389 static int
14390mkdir_recurse(dir, prot)
14391 char_u *dir;
14392 int prot;
14393{
14394 char_u *p;
14395 char_u *updir;
14396 int r = FAIL;
14397
14398 /* Get end of directory name in "dir".
14399 * We're done when it's "/" or "c:/". */
14400 p = gettail_sep(dir);
14401 if (p <= get_past_head(dir))
14402 return OK;
14403
14404 /* If the directory exists we're done. Otherwise: create it.*/
14405 updir = vim_strnsave(dir, (int)(p - dir));
14406 if (updir == NULL)
14407 return FAIL;
14408 if (mch_isdir(updir))
14409 r = OK;
14410 else if (mkdir_recurse(updir, prot) == OK)
14411 r = vim_mkdir_emsg(updir, prot);
14412 vim_free(updir);
14413 return r;
14414}
14415
14416#ifdef vim_mkdir
14417/*
14418 * "mkdir()" function
14419 */
14420 static void
14421f_mkdir(argvars, rettv)
14422 typval_T *argvars;
14423 typval_T *rettv;
14424{
14425 char_u *dir;
14426 char_u buf[NUMBUFLEN];
14427 int prot = 0755;
14428
14429 rettv->vval.v_number = FAIL;
14430 if (check_restricted() || check_secure())
14431 return;
14432
14433 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014434 if (*dir == NUL)
14435 rettv->vval.v_number = FAIL;
14436 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014437 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014438 if (*gettail(dir) == NUL)
14439 /* remove trailing slashes */
14440 *gettail_sep(dir) = NUL;
14441
14442 if (argvars[1].v_type != VAR_UNKNOWN)
14443 {
14444 if (argvars[2].v_type != VAR_UNKNOWN)
14445 prot = get_tv_number_chk(&argvars[2], NULL);
14446 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14447 mkdir_recurse(dir, prot);
14448 }
14449 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014450 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014451}
14452#endif
14453
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 * "mode()" function
14456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 typval_T *argvars;
14460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014462 char_u buf[3];
14463
14464 buf[1] = NUL;
14465 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 if (VIsual_active)
14468 {
14469 if (VIsual_select)
14470 buf[0] = VIsual_mode + 's' - 'v';
14471 else
14472 buf[0] = VIsual_mode;
14473 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014474 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014475 || State == CONFIRM)
14476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014478 if (State == ASKMORE)
14479 buf[1] = 'm';
14480 else if (State == CONFIRM)
14481 buf[1] = '?';
14482 }
14483 else if (State == EXTERNCMD)
14484 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 else if (State & INSERT)
14486 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014487#ifdef FEAT_VREPLACE
14488 if (State & VREPLACE_FLAG)
14489 {
14490 buf[0] = 'R';
14491 buf[1] = 'v';
14492 }
14493 else
14494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 if (State & REPLACE_FLAG)
14496 buf[0] = 'R';
14497 else
14498 buf[0] = 'i';
14499 }
14500 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014503 if (exmode_active)
14504 buf[1] = 'v';
14505 }
14506 else if (exmode_active)
14507 {
14508 buf[0] = 'c';
14509 buf[1] = 'e';
14510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014514 if (finish_op)
14515 buf[1] = 'o';
14516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014518 /* Clear out the minor mode when the argument is not a non-zero number or
14519 * non-empty string. */
14520 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014521 buf[1] = NUL;
14522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_string = vim_strsave(buf);
14524 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525}
14526
Bram Moolenaar429fa852013-04-15 12:27:36 +020014527#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014528/*
14529 * "mzeval()" function
14530 */
14531 static void
14532f_mzeval(argvars, rettv)
14533 typval_T *argvars;
14534 typval_T *rettv;
14535{
14536 char_u *str;
14537 char_u buf[NUMBUFLEN];
14538
14539 str = get_tv_string_buf(&argvars[0], buf);
14540 do_mzeval(str, rettv);
14541}
Bram Moolenaar75676462013-01-30 14:55:42 +010014542
14543 void
14544mzscheme_call_vim(name, args, rettv)
14545 char_u *name;
14546 typval_T *args;
14547 typval_T *rettv;
14548{
14549 typval_T argvars[3];
14550
14551 argvars[0].v_type = VAR_STRING;
14552 argvars[0].vval.v_string = name;
14553 copy_tv(args, &argvars[1]);
14554 argvars[2].v_type = VAR_UNKNOWN;
14555 f_call(argvars, rettv);
14556 clear_tv(&argvars[1]);
14557}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014558#endif
14559
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561 * "nextnonblank()" function
14562 */
14563 static void
14564f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014565 typval_T *argvars;
14566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567{
14568 linenr_T lnum;
14569
14570 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 {
14574 lnum = 0;
14575 break;
14576 }
14577 if (*skipwhite(ml_get(lnum)) != NUL)
14578 break;
14579 }
14580 rettv->vval.v_number = lnum;
14581}
14582
14583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 * "nr2char()" function
14585 */
14586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T *argvars;
14589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590{
14591 char_u buf[NUMBUFLEN];
14592
14593#ifdef FEAT_MBYTE
14594 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014595 {
14596 int utf8 = 0;
14597
14598 if (argvars[1].v_type != VAR_UNKNOWN)
14599 utf8 = get_tv_number_chk(&argvars[1], NULL);
14600 if (utf8)
14601 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14602 else
14603 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 else
14606#endif
14607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014608 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 buf[1] = NUL;
14610 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->v_type = VAR_STRING;
14612 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014613}
14614
14615/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014616 * "or(expr, expr)" function
14617 */
14618 static void
14619f_or(argvars, rettv)
14620 typval_T *argvars;
14621 typval_T *rettv;
14622{
14623 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14624 | get_tv_number_chk(&argvars[1], NULL);
14625}
14626
14627/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014628 * "pathshorten()" function
14629 */
14630 static void
14631f_pathshorten(argvars, rettv)
14632 typval_T *argvars;
14633 typval_T *rettv;
14634{
14635 char_u *p;
14636
14637 rettv->v_type = VAR_STRING;
14638 p = get_tv_string_chk(&argvars[0]);
14639 if (p == NULL)
14640 rettv->vval.v_string = NULL;
14641 else
14642 {
14643 p = vim_strsave(p);
14644 rettv->vval.v_string = p;
14645 if (p != NULL)
14646 shorten_dir(p);
14647 }
14648}
14649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014650#ifdef FEAT_FLOAT
14651/*
14652 * "pow()" function
14653 */
14654 static void
14655f_pow(argvars, rettv)
14656 typval_T *argvars;
14657 typval_T *rettv;
14658{
14659 float_T fx, fy;
14660
14661 rettv->v_type = VAR_FLOAT;
14662 if (get_float_arg(argvars, &fx) == OK
14663 && get_float_arg(&argvars[1], &fy) == OK)
14664 rettv->vval.v_float = pow(fx, fy);
14665 else
14666 rettv->vval.v_float = 0.0;
14667}
14668#endif
14669
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671 * "prevnonblank()" function
14672 */
14673 static void
14674f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014675 typval_T *argvars;
14676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677{
14678 linenr_T lnum;
14679
14680 lnum = get_tv_lnum(argvars);
14681 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14682 lnum = 0;
14683 else
14684 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14685 --lnum;
14686 rettv->vval.v_number = lnum;
14687}
14688
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014689#ifdef HAVE_STDARG_H
14690/* This dummy va_list is here because:
14691 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14692 * - locally in the function results in a "used before set" warning
14693 * - using va_start() to initialize it gives "function with fixed args" error */
14694static va_list ap;
14695#endif
14696
Bram Moolenaar8c711452005-01-14 21:53:12 +000014697/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014698 * "printf()" function
14699 */
14700 static void
14701f_printf(argvars, rettv)
14702 typval_T *argvars;
14703 typval_T *rettv;
14704{
14705 rettv->v_type = VAR_STRING;
14706 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014707#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014708 {
14709 char_u buf[NUMBUFLEN];
14710 int len;
14711 char_u *s;
14712 int saved_did_emsg = did_emsg;
14713 char *fmt;
14714
14715 /* Get the required length, allocate the buffer and do it for real. */
14716 did_emsg = FALSE;
14717 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014718 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014719 if (!did_emsg)
14720 {
14721 s = alloc(len + 1);
14722 if (s != NULL)
14723 {
14724 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014725 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014726 }
14727 }
14728 did_emsg |= saved_did_emsg;
14729 }
14730#endif
14731}
14732
14733/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014734 * "pumvisible()" function
14735 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014736 static void
14737f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014738 typval_T *argvars UNUSED;
14739 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014740{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014741#ifdef FEAT_INS_EXPAND
14742 if (pum_visible())
14743 rettv->vval.v_number = 1;
14744#endif
14745}
14746
Bram Moolenaardb913952012-06-29 12:54:53 +020014747#ifdef FEAT_PYTHON3
14748/*
14749 * "py3eval()" function
14750 */
14751 static void
14752f_py3eval(argvars, rettv)
14753 typval_T *argvars;
14754 typval_T *rettv;
14755{
14756 char_u *str;
14757 char_u buf[NUMBUFLEN];
14758
14759 str = get_tv_string_buf(&argvars[0], buf);
14760 do_py3eval(str, rettv);
14761}
14762#endif
14763
14764#ifdef FEAT_PYTHON
14765/*
14766 * "pyeval()" function
14767 */
14768 static void
14769f_pyeval(argvars, rettv)
14770 typval_T *argvars;
14771 typval_T *rettv;
14772{
14773 char_u *str;
14774 char_u buf[NUMBUFLEN];
14775
14776 str = get_tv_string_buf(&argvars[0], buf);
14777 do_pyeval(str, rettv);
14778}
14779#endif
14780
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014781/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014782 * "range()" function
14783 */
14784 static void
14785f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 typval_T *argvars;
14787 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014788{
14789 long start;
14790 long end;
14791 long stride = 1;
14792 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014795 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014796 if (argvars[1].v_type == VAR_UNKNOWN)
14797 {
14798 end = start - 1;
14799 start = 0;
14800 }
14801 else
14802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014806 }
14807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 if (error)
14809 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014810 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014811 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014812 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014813 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014814 else
14815 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014816 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014817 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014818 if (list_append_number(rettv->vval.v_list,
14819 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014820 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014821 }
14822}
14823
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014824/*
14825 * "readfile()" function
14826 */
14827 static void
14828f_readfile(argvars, rettv)
14829 typval_T *argvars;
14830 typval_T *rettv;
14831{
14832 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014833 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014834 char_u *fname;
14835 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14837 int io_size = sizeof(buf);
14838 int readlen; /* size of last fread() */
14839 char_u *prev = NULL; /* previously read bytes, if any */
14840 long prevlen = 0; /* length of data in prev */
14841 long prevsize = 0; /* size of prev buffer */
14842 long maxline = MAXLNUM;
14843 long cnt = 0;
14844 char_u *p; /* position in buf */
14845 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014846
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014847 if (argvars[1].v_type != VAR_UNKNOWN)
14848 {
14849 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14850 binary = TRUE;
14851 if (argvars[2].v_type != VAR_UNKNOWN)
14852 maxline = get_tv_number(&argvars[2]);
14853 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014855 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014856 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857
14858 /* Always open the file in binary mode, library functions have a mind of
14859 * their own about CR-LF conversion. */
14860 fname = get_tv_string(&argvars[0]);
14861 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14862 {
14863 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14864 return;
14865 }
14866
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014867 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014868 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014870
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014871 /* This for loop processes what was read, but is also entered at end
14872 * of file so that either:
14873 * - an incomplete line gets written
14874 * - a "binary" file gets an empty line at the end if it ends in a
14875 * newline. */
14876 for (p = buf, start = buf;
14877 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14878 ++p)
14879 {
14880 if (*p == '\n' || readlen <= 0)
14881 {
14882 listitem_T *li;
14883 char_u *s = NULL;
14884 long_u len = p - start;
14885
14886 /* Finished a line. Remove CRs before NL. */
14887 if (readlen > 0 && !binary)
14888 {
14889 while (len > 0 && start[len - 1] == '\r')
14890 --len;
14891 /* removal may cross back to the "prev" string */
14892 if (len == 0)
14893 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14894 --prevlen;
14895 }
14896 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014897 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898 else
14899 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014900 /* Change "prev" buffer to be the right size. This way
14901 * the bytes are only copied once, and very long lines are
14902 * allocated only once. */
14903 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014904 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014905 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014906 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014907 prev = NULL; /* the list will own the string */
14908 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014909 }
14910 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 if (s == NULL)
14912 {
14913 do_outofmem_msg((long_u) prevlen + len + 1);
14914 failed = TRUE;
14915 break;
14916 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014917
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014918 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014919 {
14920 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014921 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922 break;
14923 }
14924 li->li_tv.v_type = VAR_STRING;
14925 li->li_tv.v_lock = 0;
14926 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014927 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014928
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014929 start = p + 1; /* step over newline */
14930 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014931 break;
14932 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014933 else if (*p == NUL)
14934 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014935#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014936 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14937 * when finding the BF and check the previous two bytes. */
14938 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014939 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014940 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14941 * + 1, these may be in the "prev" string. */
14942 char_u back1 = p >= buf + 1 ? p[-1]
14943 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14944 char_u back2 = p >= buf + 2 ? p[-2]
14945 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14946 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014947
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014948 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014949 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014950 char_u *dest = p - 2;
14951
14952 /* Usually a BOM is at the beginning of a file, and so at
14953 * the beginning of a line; then we can just step over it.
14954 */
14955 if (start == dest)
14956 start = p + 1;
14957 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014958 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014959 /* have to shuffle buf to close gap */
14960 int adjust_prevlen = 0;
14961
14962 if (dest < buf)
14963 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014964 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014965 dest = buf;
14966 }
14967 if (readlen > p - buf + 1)
14968 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14969 readlen -= 3 - adjust_prevlen;
14970 prevlen -= adjust_prevlen;
14971 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014972 }
14973 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014974 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014975#endif
14976 } /* for */
14977
14978 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14979 break;
14980 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014981 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014982 /* There's part of a line in buf, store it in "prev". */
14983 if (p - start + prevlen >= prevsize)
14984 {
14985 /* need bigger "prev" buffer */
14986 char_u *newprev;
14987
14988 /* A common use case is ordinary text files and "prev" gets a
14989 * fragment of a line, so the first allocation is made
14990 * small, to avoid repeatedly 'allocing' large and
14991 * 'reallocing' small. */
14992 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014993 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014994 else
14995 {
14996 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014997 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014998 prevsize = grow50pc > growmin ? grow50pc : growmin;
14999 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015000 newprev = prev == NULL ? alloc(prevsize)
15001 : vim_realloc(prev, prevsize);
15002 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015003 {
15004 do_outofmem_msg((long_u)prevsize);
15005 failed = TRUE;
15006 break;
15007 }
15008 prev = newprev;
15009 }
15010 /* Add the line part to end of "prev". */
15011 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015012 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015013 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015014 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015015
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015016 /*
15017 * For a negative line count use only the lines at the end of the file,
15018 * free the rest.
15019 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015020 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015021 while (cnt > -maxline)
15022 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015023 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015024 --cnt;
15025 }
15026
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015027 if (failed)
15028 {
15029 list_free(rettv->vval.v_list, TRUE);
15030 /* readfile doc says an empty list is returned on error */
15031 rettv->vval.v_list = list_alloc();
15032 }
15033
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015034 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035 fclose(fd);
15036}
15037
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015038#if defined(FEAT_RELTIME)
15039static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15040
15041/*
15042 * Convert a List to proftime_T.
15043 * Return FAIL when there is something wrong.
15044 */
15045 static int
15046list2proftime(arg, tm)
15047 typval_T *arg;
15048 proftime_T *tm;
15049{
15050 long n1, n2;
15051 int error = FALSE;
15052
15053 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15054 || arg->vval.v_list->lv_len != 2)
15055 return FAIL;
15056 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15057 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15058# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015059 tm->HighPart = n1;
15060 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015061# else
15062 tm->tv_sec = n1;
15063 tm->tv_usec = n2;
15064# endif
15065 return error ? FAIL : OK;
15066}
15067#endif /* FEAT_RELTIME */
15068
15069/*
15070 * "reltime()" function
15071 */
15072 static void
15073f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015074 typval_T *argvars UNUSED;
15075 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015076{
15077#ifdef FEAT_RELTIME
15078 proftime_T res;
15079 proftime_T start;
15080
15081 if (argvars[0].v_type == VAR_UNKNOWN)
15082 {
15083 /* No arguments: get current time. */
15084 profile_start(&res);
15085 }
15086 else if (argvars[1].v_type == VAR_UNKNOWN)
15087 {
15088 if (list2proftime(&argvars[0], &res) == FAIL)
15089 return;
15090 profile_end(&res);
15091 }
15092 else
15093 {
15094 /* Two arguments: compute the difference. */
15095 if (list2proftime(&argvars[0], &start) == FAIL
15096 || list2proftime(&argvars[1], &res) == FAIL)
15097 return;
15098 profile_sub(&res, &start);
15099 }
15100
15101 if (rettv_list_alloc(rettv) == OK)
15102 {
15103 long n1, n2;
15104
15105# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015106 n1 = res.HighPart;
15107 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015108# else
15109 n1 = res.tv_sec;
15110 n2 = res.tv_usec;
15111# endif
15112 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15113 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15114 }
15115#endif
15116}
15117
15118/*
15119 * "reltimestr()" function
15120 */
15121 static void
15122f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015123 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015124 typval_T *rettv;
15125{
15126#ifdef FEAT_RELTIME
15127 proftime_T tm;
15128#endif
15129
15130 rettv->v_type = VAR_STRING;
15131 rettv->vval.v_string = NULL;
15132#ifdef FEAT_RELTIME
15133 if (list2proftime(&argvars[0], &tm) == OK)
15134 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15135#endif
15136}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015137
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15139static void make_connection __ARGS((void));
15140static int check_connection __ARGS((void));
15141
15142 static void
15143make_connection()
15144{
15145 if (X_DISPLAY == NULL
15146# ifdef FEAT_GUI
15147 && !gui.in_use
15148# endif
15149 )
15150 {
15151 x_force_connect = TRUE;
15152 setup_term_clip();
15153 x_force_connect = FALSE;
15154 }
15155}
15156
15157 static int
15158check_connection()
15159{
15160 make_connection();
15161 if (X_DISPLAY == NULL)
15162 {
15163 EMSG(_("E240: No connection to Vim server"));
15164 return FAIL;
15165 }
15166 return OK;
15167}
15168#endif
15169
15170#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015171static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172
15173 static void
15174remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015175 typval_T *argvars;
15176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177 int expr;
15178{
15179 char_u *server_name;
15180 char_u *keys;
15181 char_u *r = NULL;
15182 char_u buf[NUMBUFLEN];
15183# ifdef WIN32
15184 HWND w;
15185# else
15186 Window w;
15187# endif
15188
15189 if (check_restricted() || check_secure())
15190 return;
15191
15192# ifdef FEAT_X11
15193 if (check_connection() == FAIL)
15194 return;
15195# endif
15196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 server_name = get_tv_string_chk(&argvars[0]);
15198 if (server_name == NULL)
15199 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 keys = get_tv_string_buf(&argvars[1], buf);
15201# ifdef WIN32
15202 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15203# else
15204 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15205 < 0)
15206# endif
15207 {
15208 if (r != NULL)
15209 EMSG(r); /* sending worked but evaluation failed */
15210 else
15211 EMSG2(_("E241: Unable to send to %s"), server_name);
15212 return;
15213 }
15214
15215 rettv->vval.v_string = r;
15216
15217 if (argvars[2].v_type != VAR_UNKNOWN)
15218 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015220 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015221 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015223 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015224 v.di_tv.v_type = VAR_STRING;
15225 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 idvar = get_tv_string_chk(&argvars[2]);
15227 if (idvar != NULL)
15228 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015229 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230 }
15231}
15232#endif
15233
15234/*
15235 * "remote_expr()" function
15236 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 static void
15238f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015239 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241{
15242 rettv->v_type = VAR_STRING;
15243 rettv->vval.v_string = NULL;
15244#ifdef FEAT_CLIENTSERVER
15245 remote_common(argvars, rettv, TRUE);
15246#endif
15247}
15248
15249/*
15250 * "remote_foreground()" function
15251 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 static void
15253f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015254 typval_T *argvars UNUSED;
15255 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257#ifdef FEAT_CLIENTSERVER
15258# ifdef WIN32
15259 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 {
15261 char_u *server_name = get_tv_string_chk(&argvars[0]);
15262
15263 if (server_name != NULL)
15264 serverForeground(server_name);
15265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266# else
15267 /* Send a foreground() expression to the server. */
15268 argvars[1].v_type = VAR_STRING;
15269 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15270 argvars[2].v_type = VAR_UNKNOWN;
15271 remote_common(argvars, rettv, TRUE);
15272 vim_free(argvars[1].vval.v_string);
15273# endif
15274#endif
15275}
15276
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277 static void
15278f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281{
15282#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015283 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 char_u *s = NULL;
15285# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015286 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015288 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289
15290 if (check_restricted() || check_secure())
15291 {
15292 rettv->vval.v_number = -1;
15293 return;
15294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015295 serverid = get_tv_string_chk(&argvars[0]);
15296 if (serverid == NULL)
15297 {
15298 rettv->vval.v_number = -1;
15299 return; /* type error; errmsg already given */
15300 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015302 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015303 if (n == 0)
15304 rettv->vval.v_number = -1;
15305 else
15306 {
15307 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15308 rettv->vval.v_number = (s != NULL);
15309 }
15310# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015311 if (check_connection() == FAIL)
15312 return;
15313
15314 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316# endif
15317
15318 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 char_u *retvar;
15321
Bram Moolenaar33570922005-01-25 22:26:29 +000015322 v.di_tv.v_type = VAR_STRING;
15323 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 retvar = get_tv_string_chk(&argvars[1]);
15325 if (retvar != NULL)
15326 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 }
15329#else
15330 rettv->vval.v_number = -1;
15331#endif
15332}
15333
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 static void
15335f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338{
15339 char_u *r = NULL;
15340
15341#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015342 char_u *serverid = get_tv_string_chk(&argvars[0]);
15343
15344 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015345 {
15346# ifdef WIN32
15347 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015348 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015350 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 if (n != 0)
15352 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15353 if (r == NULL)
15354# else
15355 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357# endif
15358 EMSG(_("E277: Unable to read a server reply"));
15359 }
15360#endif
15361 rettv->v_type = VAR_STRING;
15362 rettv->vval.v_string = r;
15363}
15364
15365/*
15366 * "remote_send()" function
15367 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368 static void
15369f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015371 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372{
15373 rettv->v_type = VAR_STRING;
15374 rettv->vval.v_string = NULL;
15375#ifdef FEAT_CLIENTSERVER
15376 remote_common(argvars, rettv, FALSE);
15377#endif
15378}
15379
15380/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015381 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015382 */
15383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015385 typval_T *argvars;
15386 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015387{
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 list_T *l;
15389 listitem_T *item, *item2;
15390 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015391 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015392 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015393 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 dict_T *d;
15395 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015396 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015397
Bram Moolenaar8c711452005-01-14 21:53:12 +000015398 if (argvars[0].v_type == VAR_DICT)
15399 {
15400 if (argvars[2].v_type != VAR_UNKNOWN)
15401 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015402 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015403 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 key = get_tv_string_chk(&argvars[1]);
15406 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015408 di = dict_find(d, key, -1);
15409 if (di == NULL)
15410 EMSG2(_(e_dictkey), key);
15411 else
15412 {
15413 *rettv = di->di_tv;
15414 init_tv(&di->di_tv);
15415 dictitem_remove(d, di);
15416 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015417 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015418 }
15419 }
15420 else if (argvars[0].v_type != VAR_LIST)
15421 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015422 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015423 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015425 int error = FALSE;
15426
15427 idx = get_tv_number_chk(&argvars[1], &error);
15428 if (error)
15429 ; /* type error: do nothing, errmsg already given */
15430 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015431 EMSGN(_(e_listidx), idx);
15432 else
15433 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015434 if (argvars[2].v_type == VAR_UNKNOWN)
15435 {
15436 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015437 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015438 *rettv = item->li_tv;
15439 vim_free(item);
15440 }
15441 else
15442 {
15443 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015444 end = get_tv_number_chk(&argvars[2], &error);
15445 if (error)
15446 ; /* type error: do nothing */
15447 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015448 EMSGN(_(e_listidx), end);
15449 else
15450 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015451 int cnt = 0;
15452
15453 for (li = item; li != NULL; li = li->li_next)
15454 {
15455 ++cnt;
15456 if (li == item2)
15457 break;
15458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015459 if (li == NULL) /* didn't find "item2" after "item" */
15460 EMSG(_(e_invrange));
15461 else
15462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015463 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015464 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015465 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015466 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015467 l->lv_first = item;
15468 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015469 item->li_prev = NULL;
15470 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015471 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015472 }
15473 }
15474 }
15475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015476 }
15477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478}
15479
15480/*
15481 * "rename({from}, {to})" function
15482 */
15483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015485 typval_T *argvars;
15486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487{
15488 char_u buf[NUMBUFLEN];
15489
15490 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15494 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495}
15496
15497/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015498 * "repeat()" function
15499 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015500 static void
15501f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015502 typval_T *argvars;
15503 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015504{
15505 char_u *p;
15506 int n;
15507 int slen;
15508 int len;
15509 char_u *r;
15510 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015511
15512 n = get_tv_number(&argvars[1]);
15513 if (argvars[0].v_type == VAR_LIST)
15514 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015515 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015516 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015517 if (list_extend(rettv->vval.v_list,
15518 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015519 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015520 }
15521 else
15522 {
15523 p = get_tv_string(&argvars[0]);
15524 rettv->v_type = VAR_STRING;
15525 rettv->vval.v_string = NULL;
15526
15527 slen = (int)STRLEN(p);
15528 len = slen * n;
15529 if (len <= 0)
15530 return;
15531
15532 r = alloc(len + 1);
15533 if (r != NULL)
15534 {
15535 for (i = 0; i < n; i++)
15536 mch_memmove(r + i * slen, p, (size_t)slen);
15537 r[len] = NUL;
15538 }
15539
15540 rettv->vval.v_string = r;
15541 }
15542}
15543
15544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 * "resolve()" function
15546 */
15547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *argvars;
15550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551{
15552 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015553#ifdef HAVE_READLINK
15554 char_u *buf = NULL;
15555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558#ifdef FEAT_SHORTCUT
15559 {
15560 char_u *v = NULL;
15561
15562 v = mch_resolve_shortcut(p);
15563 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 }
15568#else
15569# ifdef HAVE_READLINK
15570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 char_u *cpy;
15572 int len;
15573 char_u *remain = NULL;
15574 char_u *q;
15575 int is_relative_to_current = FALSE;
15576 int has_trailing_pathsep = FALSE;
15577 int limit = 100;
15578
15579 p = vim_strsave(p);
15580
15581 if (p[0] == '.' && (vim_ispathsep(p[1])
15582 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15583 is_relative_to_current = TRUE;
15584
15585 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015586 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015589 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591
15592 q = getnextcomp(p);
15593 if (*q != NUL)
15594 {
15595 /* Separate the first path component in "p", and keep the
15596 * remainder (beginning with the path separator). */
15597 remain = vim_strsave(q - 1);
15598 q[-1] = NUL;
15599 }
15600
Bram Moolenaard9462e32011-04-11 21:35:11 +020015601 buf = alloc(MAXPATHL + 1);
15602 if (buf == NULL)
15603 goto fail;
15604
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 for (;;)
15606 {
15607 for (;;)
15608 {
15609 len = readlink((char *)p, (char *)buf, MAXPATHL);
15610 if (len <= 0)
15611 break;
15612 buf[len] = NUL;
15613
15614 if (limit-- == 0)
15615 {
15616 vim_free(p);
15617 vim_free(remain);
15618 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 goto fail;
15621 }
15622
15623 /* Ensure that the result will have a trailing path separator
15624 * if the argument has one. */
15625 if (remain == NULL && has_trailing_pathsep)
15626 add_pathsep(buf);
15627
15628 /* Separate the first path component in the link value and
15629 * concatenate the remainders. */
15630 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15631 if (*q != NUL)
15632 {
15633 if (remain == NULL)
15634 remain = vim_strsave(q - 1);
15635 else
15636 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015637 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 if (cpy != NULL)
15639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 vim_free(remain);
15641 remain = cpy;
15642 }
15643 }
15644 q[-1] = NUL;
15645 }
15646
15647 q = gettail(p);
15648 if (q > p && *q == NUL)
15649 {
15650 /* Ignore trailing path separator. */
15651 q[-1] = NUL;
15652 q = gettail(p);
15653 }
15654 if (q > p && !mch_isFullName(buf))
15655 {
15656 /* symlink is relative to directory of argument */
15657 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15658 if (cpy != NULL)
15659 {
15660 STRCPY(cpy, p);
15661 STRCPY(gettail(cpy), buf);
15662 vim_free(p);
15663 p = cpy;
15664 }
15665 }
15666 else
15667 {
15668 vim_free(p);
15669 p = vim_strsave(buf);
15670 }
15671 }
15672
15673 if (remain == NULL)
15674 break;
15675
15676 /* Append the first path component of "remain" to "p". */
15677 q = getnextcomp(remain + 1);
15678 len = q - remain - (*q != NUL);
15679 cpy = vim_strnsave(p, STRLEN(p) + len);
15680 if (cpy != NULL)
15681 {
15682 STRNCAT(cpy, remain, len);
15683 vim_free(p);
15684 p = cpy;
15685 }
15686 /* Shorten "remain". */
15687 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015688 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 else
15690 {
15691 vim_free(remain);
15692 remain = NULL;
15693 }
15694 }
15695
15696 /* If the result is a relative path name, make it explicitly relative to
15697 * the current directory if and only if the argument had this form. */
15698 if (!vim_ispathsep(*p))
15699 {
15700 if (is_relative_to_current
15701 && *p != NUL
15702 && !(p[0] == '.'
15703 && (p[1] == NUL
15704 || vim_ispathsep(p[1])
15705 || (p[1] == '.'
15706 && (p[2] == NUL
15707 || vim_ispathsep(p[2]))))))
15708 {
15709 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015710 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 if (cpy != NULL)
15712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 vim_free(p);
15714 p = cpy;
15715 }
15716 }
15717 else if (!is_relative_to_current)
15718 {
15719 /* Strip leading "./". */
15720 q = p;
15721 while (q[0] == '.' && vim_ispathsep(q[1]))
15722 q += 2;
15723 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015724 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 }
15726 }
15727
15728 /* Ensure that the result will have no trailing path separator
15729 * if the argument had none. But keep "/" or "//". */
15730 if (!has_trailing_pathsep)
15731 {
15732 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015733 if (after_pathsep(p, q))
15734 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 }
15736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 }
15739# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015740 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741# endif
15742#endif
15743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015744 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
15746#ifdef HAVE_READLINK
15747fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015748 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751}
15752
15753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 */
15756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 typval_T *argvars;
15759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760{
Bram Moolenaar33570922005-01-25 22:26:29 +000015761 list_T *l;
15762 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764 if (argvars[0].v_type != VAR_LIST)
15765 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015766 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015767 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 {
15769 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015770 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015771 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 while (li != NULL)
15773 {
15774 ni = li->li_prev;
15775 list_append(l, li);
15776 li = ni;
15777 }
15778 rettv->vval.v_list = l;
15779 rettv->v_type = VAR_LIST;
15780 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015781 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783}
15784
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015785#define SP_NOMOVE 0x01 /* don't move cursor */
15786#define SP_REPEAT 0x02 /* repeat to find outer pair */
15787#define SP_RETCOUNT 0x04 /* return matchcount */
15788#define SP_SETPCMARK 0x08 /* set previous context mark */
15789#define SP_START 0x10 /* accept match at start position */
15790#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15791#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015792
Bram Moolenaar33570922005-01-25 22:26:29 +000015793static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794
15795/*
15796 * Get flags for a search function.
15797 * Possibly sets "p_ws".
15798 * Returns BACKWARD, FORWARD or zero (for an error).
15799 */
15800 static int
15801get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015802 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015803 int *flagsp;
15804{
15805 int dir = FORWARD;
15806 char_u *flags;
15807 char_u nbuf[NUMBUFLEN];
15808 int mask;
15809
15810 if (varp->v_type != VAR_UNKNOWN)
15811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015812 flags = get_tv_string_buf_chk(varp, nbuf);
15813 if (flags == NULL)
15814 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 while (*flags != NUL)
15816 {
15817 switch (*flags)
15818 {
15819 case 'b': dir = BACKWARD; break;
15820 case 'w': p_ws = TRUE; break;
15821 case 'W': p_ws = FALSE; break;
15822 default: mask = 0;
15823 if (flagsp != NULL)
15824 switch (*flags)
15825 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015826 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015827 case 'e': mask = SP_END; break;
15828 case 'm': mask = SP_RETCOUNT; break;
15829 case 'n': mask = SP_NOMOVE; break;
15830 case 'p': mask = SP_SUBPAT; break;
15831 case 'r': mask = SP_REPEAT; break;
15832 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833 }
15834 if (mask == 0)
15835 {
15836 EMSG2(_(e_invarg2), flags);
15837 dir = 0;
15838 }
15839 else
15840 *flagsp |= mask;
15841 }
15842 if (dir == 0)
15843 break;
15844 ++flags;
15845 }
15846 }
15847 return dir;
15848}
15849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015851 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015853 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015854search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015855 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015856 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015857 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015859 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 char_u *pat;
15861 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015862 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 int save_p_ws = p_ws;
15864 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015865 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015866 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015867 proftime_T tm;
15868#ifdef FEAT_RELTIME
15869 long time_limit = 0;
15870#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015871 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015872 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015874 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015875 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015876 if (dir == 0)
15877 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015878 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015879 if (flags & SP_START)
15880 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015881 if (flags & SP_END)
15882 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015883
Bram Moolenaar76929292008-01-06 19:07:36 +000015884 /* Optional arguments: line number to stop searching and timeout. */
15885 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015886 {
15887 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15888 if (lnum_stop < 0)
15889 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015890#ifdef FEAT_RELTIME
15891 if (argvars[3].v_type != VAR_UNKNOWN)
15892 {
15893 time_limit = get_tv_number_chk(&argvars[3], NULL);
15894 if (time_limit < 0)
15895 goto theend;
15896 }
15897#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015898 }
15899
Bram Moolenaar76929292008-01-06 19:07:36 +000015900#ifdef FEAT_RELTIME
15901 /* Set the time limit, if there is one. */
15902 profile_setlimit(time_limit, &tm);
15903#endif
15904
Bram Moolenaar231334e2005-07-25 20:46:57 +000015905 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015906 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015907 * Check to make sure only those flags are set.
15908 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15909 * flags cannot be set. Check for that condition also.
15910 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015911 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015912 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015914 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015915 goto theend;
15916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015918 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015919 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015920 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015921 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015923 if (flags & SP_SUBPAT)
15924 retval = subpatnum;
15925 else
15926 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015927 if (flags & SP_SETPCMARK)
15928 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930 if (match_pos != NULL)
15931 {
15932 /* Store the match cursor position */
15933 match_pos->lnum = pos.lnum;
15934 match_pos->col = pos.col + 1;
15935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 /* "/$" will put the cursor after the end of the line, may need to
15937 * correct that here */
15938 check_cursor();
15939 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015940
15941 /* If 'n' flag is used: restore cursor position. */
15942 if (flags & SP_NOMOVE)
15943 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015944 else
15945 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015946theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015948
15949 return retval;
15950}
15951
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015952#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015953
15954/*
15955 * round() is not in C90, use ceil() or floor() instead.
15956 */
15957 float_T
15958vim_round(f)
15959 float_T f;
15960{
15961 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15962}
15963
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015964/*
15965 * "round({float})" function
15966 */
15967 static void
15968f_round(argvars, rettv)
15969 typval_T *argvars;
15970 typval_T *rettv;
15971{
15972 float_T f;
15973
15974 rettv->v_type = VAR_FLOAT;
15975 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015976 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015977 else
15978 rettv->vval.v_float = 0.0;
15979}
15980#endif
15981
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015983 * "screenattr()" function
15984 */
15985 static void
15986f_screenattr(argvars, rettv)
15987 typval_T *argvars UNUSED;
15988 typval_T *rettv;
15989{
15990 int row;
15991 int col;
15992 int c;
15993
15994 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15995 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15996 if (row < 0 || row >= screen_Rows
15997 || col < 0 || col >= screen_Columns)
15998 c = -1;
15999 else
16000 c = ScreenAttrs[LineOffset[row] + col];
16001 rettv->vval.v_number = c;
16002}
16003
16004/*
16005 * "screenchar()" function
16006 */
16007 static void
16008f_screenchar(argvars, rettv)
16009 typval_T *argvars UNUSED;
16010 typval_T *rettv;
16011{
16012 int row;
16013 int col;
16014 int off;
16015 int c;
16016
16017 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16018 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16019 if (row < 0 || row >= screen_Rows
16020 || col < 0 || col >= screen_Columns)
16021 c = -1;
16022 else
16023 {
16024 off = LineOffset[row] + col;
16025#ifdef FEAT_MBYTE
16026 if (enc_utf8 && ScreenLinesUC[off] != 0)
16027 c = ScreenLinesUC[off];
16028 else
16029#endif
16030 c = ScreenLines[off];
16031 }
16032 rettv->vval.v_number = c;
16033}
16034
16035/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016036 * "screencol()" function
16037 *
16038 * First column is 1 to be consistent with virtcol().
16039 */
16040 static void
16041f_screencol(argvars, rettv)
16042 typval_T *argvars UNUSED;
16043 typval_T *rettv;
16044{
16045 rettv->vval.v_number = screen_screencol() + 1;
16046}
16047
16048/*
16049 * "screenrow()" function
16050 */
16051 static void
16052f_screenrow(argvars, rettv)
16053 typval_T *argvars UNUSED;
16054 typval_T *rettv;
16055{
16056 rettv->vval.v_number = screen_screenrow() + 1;
16057}
16058
16059/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016060 * "search()" function
16061 */
16062 static void
16063f_search(argvars, rettv)
16064 typval_T *argvars;
16065 typval_T *rettv;
16066{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016067 int flags = 0;
16068
16069 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070}
16071
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016073 * "searchdecl()" function
16074 */
16075 static void
16076f_searchdecl(argvars, rettv)
16077 typval_T *argvars;
16078 typval_T *rettv;
16079{
16080 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016081 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016082 int error = FALSE;
16083 char_u *name;
16084
16085 rettv->vval.v_number = 1; /* default: FAIL */
16086
16087 name = get_tv_string_chk(&argvars[0]);
16088 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016089 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016090 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016091 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16092 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16093 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016094 if (!error && name != NULL)
16095 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016096 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016097}
16098
16099/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016100 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016102 static int
16103searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016104 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016105 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106{
16107 char_u *spat, *mpat, *epat;
16108 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 int dir;
16111 int flags = 0;
16112 char_u nbuf1[NUMBUFLEN];
16113 char_u nbuf2[NUMBUFLEN];
16114 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016115 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016116 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016117 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016120 spat = get_tv_string_chk(&argvars[0]);
16121 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16122 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16123 if (spat == NULL || mpat == NULL || epat == NULL)
16124 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 /* Handle the optional fourth argument: flags */
16127 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016128 if (dir == 0)
16129 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016130
16131 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016132 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16133 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016134 if ((flags & (SP_END | SP_SUBPAT)) != 0
16135 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016136 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016137 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016138 goto theend;
16139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016141 /* Using 'r' implies 'W', otherwise it doesn't work. */
16142 if (flags & SP_REPEAT)
16143 p_ws = FALSE;
16144
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016145 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016146 if (argvars[3].v_type == VAR_UNKNOWN
16147 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 skip = (char_u *)"";
16149 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016152 if (argvars[5].v_type != VAR_UNKNOWN)
16153 {
16154 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16155 if (lnum_stop < 0)
16156 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016157#ifdef FEAT_RELTIME
16158 if (argvars[6].v_type != VAR_UNKNOWN)
16159 {
16160 time_limit = get_tv_number_chk(&argvars[6], NULL);
16161 if (time_limit < 0)
16162 goto theend;
16163 }
16164#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016165 }
16166 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016167 if (skip == NULL)
16168 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016170 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016171 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016172
16173theend:
16174 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016175
16176 return retval;
16177}
16178
16179/*
16180 * "searchpair()" function
16181 */
16182 static void
16183f_searchpair(argvars, rettv)
16184 typval_T *argvars;
16185 typval_T *rettv;
16186{
16187 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16188}
16189
16190/*
16191 * "searchpairpos()" function
16192 */
16193 static void
16194f_searchpairpos(argvars, rettv)
16195 typval_T *argvars;
16196 typval_T *rettv;
16197{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198 pos_T match_pos;
16199 int lnum = 0;
16200 int col = 0;
16201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016203 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016204
16205 if (searchpair_cmn(argvars, &match_pos) > 0)
16206 {
16207 lnum = match_pos.lnum;
16208 col = match_pos.col;
16209 }
16210
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016211 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16212 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016213}
16214
16215/*
16216 * Search for a start/middle/end thing.
16217 * Used by searchpair(), see its documentation for the details.
16218 * Returns 0 or -1 for no match,
16219 */
16220 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016221do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16222 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016223 char_u *spat; /* start pattern */
16224 char_u *mpat; /* middle pattern */
16225 char_u *epat; /* end pattern */
16226 int dir; /* BACKWARD or FORWARD */
16227 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016228 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016229 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016230 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016231 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016232{
16233 char_u *save_cpo;
16234 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16235 long retval = 0;
16236 pos_T pos;
16237 pos_T firstpos;
16238 pos_T foundpos;
16239 pos_T save_cursor;
16240 pos_T save_pos;
16241 int n;
16242 int r;
16243 int nest = 1;
16244 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016245 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016246 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016247
16248 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16249 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016250 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016251
Bram Moolenaar76929292008-01-06 19:07:36 +000016252#ifdef FEAT_RELTIME
16253 /* Set the time limit, if there is one. */
16254 profile_setlimit(time_limit, &tm);
16255#endif
16256
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016257 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16258 * start/middle/end (pat3, for the top pair). */
16259 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16260 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16261 if (pat2 == NULL || pat3 == NULL)
16262 goto theend;
16263 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16264 if (*mpat == NUL)
16265 STRCPY(pat3, pat2);
16266 else
16267 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16268 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016269 if (flags & SP_START)
16270 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016271
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 save_cursor = curwin->w_cursor;
16273 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016274 clearpos(&firstpos);
16275 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 pat = pat3;
16277 for (;;)
16278 {
16279 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016280 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16282 /* didn't find it or found the first match again: FAIL */
16283 break;
16284
16285 if (firstpos.lnum == 0)
16286 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016287 if (equalpos(pos, foundpos))
16288 {
16289 /* Found the same position again. Can happen with a pattern that
16290 * has "\zs" at the end and searching backwards. Advance one
16291 * character and try again. */
16292 if (dir == BACKWARD)
16293 decl(&pos);
16294 else
16295 incl(&pos);
16296 }
16297 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016299 /* clear the start flag to avoid getting stuck here */
16300 options &= ~SEARCH_START;
16301
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 /* If the skip pattern matches, ignore this match. */
16303 if (*skip != NUL)
16304 {
16305 save_pos = curwin->w_cursor;
16306 curwin->w_cursor = pos;
16307 r = eval_to_bool(skip, &err, NULL, FALSE);
16308 curwin->w_cursor = save_pos;
16309 if (err)
16310 {
16311 /* Evaluating {skip} caused an error, break here. */
16312 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016313 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 break;
16315 }
16316 if (r)
16317 continue;
16318 }
16319
16320 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16321 {
16322 /* Found end when searching backwards or start when searching
16323 * forward: nested pair. */
16324 ++nest;
16325 pat = pat2; /* nested, don't search for middle */
16326 }
16327 else
16328 {
16329 /* Found end when searching forward or start when searching
16330 * backward: end of (nested) pair; or found middle in outer pair. */
16331 if (--nest == 1)
16332 pat = pat3; /* outer level, search for middle */
16333 }
16334
16335 if (nest == 0)
16336 {
16337 /* Found the match: return matchcount or line number. */
16338 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016339 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016341 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016342 if (flags & SP_SETPCMARK)
16343 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 curwin->w_cursor = pos;
16345 if (!(flags & SP_REPEAT))
16346 break;
16347 nest = 1; /* search for next unmatched */
16348 }
16349 }
16350
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016351 if (match_pos != NULL)
16352 {
16353 /* Store the match cursor position */
16354 match_pos->lnum = curwin->w_cursor.lnum;
16355 match_pos->col = curwin->w_cursor.col + 1;
16356 }
16357
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016359 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 curwin->w_cursor = save_cursor;
16361
16362theend:
16363 vim_free(pat2);
16364 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016365 if (p_cpo == empty_option)
16366 p_cpo = save_cpo;
16367 else
16368 /* Darn, evaluating the {skip} expression changed the value. */
16369 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016370
16371 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372}
16373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374/*
16375 * "searchpos()" function
16376 */
16377 static void
16378f_searchpos(argvars, rettv)
16379 typval_T *argvars;
16380 typval_T *rettv;
16381{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016382 pos_T match_pos;
16383 int lnum = 0;
16384 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016385 int n;
16386 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016389 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016391 n = search_cmn(argvars, &match_pos, &flags);
16392 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016393 {
16394 lnum = match_pos.lnum;
16395 col = match_pos.col;
16396 }
16397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016398 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16399 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016400 if (flags & SP_SUBPAT)
16401 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016402}
16403
16404
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 static void
16406f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410#ifdef FEAT_CLIENTSERVER
16411 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 char_u *server = get_tv_string_chk(&argvars[0]);
16413 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 if (server == NULL || reply == NULL)
16417 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016418 if (check_restricted() || check_secure())
16419 return;
16420# ifdef FEAT_X11
16421 if (check_connection() == FAIL)
16422 return;
16423# endif
16424
16425 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 EMSG(_("E258: Unable to send to client"));
16428 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 rettv->vval.v_number = 0;
16431#else
16432 rettv->vval.v_number = -1;
16433#endif
16434}
16435
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 static void
16437f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440{
16441 char_u *r = NULL;
16442
16443#ifdef FEAT_CLIENTSERVER
16444# ifdef WIN32
16445 r = serverGetVimNames();
16446# else
16447 make_connection();
16448 if (X_DISPLAY != NULL)
16449 r = serverGetVimNames(X_DISPLAY);
16450# endif
16451#endif
16452 rettv->v_type = VAR_STRING;
16453 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454}
16455
16456/*
16457 * "setbufvar()" function
16458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016460f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016462 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463{
16464 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016467 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 char_u nbuf[NUMBUFLEN];
16469
16470 if (check_restricted() || check_secure())
16471 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016472 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16473 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016474 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 varp = &argvars[2];
16476
16477 if (buf != NULL && varname != NULL && varp != NULL)
16478 {
16479 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481
16482 if (*varname == '&')
16483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 long numval;
16485 char_u *strval;
16486 int error = FALSE;
16487
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 numval = get_tv_number_chk(varp, &error);
16490 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 if (!error && strval != NULL)
16492 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 }
16494 else
16495 {
16496 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16497 if (bufvarname != NULL)
16498 {
16499 STRCPY(bufvarname, "b:");
16500 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016501 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 vim_free(bufvarname);
16503 }
16504 }
16505
16506 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509}
16510
16511/*
16512 * "setcmdpos()" function
16513 */
16514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016516 typval_T *argvars;
16517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 int pos = (int)get_tv_number(&argvars[0]) - 1;
16520
16521 if (pos >= 0)
16522 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523}
16524
16525/*
16526 * "setline()" function
16527 */
16528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016529f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016530 typval_T *argvars;
16531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532{
16533 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016534 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016535 list_T *l = NULL;
16536 listitem_T *li = NULL;
16537 long added = 0;
16538 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 lnum = get_tv_lnum(&argvars[0]);
16541 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016543 l = argvars[1].vval.v_list;
16544 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016546 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016548
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016549 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016550 for (;;)
16551 {
16552 if (l != NULL)
16553 {
16554 /* list argument, get next string */
16555 if (li == NULL)
16556 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016557 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016558 li = li->li_next;
16559 }
16560
16561 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016562 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016563 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016564
16565 /* When coming here from Insert mode, sync undo, so that this can be
16566 * undone separately from what was previously inserted. */
16567 if (u_sync_once == 2)
16568 {
16569 u_sync_once = 1; /* notify that u_sync() was called */
16570 u_sync(TRUE);
16571 }
16572
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016573 if (lnum <= curbuf->b_ml.ml_line_count)
16574 {
16575 /* existing line, replace it */
16576 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16577 {
16578 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016579 if (lnum == curwin->w_cursor.lnum)
16580 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016581 rettv->vval.v_number = 0; /* OK */
16582 }
16583 }
16584 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16585 {
16586 /* lnum is one past the last line, append the line */
16587 ++added;
16588 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16589 rettv->vval.v_number = 0; /* OK */
16590 }
16591
16592 if (l == NULL) /* only one string argument */
16593 break;
16594 ++lnum;
16595 }
16596
16597 if (added > 0)
16598 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599}
16600
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016601static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16602
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016604 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016605 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016606 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016607set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016608 win_T *wp UNUSED;
16609 typval_T *list_arg UNUSED;
16610 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016611 typval_T *rettv;
16612{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016613#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016614 char_u *act;
16615 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016616#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016617
Bram Moolenaar2641f772005-03-25 21:58:17 +000016618 rettv->vval.v_number = -1;
16619
16620#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016621 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016622 EMSG(_(e_listreq));
16623 else
16624 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016625 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016626
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016627 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016628 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016629 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 if (act == NULL)
16631 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016632 if (*act == 'a' || *act == 'r')
16633 action = *act;
16634 }
16635
Bram Moolenaar81484f42012-12-05 15:16:47 +010016636 if (l != NULL && set_errorlist(wp, l, action,
16637 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016638 rettv->vval.v_number = 0;
16639 }
16640#endif
16641}
16642
16643/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016644 * "setloclist()" function
16645 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016646 static void
16647f_setloclist(argvars, rettv)
16648 typval_T *argvars;
16649 typval_T *rettv;
16650{
16651 win_T *win;
16652
16653 rettv->vval.v_number = -1;
16654
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016655 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016656 if (win != NULL)
16657 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16658}
16659
16660/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016661 * "setmatches()" function
16662 */
16663 static void
16664f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016665 typval_T *argvars UNUSED;
16666 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016667{
16668#ifdef FEAT_SEARCH_EXTRA
16669 list_T *l;
16670 listitem_T *li;
16671 dict_T *d;
16672
16673 rettv->vval.v_number = -1;
16674 if (argvars[0].v_type != VAR_LIST)
16675 {
16676 EMSG(_(e_listreq));
16677 return;
16678 }
16679 if ((l = argvars[0].vval.v_list) != NULL)
16680 {
16681
16682 /* To some extent make sure that we are dealing with a list from
16683 * "getmatches()". */
16684 li = l->lv_first;
16685 while (li != NULL)
16686 {
16687 if (li->li_tv.v_type != VAR_DICT
16688 || (d = li->li_tv.vval.v_dict) == NULL)
16689 {
16690 EMSG(_(e_invarg));
16691 return;
16692 }
16693 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16694 && dict_find(d, (char_u *)"pattern", -1) != NULL
16695 && dict_find(d, (char_u *)"priority", -1) != NULL
16696 && dict_find(d, (char_u *)"id", -1) != NULL))
16697 {
16698 EMSG(_(e_invarg));
16699 return;
16700 }
16701 li = li->li_next;
16702 }
16703
16704 clear_matches(curwin);
16705 li = l->lv_first;
16706 while (li != NULL)
16707 {
16708 d = li->li_tv.vval.v_dict;
16709 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16710 get_dict_string(d, (char_u *)"pattern", FALSE),
16711 (int)get_dict_number(d, (char_u *)"priority"),
16712 (int)get_dict_number(d, (char_u *)"id"));
16713 li = li->li_next;
16714 }
16715 rettv->vval.v_number = 0;
16716 }
16717#endif
16718}
16719
16720/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016721 * "setpos()" function
16722 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016723 static void
16724f_setpos(argvars, rettv)
16725 typval_T *argvars;
16726 typval_T *rettv;
16727{
16728 pos_T pos;
16729 int fnum;
16730 char_u *name;
16731
Bram Moolenaar08250432008-02-13 11:42:46 +000016732 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016733 name = get_tv_string_chk(argvars);
16734 if (name != NULL)
16735 {
16736 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16737 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016738 if (--pos.col < 0)
16739 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016740 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016741 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016742 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016743 if (fnum == curbuf->b_fnum)
16744 {
16745 curwin->w_cursor = pos;
16746 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016747 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016748 }
16749 else
16750 EMSG(_(e_invarg));
16751 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016752 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16753 {
16754 /* set mark */
16755 if (setmark_pos(name[1], &pos, fnum) == OK)
16756 rettv->vval.v_number = 0;
16757 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016758 else
16759 EMSG(_(e_invarg));
16760 }
16761 }
16762}
16763
16764/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016765 * "setqflist()" function
16766 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016767 static void
16768f_setqflist(argvars, rettv)
16769 typval_T *argvars;
16770 typval_T *rettv;
16771{
16772 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16773}
16774
16775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 * "setreg()" function
16777 */
16778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016779f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016780 typval_T *argvars;
16781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782{
16783 int regname;
16784 char_u *strregname;
16785 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016786 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 int append;
16788 char_u yank_type;
16789 long block_len;
16790
16791 block_len = -1;
16792 yank_type = MAUTO;
16793 append = FALSE;
16794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016795 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016796 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016798 if (strregname == NULL)
16799 return; /* type error; errmsg already given */
16800 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 if (regname == 0 || regname == '@')
16802 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016806 stropt = get_tv_string_chk(&argvars[2]);
16807 if (stropt == NULL)
16808 return; /* type error */
16809 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 switch (*stropt)
16811 {
16812 case 'a': case 'A': /* append */
16813 append = TRUE;
16814 break;
16815 case 'v': case 'c': /* character-wise selection */
16816 yank_type = MCHAR;
16817 break;
16818 case 'V': case 'l': /* line-wise selection */
16819 yank_type = MLINE;
16820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 case 'b': case Ctrl_V: /* block-wise selection */
16822 yank_type = MBLOCK;
16823 if (VIM_ISDIGIT(stropt[1]))
16824 {
16825 ++stropt;
16826 block_len = getdigits(&stropt) - 1;
16827 --stropt;
16828 }
16829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 }
16831 }
16832
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016833 if (argvars[1].v_type == VAR_LIST)
16834 {
16835 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016836 char_u **allocval;
16837 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016838 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016839 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016840 int len = argvars[1].vval.v_list->lv_len;
16841 listitem_T *li;
16842
Bram Moolenaar7d647822014-04-05 21:28:56 +020016843 /* First half: use for pointers to result lines; second half: use for
16844 * pointers to allocated copies. */
16845 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016846 if (lstval == NULL)
16847 return;
16848 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016849 allocval = lstval + len + 2;
16850 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016851
16852 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16853 li = li->li_next)
16854 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016855 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016856 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016857 goto free_lstval;
16858 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016859 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016860 /* Need to make a copy, next get_tv_string_buf_chk() will
16861 * overwrite the string. */
16862 strval = vim_strsave(buf);
16863 if (strval == NULL)
16864 goto free_lstval;
16865 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016866 }
16867 *curval++ = strval;
16868 }
16869 *curval++ = NULL;
16870
16871 write_reg_contents_lst(regname, lstval, -1,
16872 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016873free_lstval:
16874 while (curallocval > allocval)
16875 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016876 vim_free(lstval);
16877 }
16878 else
16879 {
16880 strval = get_tv_string_chk(&argvars[1]);
16881 if (strval == NULL)
16882 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016883 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016886 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887}
16888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016889/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016890 * "settabvar()" function
16891 */
16892 static void
16893f_settabvar(argvars, rettv)
16894 typval_T *argvars;
16895 typval_T *rettv;
16896{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016897#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016898 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016899 tabpage_T *tp;
16900#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016901 char_u *varname, *tabvarname;
16902 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016903
16904 rettv->vval.v_number = 0;
16905
16906 if (check_restricted() || check_secure())
16907 return;
16908
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016909#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016910 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016911#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016912 varname = get_tv_string_chk(&argvars[1]);
16913 varp = &argvars[2];
16914
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016915 if (varname != NULL && varp != NULL
16916#ifdef FEAT_WINDOWS
16917 && tp != NULL
16918#endif
16919 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016920 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016921#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016922 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016923 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016924#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016925
16926 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16927 if (tabvarname != NULL)
16928 {
16929 STRCPY(tabvarname, "t:");
16930 STRCPY(tabvarname + 2, varname);
16931 set_var(tabvarname, varp, TRUE);
16932 vim_free(tabvarname);
16933 }
16934
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016935#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016936 /* Restore current tabpage */
16937 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016938 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016939#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016940 }
16941}
16942
16943/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016944 * "settabwinvar()" function
16945 */
16946 static void
16947f_settabwinvar(argvars, rettv)
16948 typval_T *argvars;
16949 typval_T *rettv;
16950{
16951 setwinvar(argvars, rettv, 1);
16952}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953
16954/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016955 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 typval_T *argvars;
16960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016962 setwinvar(argvars, rettv, 0);
16963}
16964
16965/*
16966 * "setwinvar()" and "settabwinvar()" functions
16967 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016968
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016969 static void
16970setwinvar(argvars, rettv, off)
16971 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016972 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016973 int off;
16974{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 win_T *win;
16976#ifdef FEAT_WINDOWS
16977 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016978 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979#endif
16980 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016983 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
16985 if (check_restricted() || check_secure())
16986 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016987
16988#ifdef FEAT_WINDOWS
16989 if (off == 1)
16990 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16991 else
16992 tp = curtab;
16993#endif
16994 win = find_win_by_nr(&argvars[off], tp);
16995 varname = get_tv_string_chk(&argvars[off + 1]);
16996 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997
16998 if (win != NULL && varname != NULL && varp != NULL)
16999 {
17000#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017001 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017002 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003#endif
17004
17005 if (*varname == '&')
17006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017007 long numval;
17008 char_u *strval;
17009 int error = FALSE;
17010
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 numval = get_tv_number_chk(varp, &error);
17013 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 if (!error && strval != NULL)
17015 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 }
17017 else
17018 {
17019 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17020 if (winvarname != NULL)
17021 {
17022 STRCPY(winvarname, "w:");
17023 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017024 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 vim_free(winvarname);
17026 }
17027 }
17028
17029#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017030 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031#endif
17032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033}
17034
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017035#ifdef FEAT_CRYPT
17036/*
17037 * "sha256({string})" function
17038 */
17039 static void
17040f_sha256(argvars, rettv)
17041 typval_T *argvars;
17042 typval_T *rettv;
17043{
17044 char_u *p;
17045
17046 p = get_tv_string(&argvars[0]);
17047 rettv->vval.v_string = vim_strsave(
17048 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17049 rettv->v_type = VAR_STRING;
17050}
17051#endif /* FEAT_CRYPT */
17052
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017054 * "shellescape({string})" function
17055 */
17056 static void
17057f_shellescape(argvars, rettv)
17058 typval_T *argvars;
17059 typval_T *rettv;
17060{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017061 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017062 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017063 rettv->v_type = VAR_STRING;
17064}
17065
17066/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017067 * shiftwidth() function
17068 */
17069 static void
17070f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017071 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017072 typval_T *rettv;
17073{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017074 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017075}
17076
17077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 */
17080 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017082 typval_T *argvars;
17083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087 p = get_tv_string(&argvars[0]);
17088 rettv->vval.v_string = vim_strsave(p);
17089 simplify_filename(rettv->vval.v_string); /* simplify in place */
17090 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091}
17092
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017093#ifdef FEAT_FLOAT
17094/*
17095 * "sin()" function
17096 */
17097 static void
17098f_sin(argvars, rettv)
17099 typval_T *argvars;
17100 typval_T *rettv;
17101{
17102 float_T f;
17103
17104 rettv->v_type = VAR_FLOAT;
17105 if (get_float_arg(argvars, &f) == OK)
17106 rettv->vval.v_float = sin(f);
17107 else
17108 rettv->vval.v_float = 0.0;
17109}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017110
17111/*
17112 * "sinh()" function
17113 */
17114 static void
17115f_sinh(argvars, rettv)
17116 typval_T *argvars;
17117 typval_T *rettv;
17118{
17119 float_T f;
17120
17121 rettv->v_type = VAR_FLOAT;
17122 if (get_float_arg(argvars, &f) == OK)
17123 rettv->vval.v_float = sinh(f);
17124 else
17125 rettv->vval.v_float = 0.0;
17126}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017127#endif
17128
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129static int
17130#ifdef __BORLANDC__
17131 _RTLENTRYF
17132#endif
17133 item_compare __ARGS((const void *s1, const void *s2));
17134static int
17135#ifdef __BORLANDC__
17136 _RTLENTRYF
17137#endif
17138 item_compare2 __ARGS((const void *s1, const void *s2));
17139
17140static int item_compare_ic;
17141static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017142static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017144static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145#define ITEM_COMPARE_FAIL 999
17146
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017148 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 static int
17151#ifdef __BORLANDC__
17152_RTLENTRYF
17153#endif
17154item_compare(s1, s2)
17155 const void *s1;
17156 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158 char_u *p1, *p2;
17159 char_u *tofree1, *tofree2;
17160 int res;
17161 char_u numbuf1[NUMBUFLEN];
17162 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017164 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17165 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017166 if (p1 == NULL)
17167 p1 = (char_u *)"";
17168 if (p2 == NULL)
17169 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170 if (item_compare_ic)
17171 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 res = STRCMP(p1, p2);
17174 vim_free(tofree1);
17175 vim_free(tofree2);
17176 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177}
17178
17179 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180#ifdef __BORLANDC__
17181_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183item_compare2(s1, s2)
17184 const void *s1;
17185 const void *s2;
17186{
17187 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017189 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017192 /* shortcut after failure in previous call; compare all items equal */
17193 if (item_compare_func_err)
17194 return 0;
17195
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017196 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17197 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17199 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200
17201 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017202 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017203 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17204 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205 clear_tv(&argv[0]);
17206 clear_tv(&argv[1]);
17207
17208 if (res == FAIL)
17209 res = ITEM_COMPARE_FAIL;
17210 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017211 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17212 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017213 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017214 clear_tv(&rettv);
17215 return res;
17216}
17217
17218/*
17219 * "sort({list})" function
17220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017222do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 typval_T *argvars;
17224 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017225 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226{
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 list_T *l;
17228 listitem_T *li;
17229 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230 long len;
17231 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017234 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 else
17236 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017238 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017239 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017240 return;
17241 rettv->vval.v_list = l;
17242 rettv->v_type = VAR_LIST;
17243 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 len = list_len(l);
17246 if (len <= 1)
17247 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 item_compare_ic = FALSE;
17250 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017251 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017252 if (argvars[1].v_type != VAR_UNKNOWN)
17253 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017254 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017255 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257 else
17258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017259 int error = FALSE;
17260
17261 i = get_tv_number_chk(&argvars[1], &error);
17262 if (error)
17263 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017264 if (i == 1)
17265 item_compare_ic = TRUE;
17266 else
17267 item_compare_func = get_tv_string(&argvars[1]);
17268 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017269
17270 if (argvars[2].v_type != VAR_UNKNOWN)
17271 {
17272 /* optional third argument: {dict} */
17273 if (argvars[2].v_type != VAR_DICT)
17274 {
17275 EMSG(_(e_dictreq));
17276 return;
17277 }
17278 item_compare_selfdict = argvars[2].vval.v_dict;
17279 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284 if (ptrs == NULL)
17285 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286
Bram Moolenaar327aa022014-03-25 18:24:23 +010017287 i = 0;
17288 if (sort)
17289 {
17290 /* sort(): ptrs will be the list to sort */
17291 for (li = l->lv_first; li != NULL; li = li->li_next)
17292 ptrs[i++] = li;
17293
17294 item_compare_func_err = FALSE;
17295 /* test the compare function */
17296 if (item_compare_func != NULL
17297 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017299 EMSG(_("E702: Sort compare function failed"));
17300 else
17301 {
17302 /* Sort the array with item pointers. */
17303 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17304 item_compare_func == NULL ? item_compare : item_compare2);
17305
17306 if (!item_compare_func_err)
17307 {
17308 /* Clear the List and append the items in sorted order. */
17309 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17310 l->lv_len = 0;
17311 for (i = 0; i < len; ++i)
17312 list_append(l, ptrs[i]);
17313 }
17314 }
17315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017318 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17319
17320 /* f_uniq(): ptrs will be a stack of items to remove */
17321 item_compare_func_err = FALSE;
17322 item_compare_func_ptr = item_compare_func
17323 ? item_compare2 : item_compare;
17324
17325 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17326 li = li->li_next)
17327 {
17328 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17329 == 0)
17330 ptrs[i++] = li;
17331 if (item_compare_func_err)
17332 {
17333 EMSG(_("E882: Uniq compare function failed"));
17334 break;
17335 }
17336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017338 if (!item_compare_func_err)
17339 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017340 while (--i >= 0)
17341 {
17342 li = ptrs[i]->li_next;
17343 ptrs[i]->li_next = li->li_next;
17344 if (li->li_next != NULL)
17345 li->li_next->li_prev = ptrs[i];
17346 else
17347 l->lv_last = ptrs[i];
17348 list_fix_watch(l, li);
17349 listitem_free(li);
17350 l->lv_len--;
17351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017353 }
17354
17355 vim_free(ptrs);
17356 }
17357}
17358
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017359/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017360 * "sort({list})" function
17361 */
17362 static void
17363f_sort(argvars, rettv)
17364 typval_T *argvars;
17365 typval_T *rettv;
17366{
17367 do_sort_uniq(argvars, rettv, TRUE);
17368}
17369
17370/*
17371 * "uniq({list})" function
17372 */
17373 static void
17374f_uniq(argvars, rettv)
17375 typval_T *argvars;
17376 typval_T *rettv;
17377{
17378 do_sort_uniq(argvars, rettv, FALSE);
17379}
17380
17381/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017382 * "soundfold({word})" function
17383 */
17384 static void
17385f_soundfold(argvars, rettv)
17386 typval_T *argvars;
17387 typval_T *rettv;
17388{
17389 char_u *s;
17390
17391 rettv->v_type = VAR_STRING;
17392 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017393#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017394 rettv->vval.v_string = eval_soundfold(s);
17395#else
17396 rettv->vval.v_string = vim_strsave(s);
17397#endif
17398}
17399
17400/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017401 * "spellbadword()" function
17402 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017403 static void
17404f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017405 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017406 typval_T *rettv;
17407{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017408 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017409 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017410 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017412 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017413 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017414
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017415#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017416 if (argvars[0].v_type == VAR_UNKNOWN)
17417 {
17418 /* Find the start and length of the badly spelled word. */
17419 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17420 if (len != 0)
17421 word = ml_get_cursor();
17422 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017423 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017424 {
17425 char_u *str = get_tv_string_chk(&argvars[0]);
17426 int capcol = -1;
17427
17428 if (str != NULL)
17429 {
17430 /* Check the argument for spelling. */
17431 while (*str != NUL)
17432 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017433 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017434 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017435 {
17436 word = str;
17437 break;
17438 }
17439 str += len;
17440 }
17441 }
17442 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017443#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017444
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017445 list_append_string(rettv->vval.v_list, word, len);
17446 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017447 attr == HLF_SPB ? "bad" :
17448 attr == HLF_SPR ? "rare" :
17449 attr == HLF_SPL ? "local" :
17450 attr == HLF_SPC ? "caps" :
17451 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017452}
17453
17454/*
17455 * "spellsuggest()" function
17456 */
17457 static void
17458f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017459 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017460 typval_T *rettv;
17461{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017462#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017463 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017464 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017465 int maxcount;
17466 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017467 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017468 listitem_T *li;
17469 int need_capital = FALSE;
17470#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017473 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017475#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017476 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017477 {
17478 str = get_tv_string(&argvars[0]);
17479 if (argvars[1].v_type != VAR_UNKNOWN)
17480 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017481 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017482 if (maxcount <= 0)
17483 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017484 if (argvars[2].v_type != VAR_UNKNOWN)
17485 {
17486 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17487 if (typeerr)
17488 return;
17489 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017490 }
17491 else
17492 maxcount = 25;
17493
Bram Moolenaar4770d092006-01-12 23:22:24 +000017494 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017495
17496 for (i = 0; i < ga.ga_len; ++i)
17497 {
17498 str = ((char_u **)ga.ga_data)[i];
17499
17500 li = listitem_alloc();
17501 if (li == NULL)
17502 vim_free(str);
17503 else
17504 {
17505 li->li_tv.v_type = VAR_STRING;
17506 li->li_tv.v_lock = 0;
17507 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017508 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017509 }
17510 }
17511 ga_clear(&ga);
17512 }
17513#endif
17514}
17515
Bram Moolenaar0d660222005-01-07 21:51:51 +000017516 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017517f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 typval_T *argvars;
17519 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017520{
17521 char_u *str;
17522 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017523 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 regmatch_T regmatch;
17525 char_u patbuf[NUMBUFLEN];
17526 char_u *save_cpo;
17527 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017528 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017529 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017530 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017531
17532 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17533 save_cpo = p_cpo;
17534 p_cpo = (char_u *)"";
17535
17536 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017537 if (argvars[1].v_type != VAR_UNKNOWN)
17538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17540 if (pat == NULL)
17541 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017543 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017544 }
17545 if (pat == NULL || *pat == NUL)
17546 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017548 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017550 if (typeerr)
17551 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17554 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017557 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017558 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017559 if (*str == NUL)
17560 match = FALSE; /* empty item at the end */
17561 else
17562 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 if (match)
17564 end = regmatch.startp[0];
17565 else
17566 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017567 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17568 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017570 if (list_append_string(rettv->vval.v_list, str,
17571 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 }
17574 if (!match)
17575 break;
17576 /* Advance to just after the match. */
17577 if (regmatch.endp[0] > str)
17578 col = 0;
17579 else
17580 {
17581 /* Don't get stuck at the same match. */
17582#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017583 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584#else
17585 col = 1;
17586#endif
17587 }
17588 str = regmatch.endp[0];
17589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590
Bram Moolenaar473de612013-06-08 18:19:48 +020017591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595}
17596
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017597#ifdef FEAT_FLOAT
17598/*
17599 * "sqrt()" function
17600 */
17601 static void
17602f_sqrt(argvars, rettv)
17603 typval_T *argvars;
17604 typval_T *rettv;
17605{
17606 float_T f;
17607
17608 rettv->v_type = VAR_FLOAT;
17609 if (get_float_arg(argvars, &f) == OK)
17610 rettv->vval.v_float = sqrt(f);
17611 else
17612 rettv->vval.v_float = 0.0;
17613}
17614
17615/*
17616 * "str2float()" function
17617 */
17618 static void
17619f_str2float(argvars, rettv)
17620 typval_T *argvars;
17621 typval_T *rettv;
17622{
17623 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17624
17625 if (*p == '+')
17626 p = skipwhite(p + 1);
17627 (void)string2float(p, &rettv->vval.v_float);
17628 rettv->v_type = VAR_FLOAT;
17629}
17630#endif
17631
Bram Moolenaar2c932302006-03-18 21:42:09 +000017632/*
17633 * "str2nr()" function
17634 */
17635 static void
17636f_str2nr(argvars, rettv)
17637 typval_T *argvars;
17638 typval_T *rettv;
17639{
17640 int base = 10;
17641 char_u *p;
17642 long n;
17643
17644 if (argvars[1].v_type != VAR_UNKNOWN)
17645 {
17646 base = get_tv_number(&argvars[1]);
17647 if (base != 8 && base != 10 && base != 16)
17648 {
17649 EMSG(_(e_invarg));
17650 return;
17651 }
17652 }
17653
17654 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017655 if (*p == '+')
17656 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017657 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17658 rettv->vval.v_number = n;
17659}
17660
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661#ifdef HAVE_STRFTIME
17662/*
17663 * "strftime({format}[, {time}])" function
17664 */
17665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 typval_T *argvars;
17668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669{
17670 char_u result_buf[256];
17671 struct tm *curtime;
17672 time_t seconds;
17673 char_u *p;
17674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017678 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 seconds = time(NULL);
17680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 curtime = localtime(&seconds);
17683 /* MSVC returns NULL for an invalid value of seconds. */
17684 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 else
17687 {
17688# ifdef FEAT_MBYTE
17689 vimconv_T conv;
17690 char_u *enc;
17691
17692 conv.vc_type = CONV_NONE;
17693 enc = enc_locale();
17694 convert_setup(&conv, p_enc, enc);
17695 if (conv.vc_type != CONV_NONE)
17696 p = string_convert(&conv, p, NULL);
17697# endif
17698 if (p != NULL)
17699 (void)strftime((char *)result_buf, sizeof(result_buf),
17700 (char *)p, curtime);
17701 else
17702 result_buf[0] = NUL;
17703
17704# ifdef FEAT_MBYTE
17705 if (conv.vc_type != CONV_NONE)
17706 vim_free(p);
17707 convert_setup(&conv, enc, p_enc);
17708 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017709 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 else
17711# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017712 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713
17714# ifdef FEAT_MBYTE
17715 /* Release conversion descriptors */
17716 convert_setup(&conv, NULL, NULL);
17717 vim_free(enc);
17718# endif
17719 }
17720}
17721#endif
17722
17723/*
17724 * "stridx()" function
17725 */
17726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017728 typval_T *argvars;
17729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730{
17731 char_u buf[NUMBUFLEN];
17732 char_u *needle;
17733 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738 needle = get_tv_string_chk(&argvars[1]);
17739 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017741 if (needle == NULL || haystack == NULL)
17742 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 if (argvars[2].v_type != VAR_UNKNOWN)
17745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 int error = FALSE;
17747
17748 start_idx = get_tv_number_chk(&argvars[2], &error);
17749 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017751 if (start_idx >= 0)
17752 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017753 }
17754
17755 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17756 if (pos != NULL)
17757 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758}
17759
17760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017761 * "string()" function
17762 */
17763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 typval_T *argvars;
17766 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017767{
17768 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017769 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017772 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017773 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017774 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776}
17777
17778/*
17779 * "strlen()" function
17780 */
17781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017782f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 typval_T *argvars;
17784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017786 rettv->vval.v_number = (varnumber_T)(STRLEN(
17787 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788}
17789
17790/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017791 * "strchars()" function
17792 */
17793 static void
17794f_strchars(argvars, rettv)
17795 typval_T *argvars;
17796 typval_T *rettv;
17797{
17798 char_u *s = get_tv_string(&argvars[0]);
17799#ifdef FEAT_MBYTE
17800 varnumber_T len = 0;
17801
17802 while (*s != NUL)
17803 {
17804 mb_cptr2char_adv(&s);
17805 ++len;
17806 }
17807 rettv->vval.v_number = len;
17808#else
17809 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17810#endif
17811}
17812
17813/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017814 * "strdisplaywidth()" function
17815 */
17816 static void
17817f_strdisplaywidth(argvars, rettv)
17818 typval_T *argvars;
17819 typval_T *rettv;
17820{
17821 char_u *s = get_tv_string(&argvars[0]);
17822 int col = 0;
17823
17824 if (argvars[1].v_type != VAR_UNKNOWN)
17825 col = get_tv_number(&argvars[1]);
17826
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017827 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017828}
17829
17830/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017831 * "strwidth()" function
17832 */
17833 static void
17834f_strwidth(argvars, rettv)
17835 typval_T *argvars;
17836 typval_T *rettv;
17837{
17838 char_u *s = get_tv_string(&argvars[0]);
17839
17840 rettv->vval.v_number = (varnumber_T)(
17841#ifdef FEAT_MBYTE
17842 mb_string2cells(s, -1)
17843#else
17844 STRLEN(s)
17845#endif
17846 );
17847}
17848
17849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 * "strpart()" function
17851 */
17852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *argvars;
17855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
17857 char_u *p;
17858 int n;
17859 int len;
17860 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017861 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 slen = (int)STRLEN(p);
17865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017866 n = get_tv_number_chk(&argvars[1], &error);
17867 if (error)
17868 len = 0;
17869 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017870 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 else
17872 len = slen - n; /* default len: all bytes that are available. */
17873
17874 /*
17875 * Only return the overlap between the specified part and the actual
17876 * string.
17877 */
17878 if (n < 0)
17879 {
17880 len += n;
17881 n = 0;
17882 }
17883 else if (n > slen)
17884 n = slen;
17885 if (len < 0)
17886 len = 0;
17887 else if (n + len > slen)
17888 len = slen - n;
17889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890 rettv->v_type = VAR_STRING;
17891 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892}
17893
17894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 * "strridx()" function
17896 */
17897 static void
17898f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017899 typval_T *argvars;
17900 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017901{
17902 char_u buf[NUMBUFLEN];
17903 char_u *needle;
17904 char_u *haystack;
17905 char_u *rest;
17906 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017907 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017909 needle = get_tv_string_chk(&argvars[1]);
17910 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017911
17912 rettv->vval.v_number = -1;
17913 if (needle == NULL || haystack == NULL)
17914 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017915
17916 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017917 if (argvars[2].v_type != VAR_UNKNOWN)
17918 {
17919 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017921 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017922 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017923 }
17924 else
17925 end_idx = haystack_len;
17926
Bram Moolenaar0d660222005-01-07 21:51:51 +000017927 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017928 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017930 lastmatch = haystack + end_idx;
17931 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017933 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934 for (rest = haystack; *rest != '\0'; ++rest)
17935 {
17936 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017937 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 break;
17939 lastmatch = rest;
17940 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017941 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017942
17943 if (lastmatch == NULL)
17944 rettv->vval.v_number = -1;
17945 else
17946 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17947}
17948
17949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 * "strtrans()" function
17951 */
17952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017954 typval_T *argvars;
17955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 rettv->v_type = VAR_STRING;
17958 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959}
17960
17961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017962 * "submatch()" function
17963 */
17964 static void
17965f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017966 typval_T *argvars;
17967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017968{
Bram Moolenaar41571762014-04-02 19:00:58 +020017969 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017970 int no;
17971 int retList = 0;
17972
17973 no = (int)get_tv_number_chk(&argvars[0], &error);
17974 if (error)
17975 return;
17976 error = FALSE;
17977 if (argvars[1].v_type != VAR_UNKNOWN)
17978 retList = get_tv_number_chk(&argvars[1], &error);
17979 if (error)
17980 return;
17981
17982 if (retList == 0)
17983 {
17984 rettv->v_type = VAR_STRING;
17985 rettv->vval.v_string = reg_submatch(no);
17986 }
17987 else
17988 {
17989 rettv->v_type = VAR_LIST;
17990 rettv->vval.v_list = reg_submatch_list(no);
17991 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992}
17993
17994/*
17995 * "substitute()" function
17996 */
17997 static void
17998f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 typval_T *argvars;
18000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018001{
18002 char_u patbuf[NUMBUFLEN];
18003 char_u subbuf[NUMBUFLEN];
18004 char_u flagsbuf[NUMBUFLEN];
18005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018006 char_u *str = get_tv_string_chk(&argvars[0]);
18007 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18008 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18009 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18010
Bram Moolenaar0d660222005-01-07 21:51:51 +000018011 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018012 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18013 rettv->vval.v_string = NULL;
18014 else
18015 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016}
18017
18018/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018019 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018022f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018023 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025{
18026 int id = 0;
18027#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018028 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 long col;
18030 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018031 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018033 lnum = get_tv_lnum(argvars); /* -1 on type error */
18034 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18035 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018037 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018038 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018039 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040#endif
18041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018042 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
18045/*
18046 * "synIDattr(id, what [, mode])" function
18047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018050 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052{
18053 char_u *p = NULL;
18054#ifdef FEAT_SYN_HL
18055 int id;
18056 char_u *what;
18057 char_u *mode;
18058 char_u modebuf[NUMBUFLEN];
18059 int modec;
18060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061 id = get_tv_number(&argvars[0]);
18062 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018063 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018065 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018067 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 modec = 0; /* replace invalid with current */
18069 }
18070 else
18071 {
18072#ifdef FEAT_GUI
18073 if (gui.in_use)
18074 modec = 'g';
18075 else
18076#endif
18077 if (t_colors > 1)
18078 modec = 'c';
18079 else
18080 modec = 't';
18081 }
18082
18083
18084 switch (TOLOWER_ASC(what[0]))
18085 {
18086 case 'b':
18087 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18088 p = highlight_color(id, what, modec);
18089 else /* bold */
18090 p = highlight_has_attr(id, HL_BOLD, modec);
18091 break;
18092
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018093 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 p = highlight_color(id, what, modec);
18095 break;
18096
18097 case 'i':
18098 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18099 p = highlight_has_attr(id, HL_INVERSE, modec);
18100 else /* italic */
18101 p = highlight_has_attr(id, HL_ITALIC, modec);
18102 break;
18103
18104 case 'n': /* name */
18105 p = get_highlight_name(NULL, id - 1);
18106 break;
18107
18108 case 'r': /* reverse */
18109 p = highlight_has_attr(id, HL_INVERSE, modec);
18110 break;
18111
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018112 case 's':
18113 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18114 p = highlight_color(id, what, modec);
18115 else /* standout */
18116 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 break;
18118
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018119 case 'u':
18120 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18121 /* underline */
18122 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18123 else
18124 /* undercurl */
18125 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 break;
18127 }
18128
18129 if (p != NULL)
18130 p = vim_strsave(p);
18131#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 rettv->v_type = VAR_STRING;
18133 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134}
18135
18136/*
18137 * "synIDtrans(id)" function
18138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
18144 int id;
18145
18146#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018147 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148
18149 if (id > 0)
18150 id = syn_get_final_id(id);
18151 else
18152#endif
18153 id = 0;
18154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018159 * "synconcealed(lnum, col)" function
18160 */
18161 static void
18162f_synconcealed(argvars, rettv)
18163 typval_T *argvars UNUSED;
18164 typval_T *rettv;
18165{
18166#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18167 long lnum;
18168 long col;
18169 int syntax_flags = 0;
18170 int cchar;
18171 int matchid = 0;
18172 char_u str[NUMBUFLEN];
18173#endif
18174
18175 rettv->v_type = VAR_LIST;
18176 rettv->vval.v_list = NULL;
18177
18178#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18179 lnum = get_tv_lnum(argvars); /* -1 on type error */
18180 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18181
18182 vim_memset(str, NUL, sizeof(str));
18183
18184 if (rettv_list_alloc(rettv) != FAIL)
18185 {
18186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18187 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18188 && curwin->w_p_cole > 0)
18189 {
18190 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18191 syntax_flags = get_syntax_info(&matchid);
18192
18193 /* get the conceal character */
18194 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18195 {
18196 cchar = syn_get_sub_char();
18197 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18198 cchar = lcs_conceal;
18199 if (cchar != NUL)
18200 {
18201# ifdef FEAT_MBYTE
18202 if (has_mbyte)
18203 (*mb_char2bytes)(cchar, str);
18204 else
18205# endif
18206 str[0] = cchar;
18207 }
18208 }
18209 }
18210
18211 list_append_number(rettv->vval.v_list,
18212 (syntax_flags & HL_CONCEAL) != 0);
18213 /* -1 to auto-determine strlen */
18214 list_append_string(rettv->vval.v_list, str, -1);
18215 list_append_number(rettv->vval.v_list, matchid);
18216 }
18217#endif
18218}
18219
18220/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018221 * "synstack(lnum, col)" function
18222 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018223 static void
18224f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018225 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018226 typval_T *rettv;
18227{
18228#ifdef FEAT_SYN_HL
18229 long lnum;
18230 long col;
18231 int i;
18232 int id;
18233#endif
18234
18235 rettv->v_type = VAR_LIST;
18236 rettv->vval.v_list = NULL;
18237
18238#ifdef FEAT_SYN_HL
18239 lnum = get_tv_lnum(argvars); /* -1 on type error */
18240 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18241
18242 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018243 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018244 && rettv_list_alloc(rettv) != FAIL)
18245 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018246 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018247 for (i = 0; ; ++i)
18248 {
18249 id = syn_get_stack_item(i);
18250 if (id < 0)
18251 break;
18252 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18253 break;
18254 }
18255 }
18256#endif
18257}
18258
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018260get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 typval_T *argvars;
18262 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018263 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018265 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018267 char_u *infile = NULL;
18268 char_u buf[NUMBUFLEN];
18269 int err = FALSE;
18270 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018271 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018273 rettv->v_type = VAR_STRING;
18274 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018275 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018276 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018277
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018278 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018279 {
18280 /*
18281 * Write the string to a temp file, to be used for input of the shell
18282 * command.
18283 */
18284 if ((infile = vim_tempname('i')) == NULL)
18285 {
18286 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018287 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018288 }
18289
18290 fd = mch_fopen((char *)infile, WRITEBIN);
18291 if (fd == NULL)
18292 {
18293 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018294 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018295 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018296 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018297 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018298 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18299 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018300 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018301 else
18302 {
18303 p = get_tv_string_buf_chk(&argvars[1], buf);
18304 if (p == NULL)
18305 {
18306 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018307 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018308 }
18309 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18310 err = TRUE;
18311 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018312 if (fclose(fd) != 0)
18313 err = TRUE;
18314 if (err)
18315 {
18316 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018317 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018318 }
18319 }
18320
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018321 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018323 int len;
18324 listitem_T *li;
18325 char_u *s = NULL;
18326 char_u *start;
18327 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018328 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018330 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18331 SHELL_SILENT | SHELL_COOKED, &len);
18332 if (res == NULL)
18333 goto errret;
18334
18335 list = list_alloc();
18336 if (list == NULL)
18337 goto errret;
18338
18339 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018341 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018342 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018343 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018344 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018345
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018346 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018347 if (s == NULL)
18348 goto errret;
18349
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018350 for (p = s; start < end; ++p, ++start)
18351 *p = *start == NUL ? NL : *start;
18352 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018353
18354 li = listitem_alloc();
18355 if (li == NULL)
18356 {
18357 vim_free(s);
18358 goto errret;
18359 }
18360 li->li_tv.v_type = VAR_STRING;
18361 li->li_tv.vval.v_string = s;
18362 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018364
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018365 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018366 rettv->v_type = VAR_LIST;
18367 rettv->vval.v_list = list;
18368 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018370 else
18371 {
18372 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18373 SHELL_SILENT | SHELL_COOKED, NULL);
18374#ifdef USE_CR
18375 /* translate <CR> into <NL> */
18376 if (res != NULL)
18377 {
18378 char_u *s;
18379
18380 for (s = res; *s; ++s)
18381 {
18382 if (*s == CAR)
18383 *s = NL;
18384 }
18385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386#else
18387# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018388 /* translate <CR><NL> into <NL> */
18389 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018391 char_u *s, *d;
18392
18393 d = res;
18394 for (s = res; *s; ++s)
18395 {
18396 if (s[0] == CAR && s[1] == NL)
18397 ++s;
18398 *d++ = *s;
18399 }
18400 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402# endif
18403#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018404 rettv->vval.v_string = res;
18405 res = NULL;
18406 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018407
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018408errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018409 if (infile != NULL)
18410 {
18411 mch_remove(infile);
18412 vim_free(infile);
18413 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018414 if (res != NULL)
18415 vim_free(res);
18416 if (list != NULL)
18417 list_free(list, TRUE);
18418}
18419
18420/*
18421 * "system()" function
18422 */
18423 static void
18424f_system(argvars, rettv)
18425 typval_T *argvars;
18426 typval_T *rettv;
18427{
18428 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18429}
18430
18431/*
18432 * "systemlist()" function
18433 */
18434 static void
18435f_systemlist(argvars, rettv)
18436 typval_T *argvars;
18437 typval_T *rettv;
18438{
18439 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440}
18441
18442/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018443 * "tabpagebuflist()" function
18444 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018445 static void
18446f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018447 typval_T *argvars UNUSED;
18448 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018449{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018450#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018451 tabpage_T *tp;
18452 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018453
18454 if (argvars[0].v_type == VAR_UNKNOWN)
18455 wp = firstwin;
18456 else
18457 {
18458 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18459 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018460 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018461 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018462 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018463 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018464 for (; wp != NULL; wp = wp->w_next)
18465 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018466 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018467 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018468 }
18469#endif
18470}
18471
18472
18473/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018474 * "tabpagenr()" function
18475 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018476 static void
18477f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018478 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018479 typval_T *rettv;
18480{
18481 int nr = 1;
18482#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018483 char_u *arg;
18484
18485 if (argvars[0].v_type != VAR_UNKNOWN)
18486 {
18487 arg = get_tv_string_chk(&argvars[0]);
18488 nr = 0;
18489 if (arg != NULL)
18490 {
18491 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018492 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018493 else
18494 EMSG2(_(e_invexpr2), arg);
18495 }
18496 }
18497 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018498 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018499#endif
18500 rettv->vval.v_number = nr;
18501}
18502
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018503
18504#ifdef FEAT_WINDOWS
18505static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18506
18507/*
18508 * Common code for tabpagewinnr() and winnr().
18509 */
18510 static int
18511get_winnr(tp, argvar)
18512 tabpage_T *tp;
18513 typval_T *argvar;
18514{
18515 win_T *twin;
18516 int nr = 1;
18517 win_T *wp;
18518 char_u *arg;
18519
18520 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18521 if (argvar->v_type != VAR_UNKNOWN)
18522 {
18523 arg = get_tv_string_chk(argvar);
18524 if (arg == NULL)
18525 nr = 0; /* type error; errmsg already given */
18526 else if (STRCMP(arg, "$") == 0)
18527 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18528 else if (STRCMP(arg, "#") == 0)
18529 {
18530 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18531 if (twin == NULL)
18532 nr = 0;
18533 }
18534 else
18535 {
18536 EMSG2(_(e_invexpr2), arg);
18537 nr = 0;
18538 }
18539 }
18540
18541 if (nr > 0)
18542 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18543 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018544 {
18545 if (wp == NULL)
18546 {
18547 /* didn't find it in this tabpage */
18548 nr = 0;
18549 break;
18550 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018551 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018552 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018553 return nr;
18554}
18555#endif
18556
18557/*
18558 * "tabpagewinnr()" function
18559 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018560 static void
18561f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018562 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018563 typval_T *rettv;
18564{
18565 int nr = 1;
18566#ifdef FEAT_WINDOWS
18567 tabpage_T *tp;
18568
18569 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18570 if (tp == NULL)
18571 nr = 0;
18572 else
18573 nr = get_winnr(tp, &argvars[1]);
18574#endif
18575 rettv->vval.v_number = nr;
18576}
18577
18578
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018579/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018580 * "tagfiles()" function
18581 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018582 static void
18583f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018584 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018585 typval_T *rettv;
18586{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018587 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018588 tagname_T tn;
18589 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018590
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018591 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018592 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018593 fname = alloc(MAXPATHL);
18594 if (fname == NULL)
18595 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018596
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018597 for (first = TRUE; ; first = FALSE)
18598 if (get_tagfname(&tn, first, fname) == FAIL
18599 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018600 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018601 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018602 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018603}
18604
18605/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018606 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018607 */
18608 static void
18609f_taglist(argvars, rettv)
18610 typval_T *argvars;
18611 typval_T *rettv;
18612{
18613 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018614
18615 tag_pattern = get_tv_string(&argvars[0]);
18616
18617 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018618 if (*tag_pattern == NUL)
18619 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018620
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018621 if (rettv_list_alloc(rettv) == OK)
18622 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018623}
18624
18625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 * "tempname()" function
18627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632{
18633 static int x = 'A';
18634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 rettv->v_type = VAR_STRING;
18636 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
18638 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18639 * names. Skip 'I' and 'O', they are used for shell redirection. */
18640 do
18641 {
18642 if (x == 'Z')
18643 x = '0';
18644 else if (x == '9')
18645 x = 'A';
18646 else
18647 {
18648#ifdef EBCDIC
18649 if (x == 'I')
18650 x = 'J';
18651 else if (x == 'R')
18652 x = 'S';
18653 else
18654#endif
18655 ++x;
18656 }
18657 } while (x == 'I' || x == 'O');
18658}
18659
18660/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018661 * "test(list)" function: Just checking the walls...
18662 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018663 static void
18664f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018665 typval_T *argvars UNUSED;
18666 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018667{
18668 /* Used for unit testing. Change the code below to your liking. */
18669#if 0
18670 listitem_T *li;
18671 list_T *l;
18672 char_u *bad, *good;
18673
18674 if (argvars[0].v_type != VAR_LIST)
18675 return;
18676 l = argvars[0].vval.v_list;
18677 if (l == NULL)
18678 return;
18679 li = l->lv_first;
18680 if (li == NULL)
18681 return;
18682 bad = get_tv_string(&li->li_tv);
18683 li = li->li_next;
18684 if (li == NULL)
18685 return;
18686 good = get_tv_string(&li->li_tv);
18687 rettv->vval.v_number = test_edit_score(bad, good);
18688#endif
18689}
18690
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018691#ifdef FEAT_FLOAT
18692/*
18693 * "tan()" function
18694 */
18695 static void
18696f_tan(argvars, rettv)
18697 typval_T *argvars;
18698 typval_T *rettv;
18699{
18700 float_T f;
18701
18702 rettv->v_type = VAR_FLOAT;
18703 if (get_float_arg(argvars, &f) == OK)
18704 rettv->vval.v_float = tan(f);
18705 else
18706 rettv->vval.v_float = 0.0;
18707}
18708
18709/*
18710 * "tanh()" function
18711 */
18712 static void
18713f_tanh(argvars, rettv)
18714 typval_T *argvars;
18715 typval_T *rettv;
18716{
18717 float_T f;
18718
18719 rettv->v_type = VAR_FLOAT;
18720 if (get_float_arg(argvars, &f) == OK)
18721 rettv->vval.v_float = tanh(f);
18722 else
18723 rettv->vval.v_float = 0.0;
18724}
18725#endif
18726
Bram Moolenaard52d9742005-08-21 22:20:28 +000018727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 * "tolower(string)" function
18729 */
18730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 typval_T *argvars;
18733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 char_u *p;
18736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018737 p = vim_strsave(get_tv_string(&argvars[0]));
18738 rettv->v_type = VAR_STRING;
18739 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
18741 if (p != NULL)
18742 while (*p != NUL)
18743 {
18744#ifdef FEAT_MBYTE
18745 int l;
18746
18747 if (enc_utf8)
18748 {
18749 int c, lc;
18750
18751 c = utf_ptr2char(p);
18752 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018753 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 /* TODO: reallocate string when byte count changes. */
18755 if (utf_char2len(lc) == l)
18756 utf_char2bytes(lc, p);
18757 p += l;
18758 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018759 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 p += l; /* skip multi-byte character */
18761 else
18762#endif
18763 {
18764 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18765 ++p;
18766 }
18767 }
18768}
18769
18770/*
18771 * "toupper(string)" function
18772 */
18773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 typval_T *argvars;
18776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780}
18781
18782/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018783 * "tr(string, fromstr, tostr)" function
18784 */
18785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 typval_T *argvars;
18788 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018789{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018790 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018791 char_u *fromstr;
18792 char_u *tostr;
18793 char_u *p;
18794#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018795 int inlen;
18796 int fromlen;
18797 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018798 int idx;
18799 char_u *cpstr;
18800 int cplen;
18801 int first = TRUE;
18802#endif
18803 char_u buf[NUMBUFLEN];
18804 char_u buf2[NUMBUFLEN];
18805 garray_T ga;
18806
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018807 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18809 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018810
18811 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->v_type = VAR_STRING;
18813 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018814 if (fromstr == NULL || tostr == NULL)
18815 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018816 ga_init2(&ga, (int)sizeof(char), 80);
18817
18818#ifdef FEAT_MBYTE
18819 if (!has_mbyte)
18820#endif
18821 /* not multi-byte: fromstr and tostr must be the same length */
18822 if (STRLEN(fromstr) != STRLEN(tostr))
18823 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018824#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018825error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018826#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018827 EMSG2(_(e_invarg2), fromstr);
18828 ga_clear(&ga);
18829 return;
18830 }
18831
18832 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018833 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018834 {
18835#ifdef FEAT_MBYTE
18836 if (has_mbyte)
18837 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018838 inlen = (*mb_ptr2len)(in_str);
18839 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018840 cplen = inlen;
18841 idx = 0;
18842 for (p = fromstr; *p != NUL; p += fromlen)
18843 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018844 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018845 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018846 {
18847 for (p = tostr; *p != NUL; p += tolen)
18848 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018849 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018850 if (idx-- == 0)
18851 {
18852 cplen = tolen;
18853 cpstr = p;
18854 break;
18855 }
18856 }
18857 if (*p == NUL) /* tostr is shorter than fromstr */
18858 goto error;
18859 break;
18860 }
18861 ++idx;
18862 }
18863
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018864 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018865 {
18866 /* Check that fromstr and tostr have the same number of
18867 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018868 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018869 first = FALSE;
18870 for (p = tostr; *p != NUL; p += tolen)
18871 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018872 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018873 --idx;
18874 }
18875 if (idx != 0)
18876 goto error;
18877 }
18878
18879 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018880 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018881 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018882
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018883 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018884 }
18885 else
18886#endif
18887 {
18888 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018889 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018890 if (p != NULL)
18891 ga_append(&ga, tostr[p - fromstr]);
18892 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018893 ga_append(&ga, *in_str);
18894 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018895 }
18896 }
18897
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018898 /* add a terminating NUL */
18899 ga_grow(&ga, 1);
18900 ga_append(&ga, NUL);
18901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018902 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018903}
18904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018905#ifdef FEAT_FLOAT
18906/*
18907 * "trunc({float})" function
18908 */
18909 static void
18910f_trunc(argvars, rettv)
18911 typval_T *argvars;
18912 typval_T *rettv;
18913{
18914 float_T f;
18915
18916 rettv->v_type = VAR_FLOAT;
18917 if (get_float_arg(argvars, &f) == OK)
18918 /* trunc() is not in C90, use floor() or ceil() instead. */
18919 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18920 else
18921 rettv->vval.v_float = 0.0;
18922}
18923#endif
18924
Bram Moolenaar8299df92004-07-10 09:47:34 +000018925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 * "type(expr)" function
18927 */
18928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 typval_T *argvars;
18931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018933 int n;
18934
18935 switch (argvars[0].v_type)
18936 {
18937 case VAR_NUMBER: n = 0; break;
18938 case VAR_STRING: n = 1; break;
18939 case VAR_FUNC: n = 2; break;
18940 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018941 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018942#ifdef FEAT_FLOAT
18943 case VAR_FLOAT: n = 5; break;
18944#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018945 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18946 }
18947 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948}
18949
18950/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018951 * "undofile(name)" function
18952 */
18953 static void
18954f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018955 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018956 typval_T *rettv;
18957{
18958 rettv->v_type = VAR_STRING;
18959#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018960 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018961 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018962
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018963 if (*fname == NUL)
18964 {
18965 /* If there is no file name there will be no undo file. */
18966 rettv->vval.v_string = NULL;
18967 }
18968 else
18969 {
18970 char_u *ffname = FullName_save(fname, FALSE);
18971
18972 if (ffname != NULL)
18973 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18974 vim_free(ffname);
18975 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018976 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018977#else
18978 rettv->vval.v_string = NULL;
18979#endif
18980}
18981
18982/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018983 * "undotree()" function
18984 */
18985 static void
18986f_undotree(argvars, rettv)
18987 typval_T *argvars UNUSED;
18988 typval_T *rettv;
18989{
18990 if (rettv_dict_alloc(rettv) == OK)
18991 {
18992 dict_T *dict = rettv->vval.v_dict;
18993 list_T *list;
18994
Bram Moolenaar730cde92010-06-27 05:18:54 +020018995 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018996 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018997 dict_add_nr_str(dict, "save_last",
18998 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018999 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19000 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019001 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019002
19003 list = list_alloc();
19004 if (list != NULL)
19005 {
19006 u_eval_tree(curbuf->b_u_oldhead, list);
19007 dict_add_list(dict, "entries", list);
19008 }
19009 }
19010}
19011
19012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019013 * "values(dict)" function
19014 */
19015 static void
19016f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T *argvars;
19018 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019019{
19020 dict_list(argvars, rettv, 1);
19021}
19022
19023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 * "virtcol(string)" function
19025 */
19026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 typval_T *argvars;
19029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030{
19031 colnr_T vcol = 0;
19032 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019033 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019035 fp = var2fpos(&argvars[0], FALSE, &fnum);
19036 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19037 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 {
19039 getvvcol(curwin, fp, NULL, NULL, &vcol);
19040 ++vcol;
19041 }
19042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044}
19045
19046/*
19047 * "visualmode()" function
19048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019051 typval_T *argvars UNUSED;
19052 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 char_u str[2];
19055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 str[0] = curbuf->b_visual_mode_eval;
19058 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060
19061 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019062 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064}
19065
19066/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019067 * "wildmenumode()" function
19068 */
19069 static void
19070f_wildmenumode(argvars, rettv)
19071 typval_T *argvars UNUSED;
19072 typval_T *rettv UNUSED;
19073{
19074#ifdef FEAT_WILDMENU
19075 if (wild_menu_showing)
19076 rettv->vval.v_number = 1;
19077#endif
19078}
19079
19080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 * "winbufnr(nr)" function
19082 */
19083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 typval_T *argvars;
19086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087{
19088 win_T *wp;
19089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019090 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095}
19096
19097/*
19098 * "wincol()" function
19099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019102 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104{
19105 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107}
19108
19109/*
19110 * "winheight(nr)" function
19111 */
19112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 typval_T *argvars;
19115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116{
19117 win_T *wp;
19118
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019119 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124}
19125
19126/*
19127 * "winline()" function
19128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133{
19134 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136}
19137
19138/*
19139 * "winnr()" function
19140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019142f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145{
19146 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019147
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019149 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019151 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152}
19153
19154/*
19155 * "winrestcmd()" function
19156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019158f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161{
19162#ifdef FEAT_WINDOWS
19163 win_T *wp;
19164 int winnr = 1;
19165 garray_T ga;
19166 char_u buf[50];
19167
19168 ga_init2(&ga, (int)sizeof(char), 70);
19169 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19170 {
19171 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19172 ga_concat(&ga, buf);
19173# ifdef FEAT_VERTSPLIT
19174 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19175 ga_concat(&ga, buf);
19176# endif
19177 ++winnr;
19178 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019179 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019189 * "winrestview()" function
19190 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019191 static void
19192f_winrestview(argvars, rettv)
19193 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019194 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019195{
19196 dict_T *dict;
19197
19198 if (argvars[0].v_type != VAR_DICT
19199 || (dict = argvars[0].vval.v_dict) == NULL)
19200 EMSG(_(e_invarg));
19201 else
19202 {
19203 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19204 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19205#ifdef FEAT_VIRTUALEDIT
19206 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19207#endif
19208 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019209 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019210
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019211 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019212#ifdef FEAT_DIFF
19213 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19214#endif
19215 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19216 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19217
19218 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019219 win_new_height(curwin, curwin->w_height);
19220# ifdef FEAT_VERTSPLIT
19221 win_new_width(curwin, W_WIDTH(curwin));
19222# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019223 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019224
19225 if (curwin->w_topline == 0)
19226 curwin->w_topline = 1;
19227 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19228 curwin->w_topline = curbuf->b_ml.ml_line_count;
19229#ifdef FEAT_DIFF
19230 check_topfill(curwin, TRUE);
19231#endif
19232 }
19233}
19234
19235/*
19236 * "winsaveview()" function
19237 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019238 static void
19239f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019240 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019241 typval_T *rettv;
19242{
19243 dict_T *dict;
19244
Bram Moolenaara800b422010-06-27 01:15:55 +020019245 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019246 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019247 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019248
19249 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19250 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19251#ifdef FEAT_VIRTUALEDIT
19252 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19253#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019254 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019255 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19256
19257 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19258#ifdef FEAT_DIFF
19259 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19260#endif
19261 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19262 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19263}
19264
19265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 * "winwidth(nr)" function
19267 */
19268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019269f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 typval_T *argvars;
19271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272{
19273 win_T *wp;
19274
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019275 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 else
19279#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019280 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019282 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283#endif
19284}
19285
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019287 * Write list of strings to file
19288 */
19289 static int
19290write_list(fd, list, binary)
19291 FILE *fd;
19292 list_T *list;
19293 int binary;
19294{
19295 listitem_T *li;
19296 int c;
19297 int ret = OK;
19298 char_u *s;
19299
19300 for (li = list->lv_first; li != NULL; li = li->li_next)
19301 {
19302 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19303 {
19304 if (*s == '\n')
19305 c = putc(NUL, fd);
19306 else
19307 c = putc(*s, fd);
19308 if (c == EOF)
19309 {
19310 ret = FAIL;
19311 break;
19312 }
19313 }
19314 if (!binary || li->li_next != NULL)
19315 if (putc('\n', fd) == EOF)
19316 {
19317 ret = FAIL;
19318 break;
19319 }
19320 if (ret == FAIL)
19321 {
19322 EMSG(_(e_write));
19323 break;
19324 }
19325 }
19326 return ret;
19327}
19328
19329/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019330 * "writefile()" function
19331 */
19332 static void
19333f_writefile(argvars, rettv)
19334 typval_T *argvars;
19335 typval_T *rettv;
19336{
19337 int binary = FALSE;
19338 char_u *fname;
19339 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019340 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019341
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019342 if (check_restricted() || check_secure())
19343 return;
19344
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019345 if (argvars[0].v_type != VAR_LIST)
19346 {
19347 EMSG2(_(e_listarg), "writefile()");
19348 return;
19349 }
19350 if (argvars[0].vval.v_list == NULL)
19351 return;
19352
19353 if (argvars[2].v_type != VAR_UNKNOWN
19354 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19355 binary = TRUE;
19356
19357 /* Always open the file in binary mode, library functions have a mind of
19358 * their own about CR-LF conversion. */
19359 fname = get_tv_string(&argvars[1]);
19360 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19361 {
19362 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19363 ret = -1;
19364 }
19365 else
19366 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019367 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19368 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019369 fclose(fd);
19370 }
19371
19372 rettv->vval.v_number = ret;
19373}
19374
19375/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019376 * "xor(expr, expr)" function
19377 */
19378 static void
19379f_xor(argvars, rettv)
19380 typval_T *argvars;
19381 typval_T *rettv;
19382{
19383 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19384 ^ get_tv_number_chk(&argvars[1], NULL);
19385}
19386
19387
19388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019390 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 */
19392 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019393var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019395 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019396 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019398 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019400 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401
Bram Moolenaara5525202006-03-02 22:52:09 +000019402 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019403 if (varp->v_type == VAR_LIST)
19404 {
19405 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019406 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019407 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019408 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019409
19410 l = varp->vval.v_list;
19411 if (l == NULL)
19412 return NULL;
19413
19414 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019415 pos.lnum = list_find_nr(l, 0L, &error);
19416 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019417 return NULL; /* invalid line number */
19418
19419 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019420 pos.col = list_find_nr(l, 1L, &error);
19421 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019422 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019423 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019424
19425 /* We accept "$" for the column number: last column. */
19426 li = list_find(l, 1L);
19427 if (li != NULL && li->li_tv.v_type == VAR_STRING
19428 && li->li_tv.vval.v_string != NULL
19429 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19430 pos.col = len + 1;
19431
Bram Moolenaara5525202006-03-02 22:52:09 +000019432 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019433 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019434 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019435 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019436
Bram Moolenaara5525202006-03-02 22:52:09 +000019437#ifdef FEAT_VIRTUALEDIT
19438 /* Get the virtual offset. Defaults to zero. */
19439 pos.coladd = list_find_nr(l, 2L, &error);
19440 if (error)
19441 pos.coladd = 0;
19442#endif
19443
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019444 return &pos;
19445 }
19446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019447 name = get_tv_string_chk(varp);
19448 if (name == NULL)
19449 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019450 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019452 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19453 {
19454 if (VIsual_active)
19455 return &VIsual;
19456 return &curwin->w_cursor;
19457 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019458 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019460 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19462 return NULL;
19463 return pp;
19464 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019465
19466#ifdef FEAT_VIRTUALEDIT
19467 pos.coladd = 0;
19468#endif
19469
Bram Moolenaar477933c2007-07-17 14:32:23 +000019470 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019471 {
19472 pos.col = 0;
19473 if (name[1] == '0') /* "w0": first visible line */
19474 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019475 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019476 pos.lnum = curwin->w_topline;
19477 return &pos;
19478 }
19479 else if (name[1] == '$') /* "w$": last visible line */
19480 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019481 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019482 pos.lnum = curwin->w_botline - 1;
19483 return &pos;
19484 }
19485 }
19486 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019488 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
19490 pos.lnum = curbuf->b_ml.ml_line_count;
19491 pos.col = 0;
19492 }
19493 else
19494 {
19495 pos.lnum = curwin->w_cursor.lnum;
19496 pos.col = (colnr_T)STRLEN(ml_get_curline());
19497 }
19498 return &pos;
19499 }
19500 return NULL;
19501}
19502
19503/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019504 * Convert list in "arg" into a position and optional file number.
19505 * When "fnump" is NULL there is no file number, only 3 items.
19506 * Note that the column is passed on as-is, the caller may want to decrement
19507 * it to use 1 for the first column.
19508 * Return FAIL when conversion is not possible, doesn't check the position for
19509 * validity.
19510 */
19511 static int
19512list2fpos(arg, posp, fnump)
19513 typval_T *arg;
19514 pos_T *posp;
19515 int *fnump;
19516{
19517 list_T *l = arg->vval.v_list;
19518 long i = 0;
19519 long n;
19520
Bram Moolenaarbde35262006-07-23 20:12:24 +000019521 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19522 * when "fnump" isn't NULL and "coladd" is optional. */
19523 if (arg->v_type != VAR_LIST
19524 || l == NULL
19525 || l->lv_len < (fnump == NULL ? 2 : 3)
19526 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019527 return FAIL;
19528
19529 if (fnump != NULL)
19530 {
19531 n = list_find_nr(l, i++, NULL); /* fnum */
19532 if (n < 0)
19533 return FAIL;
19534 if (n == 0)
19535 n = curbuf->b_fnum; /* current buffer */
19536 *fnump = n;
19537 }
19538
19539 n = list_find_nr(l, i++, NULL); /* lnum */
19540 if (n < 0)
19541 return FAIL;
19542 posp->lnum = n;
19543
19544 n = list_find_nr(l, i++, NULL); /* col */
19545 if (n < 0)
19546 return FAIL;
19547 posp->col = n;
19548
19549#ifdef FEAT_VIRTUALEDIT
19550 n = list_find_nr(l, i, NULL);
19551 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019552 posp->coladd = 0;
19553 else
19554 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019555#endif
19556
19557 return OK;
19558}
19559
19560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 * Get the length of an environment variable name.
19562 * Advance "arg" to the first character after the name.
19563 * Return 0 for error.
19564 */
19565 static int
19566get_env_len(arg)
19567 char_u **arg;
19568{
19569 char_u *p;
19570 int len;
19571
19572 for (p = *arg; vim_isIDc(*p); ++p)
19573 ;
19574 if (p == *arg) /* no name found */
19575 return 0;
19576
19577 len = (int)(p - *arg);
19578 *arg = p;
19579 return len;
19580}
19581
19582/*
19583 * Get the length of the name of a function or internal variable.
19584 * "arg" is advanced to the first non-white character after the name.
19585 * Return 0 if something is wrong.
19586 */
19587 static int
19588get_id_len(arg)
19589 char_u **arg;
19590{
19591 char_u *p;
19592 int len;
19593
19594 /* Find the end of the name. */
19595 for (p = *arg; eval_isnamec(*p); ++p)
19596 ;
19597 if (p == *arg) /* no name found */
19598 return 0;
19599
19600 len = (int)(p - *arg);
19601 *arg = skipwhite(p);
19602
19603 return len;
19604}
19605
19606/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019607 * Get the length of the name of a variable or function.
19608 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019610 * Return -1 if curly braces expansion failed.
19611 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 * If the name contains 'magic' {}'s, expand them and return the
19613 * expanded name in an allocated string via 'alias' - caller must free.
19614 */
19615 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019616get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 char_u **arg;
19618 char_u **alias;
19619 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019620 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
19622 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 char_u *p;
19624 char_u *expr_start;
19625 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626
19627 *alias = NULL; /* default to no alias */
19628
19629 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19630 && (*arg)[2] == (int)KE_SNR)
19631 {
19632 /* hard coded <SNR>, already translated */
19633 *arg += 3;
19634 return get_id_len(arg) + 3;
19635 }
19636 len = eval_fname_script(*arg);
19637 if (len > 0)
19638 {
19639 /* literal "<SID>", "s:" or "<SNR>" */
19640 *arg += len;
19641 }
19642
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019646 p = find_name_end(*arg, &expr_start, &expr_end,
19647 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 if (expr_start != NULL)
19649 {
19650 char_u *temp_string;
19651
19652 if (!evaluate)
19653 {
19654 len += (int)(p - *arg);
19655 *arg = skipwhite(p);
19656 return len;
19657 }
19658
19659 /*
19660 * Include any <SID> etc in the expanded string:
19661 * Thus the -len here.
19662 */
19663 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19664 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019665 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 *alias = temp_string;
19667 *arg = skipwhite(p);
19668 return (int)STRLEN(temp_string);
19669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670
19671 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019672 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 EMSG2(_(e_invexpr2), *arg);
19674
19675 return len;
19676}
19677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678/*
19679 * Find the end of a variable or function name, taking care of magic braces.
19680 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19681 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019682 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 * Return a pointer to just after the name. Equal to "arg" if there is no
19684 * valid name.
19685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019687find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 char_u *arg;
19689 char_u **expr_start;
19690 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019691 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693 int mb_nest = 0;
19694 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 char_u *p;
19696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019697 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 *expr_start = NULL;
19700 *expr_end = NULL;
19701 }
19702
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019703 /* Quick check for valid starting character. */
19704 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19705 return arg;
19706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 for (p = arg; *p != NUL
19708 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019709 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019710 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019712 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019714 if (*p == '\'')
19715 {
19716 /* skip over 'string' to avoid counting [ and ] inside it. */
19717 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19718 ;
19719 if (*p == NUL)
19720 break;
19721 }
19722 else if (*p == '"')
19723 {
19724 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19725 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19726 if (*p == '\\' && p[1] != NUL)
19727 ++p;
19728 if (*p == NUL)
19729 break;
19730 }
19731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 if (*p == '[')
19735 ++br_nest;
19736 else if (*p == ']')
19737 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019740 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 if (*p == '{')
19743 {
19744 mb_nest++;
19745 if (expr_start != NULL && *expr_start == NULL)
19746 *expr_start = p;
19747 }
19748 else if (*p == '}')
19749 {
19750 mb_nest--;
19751 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19752 *expr_end = p;
19753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 }
19756
19757 return p;
19758}
19759
19760/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019761 * Expands out the 'magic' {}'s in a variable/function name.
19762 * Note that this can call itself recursively, to deal with
19763 * constructs like foo{bar}{baz}{bam}
19764 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19765 * "in_start" ^
19766 * "expr_start" ^
19767 * "expr_end" ^
19768 * "in_end" ^
19769 *
19770 * Returns a new allocated string, which the caller must free.
19771 * Returns NULL for failure.
19772 */
19773 static char_u *
19774make_expanded_name(in_start, expr_start, expr_end, in_end)
19775 char_u *in_start;
19776 char_u *expr_start;
19777 char_u *expr_end;
19778 char_u *in_end;
19779{
19780 char_u c1;
19781 char_u *retval = NULL;
19782 char_u *temp_result;
19783 char_u *nextcmd = NULL;
19784
19785 if (expr_end == NULL || in_end == NULL)
19786 return NULL;
19787 *expr_start = NUL;
19788 *expr_end = NUL;
19789 c1 = *in_end;
19790 *in_end = NUL;
19791
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019792 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019793 if (temp_result != NULL && nextcmd == NULL)
19794 {
19795 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19796 + (in_end - expr_end) + 1));
19797 if (retval != NULL)
19798 {
19799 STRCPY(retval, in_start);
19800 STRCAT(retval, temp_result);
19801 STRCAT(retval, expr_end + 1);
19802 }
19803 }
19804 vim_free(temp_result);
19805
19806 *in_end = c1; /* put char back for error messages */
19807 *expr_start = '{';
19808 *expr_end = '}';
19809
19810 if (retval != NULL)
19811 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019812 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019813 if (expr_start != NULL)
19814 {
19815 /* Further expansion! */
19816 temp_result = make_expanded_name(retval, expr_start,
19817 expr_end, temp_result);
19818 vim_free(retval);
19819 retval = temp_result;
19820 }
19821 }
19822
19823 return retval;
19824}
19825
19826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019828 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 */
19830 static int
19831eval_isnamec(c)
19832 int c;
19833{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019834 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19835}
19836
19837/*
19838 * Return TRUE if character "c" can be used as the first character in a
19839 * variable or function name (excluding '{' and '}').
19840 */
19841 static int
19842eval_isnamec1(c)
19843 int c;
19844{
19845 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846}
19847
19848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 * Set number v: variable to "val".
19850 */
19851 void
19852set_vim_var_nr(idx, val)
19853 int idx;
19854 long val;
19855{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019856 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857}
19858
19859/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019860 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 */
19862 long
19863get_vim_var_nr(idx)
19864 int idx;
19865{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019866 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867}
19868
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019869/*
19870 * Get string v: variable value. Uses a static buffer, can only be used once.
19871 */
19872 char_u *
19873get_vim_var_str(idx)
19874 int idx;
19875{
19876 return get_tv_string(&vimvars[idx].vv_tv);
19877}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019878
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019880 * Get List v: variable value. Caller must take care of reference count when
19881 * needed.
19882 */
19883 list_T *
19884get_vim_var_list(idx)
19885 int idx;
19886{
19887 return vimvars[idx].vv_list;
19888}
19889
19890/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019891 * Set v:char to character "c".
19892 */
19893 void
19894set_vim_var_char(c)
19895 int c;
19896{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019897 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019898
19899#ifdef FEAT_MBYTE
19900 if (has_mbyte)
19901 buf[(*mb_char2bytes)(c, buf)] = NUL;
19902 else
19903#endif
19904 {
19905 buf[0] = c;
19906 buf[1] = NUL;
19907 }
19908 set_vim_var_string(VV_CHAR, buf, -1);
19909}
19910
19911/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019912 * Set v:count to "count" and v:count1 to "count1".
19913 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 */
19915 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019916set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 long count;
19918 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019919 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019921 if (set_prevcount)
19922 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019923 vimvars[VV_COUNT].vv_nr = count;
19924 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925}
19926
19927/*
19928 * Set string v: variable to a copy of "val".
19929 */
19930 void
19931set_vim_var_string(idx, val, len)
19932 int idx;
19933 char_u *val;
19934 int len; /* length of "val" to use or -1 (whole string) */
19935{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019936 /* Need to do this (at least) once, since we can't initialize a union.
19937 * Will always be invoked when "v:progname" is set. */
19938 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19939
Bram Moolenaare9a41262005-01-15 22:18:47 +000019940 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019942 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019944 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019946 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947}
19948
19949/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019950 * Set List v: variable to "val".
19951 */
19952 void
19953set_vim_var_list(idx, val)
19954 int idx;
19955 list_T *val;
19956{
19957 list_unref(vimvars[idx].vv_list);
19958 vimvars[idx].vv_list = val;
19959 if (val != NULL)
19960 ++val->lv_refcount;
19961}
19962
19963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 * Set v:register if needed.
19965 */
19966 void
19967set_reg_var(c)
19968 int c;
19969{
19970 char_u regname;
19971
19972 if (c == 0 || c == ' ')
19973 regname = '"';
19974 else
19975 regname = c;
19976 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019977 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 set_vim_var_string(VV_REG, &regname, 1);
19979}
19980
19981/*
19982 * Get or set v:exception. If "oldval" == NULL, return the current value.
19983 * Otherwise, restore the value to "oldval" and return NULL.
19984 * Must always be called in pairs to save and restore v:exception! Does not
19985 * take care of memory allocations.
19986 */
19987 char_u *
19988v_exception(oldval)
19989 char_u *oldval;
19990{
19991 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019992 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993
Bram Moolenaare9a41262005-01-15 22:18:47 +000019994 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 return NULL;
19996}
19997
19998/*
19999 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20000 * Otherwise, restore the value to "oldval" and return NULL.
20001 * Must always be called in pairs to save and restore v:throwpoint! Does not
20002 * take care of memory allocations.
20003 */
20004 char_u *
20005v_throwpoint(oldval)
20006 char_u *oldval;
20007{
20008 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020009 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010
Bram Moolenaare9a41262005-01-15 22:18:47 +000020011 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 return NULL;
20013}
20014
20015#if defined(FEAT_AUTOCMD) || defined(PROTO)
20016/*
20017 * Set v:cmdarg.
20018 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20019 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20020 * Must always be called in pairs!
20021 */
20022 char_u *
20023set_cmdarg(eap, oldarg)
20024 exarg_T *eap;
20025 char_u *oldarg;
20026{
20027 char_u *oldval;
20028 char_u *newval;
20029 unsigned len;
20030
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020032 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020034 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020035 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020036 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
20038
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020039 if (eap->force_bin == FORCE_BIN)
20040 len = 6;
20041 else if (eap->force_bin == FORCE_NOBIN)
20042 len = 8;
20043 else
20044 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020045
20046 if (eap->read_edit)
20047 len += 7;
20048
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020049 if (eap->force_ff != 0)
20050 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20051# ifdef FEAT_MBYTE
20052 if (eap->force_enc != 0)
20053 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020054 if (eap->bad_char != 0)
20055 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020056# endif
20057
20058 newval = alloc(len + 1);
20059 if (newval == NULL)
20060 return NULL;
20061
20062 if (eap->force_bin == FORCE_BIN)
20063 sprintf((char *)newval, " ++bin");
20064 else if (eap->force_bin == FORCE_NOBIN)
20065 sprintf((char *)newval, " ++nobin");
20066 else
20067 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020068
20069 if (eap->read_edit)
20070 STRCAT(newval, " ++edit");
20071
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020072 if (eap->force_ff != 0)
20073 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20074 eap->cmd + eap->force_ff);
20075# ifdef FEAT_MBYTE
20076 if (eap->force_enc != 0)
20077 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20078 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020079 if (eap->bad_char == BAD_KEEP)
20080 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20081 else if (eap->bad_char == BAD_DROP)
20082 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20083 else if (eap->bad_char != 0)
20084 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020085# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020086 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020087 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089#endif
20090
20091/*
20092 * Get the value of internal variable "name".
20093 * Return OK or FAIL.
20094 */
20095 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020096get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 char_u *name;
20098 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020100 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020101 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102{
20103 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 typval_T *tv = NULL;
20105 typval_T atv;
20106 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
20109 /* truncate the name, so that we can use strcmp() */
20110 cc = name[len];
20111 name[len] = NUL;
20112
20113 /*
20114 * Check for "b:changedtick".
20115 */
20116 if (STRCMP(name, "b:changedtick") == 0)
20117 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020118 atv.v_type = VAR_NUMBER;
20119 atv.vval.v_number = curbuf->b_changedtick;
20120 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122
20123 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 * Check for user-defined variables.
20125 */
20126 else
20127 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020128 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 }
20132
Bram Moolenaare9a41262005-01-15 22:18:47 +000020133 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020135 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020136 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 ret = FAIL;
20138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020140 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141
20142 name[len] = cc;
20143
20144 return ret;
20145}
20146
20147/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020148 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20149 * Also handle function call with Funcref variable: func(expr)
20150 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20151 */
20152 static int
20153handle_subscript(arg, rettv, evaluate, verbose)
20154 char_u **arg;
20155 typval_T *rettv;
20156 int evaluate; /* do more than finding the end */
20157 int verbose; /* give error messages */
20158{
20159 int ret = OK;
20160 dict_T *selfdict = NULL;
20161 char_u *s;
20162 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020163 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020164
20165 while (ret == OK
20166 && (**arg == '['
20167 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020168 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020169 && !vim_iswhite(*(*arg - 1)))
20170 {
20171 if (**arg == '(')
20172 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020173 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020174 if (evaluate)
20175 {
20176 functv = *rettv;
20177 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020178
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020179 /* Invoke the function. Recursive! */
20180 s = functv.vval.v_string;
20181 }
20182 else
20183 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020184 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020185 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20186 &len, evaluate, selfdict);
20187
20188 /* Clear the funcref afterwards, so that deleting it while
20189 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020190 if (evaluate)
20191 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020192
20193 /* Stop the expression evaluation when immediately aborting on
20194 * error, or when an interrupt occurred or an exception was thrown
20195 * but not caught. */
20196 if (aborting())
20197 {
20198 if (ret == OK)
20199 clear_tv(rettv);
20200 ret = FAIL;
20201 }
20202 dict_unref(selfdict);
20203 selfdict = NULL;
20204 }
20205 else /* **arg == '[' || **arg == '.' */
20206 {
20207 dict_unref(selfdict);
20208 if (rettv->v_type == VAR_DICT)
20209 {
20210 selfdict = rettv->vval.v_dict;
20211 if (selfdict != NULL)
20212 ++selfdict->dv_refcount;
20213 }
20214 else
20215 selfdict = NULL;
20216 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20217 {
20218 clear_tv(rettv);
20219 ret = FAIL;
20220 }
20221 }
20222 }
20223 dict_unref(selfdict);
20224 return ret;
20225}
20226
20227/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020228 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020229 * value).
20230 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020233{
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020235}
20236
20237/*
20238 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 * The string "s" must have been allocated, it is consumed.
20240 * Return NULL for out of memory, the variable otherwise.
20241 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 char_u *s;
20245{
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248 rettv = alloc_tv();
20249 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251 rettv->v_type = VAR_STRING;
20252 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 }
20254 else
20255 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257}
20258
20259/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020260 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020262 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
20266 if (varp != NULL)
20267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020268 switch (varp->v_type)
20269 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020270 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020271 func_unref(varp->vval.v_string);
20272 /*FALLTHROUGH*/
20273 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020274 vim_free(varp->vval.v_string);
20275 break;
20276 case VAR_LIST:
20277 list_unref(varp->vval.v_list);
20278 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020279 case VAR_DICT:
20280 dict_unref(varp->vval.v_dict);
20281 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020282 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020283#ifdef FEAT_FLOAT
20284 case VAR_FLOAT:
20285#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020286 case VAR_UNKNOWN:
20287 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020288 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020289 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020290 break;
20291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 vim_free(varp);
20293 }
20294}
20295
20296/*
20297 * Free the memory for a variable value and set the value to NULL or 0.
20298 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020299 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302{
20303 if (varp != NULL)
20304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020305 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020307 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020308 func_unref(varp->vval.v_string);
20309 /*FALLTHROUGH*/
20310 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020311 vim_free(varp->vval.v_string);
20312 varp->vval.v_string = NULL;
20313 break;
20314 case VAR_LIST:
20315 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020316 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020318 case VAR_DICT:
20319 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020320 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020321 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020322 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020323 varp->vval.v_number = 0;
20324 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020325#ifdef FEAT_FLOAT
20326 case VAR_FLOAT:
20327 varp->vval.v_float = 0.0;
20328 break;
20329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 case VAR_UNKNOWN:
20331 break;
20332 default:
20333 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020335 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 }
20337}
20338
20339/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020340 * Set the value of a variable to NULL without freeing items.
20341 */
20342 static void
20343init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020344 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020345{
20346 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348}
20349
20350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 * Get the number value of a variable.
20352 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020353 * For incompatible types, return 0.
20354 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20355 * caller of incompatible types: it sets *denote to TRUE if "denote"
20356 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 */
20358 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020362 int error = FALSE;
20363
20364 return get_tv_number_chk(varp, &error); /* return 0L on error */
20365}
20366
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020367 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020368get_tv_number_chk(varp, denote)
20369 typval_T *varp;
20370 int *denote;
20371{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020372 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020374 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020376 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020377 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020378#ifdef FEAT_FLOAT
20379 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020380 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020381 break;
20382#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020383 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020384 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020385 break;
20386 case VAR_STRING:
20387 if (varp->vval.v_string != NULL)
20388 vim_str2nr(varp->vval.v_string, NULL, NULL,
20389 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020390 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020391 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020392 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020393 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020394 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020395 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020396 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020398 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020401 if (denote == NULL) /* useful for values that must be unsigned */
20402 n = -1;
20403 else
20404 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020405 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406}
20407
20408/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020409 * Get the lnum from the first argument.
20410 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020411 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 */
20413 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416{
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 linenr_T lnum;
20419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020420 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 if (lnum == 0) /* no valid number, try using line() */
20422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 rettv.v_type = VAR_NUMBER;
20424 f_line(argvars, &rettv);
20425 lnum = rettv.vval.v_number;
20426 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 }
20428 return lnum;
20429}
20430
20431/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020432 * Get the lnum from the first argument.
20433 * Also accepts "$", then "buf" is used.
20434 * Returns 0 on error.
20435 */
20436 static linenr_T
20437get_tv_lnum_buf(argvars, buf)
20438 typval_T *argvars;
20439 buf_T *buf;
20440{
20441 if (argvars[0].v_type == VAR_STRING
20442 && argvars[0].vval.v_string != NULL
20443 && argvars[0].vval.v_string[0] == '$'
20444 && buf != NULL)
20445 return buf->b_ml.ml_line_count;
20446 return get_tv_number_chk(&argvars[0], NULL);
20447}
20448
20449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 * Get the string value of a variable.
20451 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020452 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20453 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 * If the String variable has never been set, return an empty string.
20455 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020456 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20457 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 */
20459 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020460get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462{
20463 static char_u mybuf[NUMBUFLEN];
20464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020465 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466}
20467
20468 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020471 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020473 char_u *res = get_tv_string_buf_chk(varp, buf);
20474
20475 return res != NULL ? res : (char_u *)"";
20476}
20477
Bram Moolenaar7d647822014-04-05 21:28:56 +020020478/*
20479 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20480 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020481 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020482get_tv_string_chk(varp)
20483 typval_T *varp;
20484{
20485 static char_u mybuf[NUMBUFLEN];
20486
20487 return get_tv_string_buf_chk(varp, mybuf);
20488}
20489
20490 static char_u *
20491get_tv_string_buf_chk(varp, buf)
20492 typval_T *varp;
20493 char_u *buf;
20494{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020495 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020497 case VAR_NUMBER:
20498 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20499 return buf;
20500 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020501 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020502 break;
20503 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020504 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020505 break;
20506 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020507 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020508 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020509#ifdef FEAT_FLOAT
20510 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020511 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020512 break;
20513#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020514 case VAR_STRING:
20515 if (varp->vval.v_string != NULL)
20516 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020517 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020518 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523}
20524
20525/*
20526 * Find variable "name" in the list of variables.
20527 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020528 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020529 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020530 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020533find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020536 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020539 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540
Bram Moolenaara7043832005-01-21 11:56:39 +000020541 ht = find_var_ht(name, &varname);
20542 if (htp != NULL)
20543 *htp = ht;
20544 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020546 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547}
20548
20549/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020550 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020551 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020554find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020555 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020556 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020557 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020558 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020559{
Bram Moolenaar33570922005-01-25 22:26:29 +000020560 hashitem_T *hi;
20561
20562 if (*varname == NUL)
20563 {
20564 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020565 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020566 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020567 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 case 'g': return &globvars_var;
20569 case 'v': return &vimvars_var;
20570 case 'b': return &curbuf->b_bufvar;
20571 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020572#ifdef FEAT_WINDOWS
20573 case 't': return &curtab->tp_winvar;
20574#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020575 case 'l': return current_funccal == NULL
20576 ? NULL : &current_funccal->l_vars_var;
20577 case 'a': return current_funccal == NULL
20578 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020579 }
20580 return NULL;
20581 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020582
20583 hi = hash_find(ht, varname);
20584 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020585 {
20586 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020587 * worked find the variable again. Don't auto-load a script if it was
20588 * loaded already, otherwise it would be loaded every time when
20589 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020590 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020591 {
20592 /* Note: script_autoload() may make "hi" invalid. It must either
20593 * be obtained again or not used. */
20594 if (!script_autoload(varname, FALSE) || aborting())
20595 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020596 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020597 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020598 if (HASHITEM_EMPTY(hi))
20599 return NULL;
20600 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020601 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020602}
20603
20604/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020605 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020606 * Set "varname" to the start of name without ':'.
20607 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020608 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020609find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 char_u *name;
20611 char_u **varname;
20612{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020613 hashitem_T *hi;
20614
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 if (name[1] != ':')
20616 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020617 /* The name must not start with a colon or #. */
20618 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 return NULL;
20620 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020621
20622 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020623 hi = hash_find(&compat_hashtab, name);
20624 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020625 return &compat_hashtab;
20626
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020628 return &globvarht; /* global variable */
20629 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 }
20631 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020632 if (*name == 'g') /* global variable */
20633 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020634 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20635 */
20636 if (vim_strchr(name + 2, ':') != NULL
20637 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020638 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020640 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020642 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020643#ifdef FEAT_WINDOWS
20644 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020645 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020647 if (*name == 'v') /* v: variable */
20648 return &vimvarht;
20649 if (*name == 'a' && current_funccal != NULL) /* function argument */
20650 return &current_funccal->l_avars.dv_hashtab;
20651 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20652 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 if (*name == 's' /* script variable */
20654 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20655 return &SCRIPT_VARS(current_SID);
20656 return NULL;
20657}
20658
20659/*
20660 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020661 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 * Returns NULL when it doesn't exist.
20663 */
20664 char_u *
20665get_var_value(name)
20666 char_u *name;
20667{
Bram Moolenaar33570922005-01-25 22:26:29 +000020668 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020670 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 if (v == NULL)
20672 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020673 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674}
20675
20676/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020677 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020678 * sourcing this script and when executing functions defined in the script.
20679 */
20680 void
20681new_script_vars(id)
20682 scid_T id;
20683{
Bram Moolenaara7043832005-01-21 11:56:39 +000020684 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020685 hashtab_T *ht;
20686 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020687
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20689 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020690 /* Re-allocating ga_data means that an ht_array pointing to
20691 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020693 for (i = 1; i <= ga_scripts.ga_len; ++i)
20694 {
20695 ht = &SCRIPT_VARS(i);
20696 if (ht->ht_mask == HT_INIT_SIZE - 1)
20697 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020698 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020699 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020700 }
20701
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 while (ga_scripts.ga_len < id)
20703 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020704 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020705 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020706 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 }
20709 }
20710}
20711
20712/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020713 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20714 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 */
20716 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020717init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 dict_T *dict;
20719 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020720 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721{
Bram Moolenaar33570922005-01-25 22:26:29 +000020722 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020723 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020724 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020725 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020726 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020727 dict_var->di_tv.vval.v_dict = dict;
20728 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020729 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020730 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20731 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732}
20733
20734/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020735 * Unreference a dictionary initialized by init_var_dict().
20736 */
20737 void
20738unref_var_dict(dict)
20739 dict_T *dict;
20740{
20741 /* Now the dict needs to be freed if no one else is using it, go back to
20742 * normal reference counting. */
20743 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20744 dict_unref(dict);
20745}
20746
20747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020749 * Frees all allocated variables and the value they contain.
20750 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 */
20752 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020753vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020754 hashtab_T *ht;
20755{
20756 vars_clear_ext(ht, TRUE);
20757}
20758
20759/*
20760 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20761 */
20762 static void
20763vars_clear_ext(ht, free_val)
20764 hashtab_T *ht;
20765 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766{
Bram Moolenaara7043832005-01-21 11:56:39 +000020767 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020768 hashitem_T *hi;
20769 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770
Bram Moolenaar33570922005-01-25 22:26:29 +000020771 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020772 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020773 for (hi = ht->ht_array; todo > 0; ++hi)
20774 {
20775 if (!HASHITEM_EMPTY(hi))
20776 {
20777 --todo;
20778
Bram Moolenaar33570922005-01-25 22:26:29 +000020779 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020780 * ht_array might change then. hash_clear() takes care of it
20781 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020782 v = HI2DI(hi);
20783 if (free_val)
20784 clear_tv(&v->di_tv);
20785 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20786 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020787 }
20788 }
20789 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020790 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791}
20792
Bram Moolenaara7043832005-01-21 11:56:39 +000020793/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 * Delete a variable from hashtab "ht" at item "hi".
20795 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020798delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 hashtab_T *ht;
20800 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801{
Bram Moolenaar33570922005-01-25 22:26:29 +000020802 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020803
20804 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020805 clear_tv(&di->di_tv);
20806 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807}
20808
20809/*
20810 * List the value of one internal variable.
20811 */
20812 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020813list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020814 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020816 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020818 char_u *tofree;
20819 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020820 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020821
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020822 current_copyID += COPYID_INC;
20823 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020824 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020825 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020826 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020830list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 char_u *prefix;
20832 char_u *name;
20833 int type;
20834 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020835 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836{
Bram Moolenaar31859182007-08-14 20:41:13 +000020837 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20838 msg_start();
20839 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 if (name != NULL) /* "a:" vars don't have a name stored */
20841 msg_puts(name);
20842 msg_putchar(' ');
20843 msg_advance(22);
20844 if (type == VAR_NUMBER)
20845 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020846 else if (type == VAR_FUNC)
20847 msg_putchar('*');
20848 else if (type == VAR_LIST)
20849 {
20850 msg_putchar('[');
20851 if (*string == '[')
20852 ++string;
20853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020854 else if (type == VAR_DICT)
20855 {
20856 msg_putchar('{');
20857 if (*string == '{')
20858 ++string;
20859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 else
20861 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020862
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020864
20865 if (type == VAR_FUNC)
20866 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020867 if (*first)
20868 {
20869 msg_clr_eos();
20870 *first = FALSE;
20871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872}
20873
20874/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020875 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 * If the variable already exists, the value is updated.
20877 * Otherwise the variable is created.
20878 */
20879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020880set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020882 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884{
Bram Moolenaar33570922005-01-25 22:26:29 +000020885 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020889 ht = find_var_ht(name, &varname);
20890 if (ht == NULL || *varname == NUL)
20891 {
20892 EMSG2(_(e_illvar), name);
20893 return;
20894 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020895 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020896
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020897 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20898 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020899
Bram Moolenaar33570922005-01-25 22:26:29 +000020900 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020902 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020903 if (var_check_ro(v->di_flags, name)
20904 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020905 return;
20906 if (v->di_tv.v_type != tv->v_type
20907 && !((v->di_tv.v_type == VAR_STRING
20908 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020909 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020910 || tv->v_type == VAR_NUMBER))
20911#ifdef FEAT_FLOAT
20912 && !((v->di_tv.v_type == VAR_NUMBER
20913 || v->di_tv.v_type == VAR_FLOAT)
20914 && (tv->v_type == VAR_NUMBER
20915 || tv->v_type == VAR_FLOAT))
20916#endif
20917 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020918 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020919 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020920 return;
20921 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020922
20923 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020924 * Handle setting internal v: variables separately: we don't change
20925 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 */
20927 if (ht == &vimvarht)
20928 {
20929 if (v->di_tv.v_type == VAR_STRING)
20930 {
20931 vim_free(v->di_tv.vval.v_string);
20932 if (copy || tv->v_type != VAR_STRING)
20933 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20934 else
20935 {
20936 /* Take over the string to avoid an extra alloc/free. */
20937 v->di_tv.vval.v_string = tv->vval.v_string;
20938 tv->vval.v_string = NULL;
20939 }
20940 }
20941 else if (v->di_tv.v_type != VAR_NUMBER)
20942 EMSG2(_(e_intern2), "set_var()");
20943 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020945 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020946 if (STRCMP(varname, "searchforward") == 0)
20947 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020948#ifdef FEAT_SEARCH_EXTRA
20949 else if (STRCMP(varname, "hlsearch") == 0)
20950 {
20951 no_hlsearch = !v->di_tv.vval.v_number;
20952 redraw_all_later(SOME_VALID);
20953 }
20954#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020955 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 return;
20957 }
20958
20959 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 }
20961 else /* add a new variable */
20962 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020963 /* Can't add "v:" variable. */
20964 if (ht == &vimvarht)
20965 {
20966 EMSG2(_(e_illvar), name);
20967 return;
20968 }
20969
Bram Moolenaar92124a32005-06-17 22:03:40 +000020970 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020971 if (!valid_varname(varname))
20972 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020973
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020974 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20975 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020976 if (v == NULL)
20977 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020981 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 return;
20983 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020984 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020986
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020988 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020989 else
20990 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020991 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020992 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995}
20996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020997/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020998 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 * Also give an error message.
21000 */
21001 static int
21002var_check_ro(flags, name)
21003 int flags;
21004 char_u *name;
21005{
21006 if (flags & DI_FLAGS_RO)
21007 {
21008 EMSG2(_(e_readonlyvar), name);
21009 return TRUE;
21010 }
21011 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21012 {
21013 EMSG2(_(e_readonlysbx), name);
21014 return TRUE;
21015 }
21016 return FALSE;
21017}
21018
21019/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021020 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21021 * Also give an error message.
21022 */
21023 static int
21024var_check_fixed(flags, name)
21025 int flags;
21026 char_u *name;
21027{
21028 if (flags & DI_FLAGS_FIX)
21029 {
21030 EMSG2(_("E795: Cannot delete variable %s"), name);
21031 return TRUE;
21032 }
21033 return FALSE;
21034}
21035
21036/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021037 * Check if a funcref is assigned to a valid variable name.
21038 * Return TRUE and give an error if not.
21039 */
21040 static int
21041var_check_func_name(name, new_var)
21042 char_u *name; /* points to start of variable name */
21043 int new_var; /* TRUE when creating the variable */
21044{
21045 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
21046 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21047 ? name[2] : name[0]))
21048 {
21049 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21050 name);
21051 return TRUE;
21052 }
21053 /* Don't allow hiding a function. When "v" is not NULL we might be
21054 * assigning another function to the same var, the type is checked
21055 * below. */
21056 if (new_var && function_exists(name))
21057 {
21058 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21059 name);
21060 return TRUE;
21061 }
21062 return FALSE;
21063}
21064
21065/*
21066 * Check if a variable name is valid.
21067 * Return FALSE and give an error if not.
21068 */
21069 static int
21070valid_varname(varname)
21071 char_u *varname;
21072{
21073 char_u *p;
21074
21075 for (p = varname; *p != NUL; ++p)
21076 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21077 && *p != AUTOLOAD_CHAR)
21078 {
21079 EMSG2(_(e_illvar), varname);
21080 return FALSE;
21081 }
21082 return TRUE;
21083}
21084
21085/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021086 * Return TRUE if typeval "tv" is set to be locked (immutable).
21087 * Also give an error message, using "name".
21088 */
21089 static int
21090tv_check_lock(lock, name)
21091 int lock;
21092 char_u *name;
21093{
21094 if (lock & VAR_LOCKED)
21095 {
21096 EMSG2(_("E741: Value is locked: %s"),
21097 name == NULL ? (char_u *)_("Unknown") : name);
21098 return TRUE;
21099 }
21100 if (lock & VAR_FIXED)
21101 {
21102 EMSG2(_("E742: Cannot change value of %s"),
21103 name == NULL ? (char_u *)_("Unknown") : name);
21104 return TRUE;
21105 }
21106 return FALSE;
21107}
21108
21109/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021110 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021113 * It is OK for "from" and "to" to point to the same item. This is used to
21114 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021115 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021116 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021117copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021118 typval_T *from;
21119 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021121 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021122 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021123 switch (from->v_type)
21124 {
21125 case VAR_NUMBER:
21126 to->vval.v_number = from->vval.v_number;
21127 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021128#ifdef FEAT_FLOAT
21129 case VAR_FLOAT:
21130 to->vval.v_float = from->vval.v_float;
21131 break;
21132#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021133 case VAR_STRING:
21134 case VAR_FUNC:
21135 if (from->vval.v_string == NULL)
21136 to->vval.v_string = NULL;
21137 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021138 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021139 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021140 if (from->v_type == VAR_FUNC)
21141 func_ref(to->vval.v_string);
21142 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021143 break;
21144 case VAR_LIST:
21145 if (from->vval.v_list == NULL)
21146 to->vval.v_list = NULL;
21147 else
21148 {
21149 to->vval.v_list = from->vval.v_list;
21150 ++to->vval.v_list->lv_refcount;
21151 }
21152 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021153 case VAR_DICT:
21154 if (from->vval.v_dict == NULL)
21155 to->vval.v_dict = NULL;
21156 else
21157 {
21158 to->vval.v_dict = from->vval.v_dict;
21159 ++to->vval.v_dict->dv_refcount;
21160 }
21161 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021162 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021164 break;
21165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166}
21167
21168/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021169 * Make a copy of an item.
21170 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021171 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21172 * reference to an already copied list/dict can be used.
21173 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021174 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021175 static int
21176item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021177 typval_T *from;
21178 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021179 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021180 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021181{
21182 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021183 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021184
Bram Moolenaar33570922005-01-25 22:26:29 +000021185 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021186 {
21187 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021188 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021189 }
21190 ++recurse;
21191
21192 switch (from->v_type)
21193 {
21194 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021195#ifdef FEAT_FLOAT
21196 case VAR_FLOAT:
21197#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021198 case VAR_STRING:
21199 case VAR_FUNC:
21200 copy_tv(from, to);
21201 break;
21202 case VAR_LIST:
21203 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021204 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021205 if (from->vval.v_list == NULL)
21206 to->vval.v_list = NULL;
21207 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21208 {
21209 /* use the copy made earlier */
21210 to->vval.v_list = from->vval.v_list->lv_copylist;
21211 ++to->vval.v_list->lv_refcount;
21212 }
21213 else
21214 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21215 if (to->vval.v_list == NULL)
21216 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217 break;
21218 case VAR_DICT:
21219 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021220 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021221 if (from->vval.v_dict == NULL)
21222 to->vval.v_dict = NULL;
21223 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21224 {
21225 /* use the copy made earlier */
21226 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21227 ++to->vval.v_dict->dv_refcount;
21228 }
21229 else
21230 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21231 if (to->vval.v_dict == NULL)
21232 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021233 break;
21234 default:
21235 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021236 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 }
21238 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021239 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021240}
21241
21242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 * ":echo expr1 ..." print each argument separated with a space, add a
21244 * newline at the end.
21245 * ":echon expr1 ..." print each argument plain.
21246 */
21247 void
21248ex_echo(eap)
21249 exarg_T *eap;
21250{
21251 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021252 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021253 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 char_u *p;
21255 int needclr = TRUE;
21256 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021257 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258
21259 if (eap->skip)
21260 ++emsg_skip;
21261 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21262 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021263 /* If eval1() causes an error message the text from the command may
21264 * still need to be cleared. E.g., "echo 22,44". */
21265 need_clr_eos = needclr;
21266
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 {
21270 /*
21271 * Report the invalid expression unless the expression evaluation
21272 * has been cancelled due to an aborting error, an interrupt, or an
21273 * exception.
21274 */
21275 if (!aborting())
21276 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021277 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 break;
21279 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021280 need_clr_eos = FALSE;
21281
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 if (!eap->skip)
21283 {
21284 if (atstart)
21285 {
21286 atstart = FALSE;
21287 /* Call msg_start() after eval1(), evaluating the expression
21288 * may cause a message to appear. */
21289 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021290 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021291 /* Mark the saved text as finishing the line, so that what
21292 * follows is displayed on a new line when scrolling back
21293 * at the more prompt. */
21294 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 }
21298 else if (eap->cmdidx == CMD_echo)
21299 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021300 current_copyID += COPYID_INC;
21301 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021302 if (p != NULL)
21303 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021305 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021307 if (*p != TAB && needclr)
21308 {
21309 /* remove any text still there from the command */
21310 msg_clr_eos();
21311 needclr = FALSE;
21312 }
21313 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 }
21315 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021316 {
21317#ifdef FEAT_MBYTE
21318 if (has_mbyte)
21319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021320 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021321
21322 (void)msg_outtrans_len_attr(p, i, echo_attr);
21323 p += i - 1;
21324 }
21325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021327 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021330 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021332 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 arg = skipwhite(arg);
21334 }
21335 eap->nextcmd = check_nextcmd(arg);
21336
21337 if (eap->skip)
21338 --emsg_skip;
21339 else
21340 {
21341 /* remove text that may still be there from the command */
21342 if (needclr)
21343 msg_clr_eos();
21344 if (eap->cmdidx == CMD_echo)
21345 msg_end();
21346 }
21347}
21348
21349/*
21350 * ":echohl {name}".
21351 */
21352 void
21353ex_echohl(eap)
21354 exarg_T *eap;
21355{
21356 int id;
21357
21358 id = syn_name2id(eap->arg);
21359 if (id == 0)
21360 echo_attr = 0;
21361 else
21362 echo_attr = syn_id2attr(id);
21363}
21364
21365/*
21366 * ":execute expr1 ..." execute the result of an expression.
21367 * ":echomsg expr1 ..." Print a message
21368 * ":echoerr expr1 ..." Print an error
21369 * Each gets spaces around each argument and a newline at the end for
21370 * echo commands
21371 */
21372 void
21373ex_execute(eap)
21374 exarg_T *eap;
21375{
21376 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 int ret = OK;
21379 char_u *p;
21380 garray_T ga;
21381 int len;
21382 int save_did_emsg;
21383
21384 ga_init2(&ga, 1, 80);
21385
21386 if (eap->skip)
21387 ++emsg_skip;
21388 while (*arg != NUL && *arg != '|' && *arg != '\n')
21389 {
21390 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021391 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 {
21393 /*
21394 * Report the invalid expression unless the expression evaluation
21395 * has been cancelled due to an aborting error, an interrupt, or an
21396 * exception.
21397 */
21398 if (!aborting())
21399 EMSG2(_(e_invexpr2), p);
21400 ret = FAIL;
21401 break;
21402 }
21403
21404 if (!eap->skip)
21405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 len = (int)STRLEN(p);
21408 if (ga_grow(&ga, len + 2) == FAIL)
21409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 ret = FAIL;
21412 break;
21413 }
21414 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 ga.ga_len += len;
21418 }
21419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021420 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 arg = skipwhite(arg);
21422 }
21423
21424 if (ret != FAIL && ga.ga_data != NULL)
21425 {
21426 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021429 out_flush();
21430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 else if (eap->cmdidx == CMD_echoerr)
21432 {
21433 /* We don't want to abort following commands, restore did_emsg. */
21434 save_did_emsg = did_emsg;
21435 EMSG((char_u *)ga.ga_data);
21436 if (!force_abort)
21437 did_emsg = save_did_emsg;
21438 }
21439 else if (eap->cmdidx == CMD_execute)
21440 do_cmdline((char_u *)ga.ga_data,
21441 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21442 }
21443
21444 ga_clear(&ga);
21445
21446 if (eap->skip)
21447 --emsg_skip;
21448
21449 eap->nextcmd = check_nextcmd(arg);
21450}
21451
21452/*
21453 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21454 * "arg" points to the "&" or '+' when called, to "option" when returning.
21455 * Returns NULL when no option name found. Otherwise pointer to the char
21456 * after the option name.
21457 */
21458 static char_u *
21459find_option_end(arg, opt_flags)
21460 char_u **arg;
21461 int *opt_flags;
21462{
21463 char_u *p = *arg;
21464
21465 ++p;
21466 if (*p == 'g' && p[1] == ':')
21467 {
21468 *opt_flags = OPT_GLOBAL;
21469 p += 2;
21470 }
21471 else if (*p == 'l' && p[1] == ':')
21472 {
21473 *opt_flags = OPT_LOCAL;
21474 p += 2;
21475 }
21476 else
21477 *opt_flags = 0;
21478
21479 if (!ASCII_ISALPHA(*p))
21480 return NULL;
21481 *arg = p;
21482
21483 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21484 p += 4; /* termcap option */
21485 else
21486 while (ASCII_ISALPHA(*p))
21487 ++p;
21488 return p;
21489}
21490
21491/*
21492 * ":function"
21493 */
21494 void
21495ex_function(eap)
21496 exarg_T *eap;
21497{
21498 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021499 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 int j;
21501 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021503 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 char_u *name = NULL;
21505 char_u *p;
21506 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021507 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 garray_T newargs;
21509 garray_T newlines;
21510 int varargs = FALSE;
21511 int mustend = FALSE;
21512 int flags = 0;
21513 ufunc_T *fp;
21514 int indent;
21515 int nesting;
21516 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021517 dictitem_T *v;
21518 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519 static int func_nr = 0; /* number for nameless function */
21520 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021521 hashtab_T *ht;
21522 int todo;
21523 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021524 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525
21526 /*
21527 * ":function" without argument: list functions.
21528 */
21529 if (ends_excmd(*eap->arg))
21530 {
21531 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021532 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021533 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021534 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021535 {
21536 if (!HASHITEM_EMPTY(hi))
21537 {
21538 --todo;
21539 fp = HI2UF(hi);
21540 if (!isdigit(*fp->uf_name))
21541 list_func_head(fp, FALSE);
21542 }
21543 }
21544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 eap->nextcmd = check_nextcmd(eap->arg);
21546 return;
21547 }
21548
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021549 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021550 * ":function /pat": list functions matching pattern.
21551 */
21552 if (*eap->arg == '/')
21553 {
21554 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21555 if (!eap->skip)
21556 {
21557 regmatch_T regmatch;
21558
21559 c = *p;
21560 *p = NUL;
21561 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21562 *p = c;
21563 if (regmatch.regprog != NULL)
21564 {
21565 regmatch.rm_ic = p_ic;
21566
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021567 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021568 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21569 {
21570 if (!HASHITEM_EMPTY(hi))
21571 {
21572 --todo;
21573 fp = HI2UF(hi);
21574 if (!isdigit(*fp->uf_name)
21575 && vim_regexec(&regmatch, fp->uf_name, 0))
21576 list_func_head(fp, FALSE);
21577 }
21578 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021579 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021580 }
21581 }
21582 if (*p == '/')
21583 ++p;
21584 eap->nextcmd = check_nextcmd(p);
21585 return;
21586 }
21587
21588 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021589 * Get the function name. There are these situations:
21590 * func normal function name
21591 * "name" == func, "fudi.fd_dict" == NULL
21592 * dict.func new dictionary entry
21593 * "name" == NULL, "fudi.fd_dict" set,
21594 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21595 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021596 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21598 * dict.func existing dict entry that's not a Funcref
21599 * "name" == NULL, "fudi.fd_dict" set,
21600 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021601 * s:func script-local function name
21602 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 name = trans_function_name(&p, eap->skip, 0, &fudi);
21606 paren = (vim_strchr(p, '(') != NULL);
21607 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 {
21609 /*
21610 * Return on an invalid expression in braces, unless the expression
21611 * evaluation has been cancelled due to an aborting error, an
21612 * interrupt, or an exception.
21613 */
21614 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021616 if (!eap->skip && fudi.fd_newkey != NULL)
21617 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021618 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 else
21622 eap->skip = TRUE;
21623 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021624
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 /* An error in a function call during evaluation of an expression in magic
21626 * braces should not cause the function not to be defined. */
21627 saved_did_emsg = did_emsg;
21628 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
21630 /*
21631 * ":function func" with only function name: list function.
21632 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 {
21635 if (!ends_excmd(*skipwhite(p)))
21636 {
21637 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021638 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 }
21640 eap->nextcmd = check_nextcmd(p);
21641 if (eap->nextcmd != NULL)
21642 *p = NUL;
21643 if (!eap->skip && !got_int)
21644 {
21645 fp = find_func(name);
21646 if (fp != NULL)
21647 {
21648 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021649 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021651 if (FUNCLINE(fp, j) == NULL)
21652 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 msg_putchar('\n');
21654 msg_outnum((long)(j + 1));
21655 if (j < 9)
21656 msg_putchar(' ');
21657 if (j < 99)
21658 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021659 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 out_flush(); /* show a line at a time */
21661 ui_breakcheck();
21662 }
21663 if (!got_int)
21664 {
21665 msg_putchar('\n');
21666 msg_puts((char_u *)" endfunction");
21667 }
21668 }
21669 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021670 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 }
21674
21675 /*
21676 * ":function name(arg1, arg2)" Define function.
21677 */
21678 p = skipwhite(p);
21679 if (*p != '(')
21680 {
21681 if (!eap->skip)
21682 {
21683 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021684 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 }
21686 /* attempt to continue by skipping some text */
21687 if (vim_strchr(p, '(') != NULL)
21688 p = vim_strchr(p, '(');
21689 }
21690 p = skipwhite(p + 1);
21691
21692 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21693 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21694
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021695 if (!eap->skip)
21696 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021697 /* Check the name of the function. Unless it's a dictionary function
21698 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021699 if (name != NULL)
21700 arg = name;
21701 else
21702 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021703 if (arg != NULL && (fudi.fd_di == NULL
21704 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021705 {
21706 if (*arg == K_SPECIAL)
21707 j = 3;
21708 else
21709 j = 0;
21710 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21711 : eval_isnamec(arg[j])))
21712 ++j;
21713 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021714 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021715 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021716 /* Disallow using the g: dict. */
21717 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21718 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021719 }
21720
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 /*
21722 * Isolate the arguments: "arg1, arg2, ...)"
21723 */
21724 while (*p != ')')
21725 {
21726 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21727 {
21728 varargs = TRUE;
21729 p += 3;
21730 mustend = TRUE;
21731 }
21732 else
21733 {
21734 arg = p;
21735 while (ASCII_ISALNUM(*p) || *p == '_')
21736 ++p;
21737 if (arg == p || isdigit(*arg)
21738 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21739 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21740 {
21741 if (!eap->skip)
21742 EMSG2(_("E125: Illegal argument: %s"), arg);
21743 break;
21744 }
21745 if (ga_grow(&newargs, 1) == FAIL)
21746 goto erret;
21747 c = *p;
21748 *p = NUL;
21749 arg = vim_strsave(arg);
21750 if (arg == NULL)
21751 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021752
21753 /* Check for duplicate argument name. */
21754 for (i = 0; i < newargs.ga_len; ++i)
21755 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21756 {
21757 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021758 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021759 goto erret;
21760 }
21761
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21763 *p = c;
21764 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 if (*p == ',')
21766 ++p;
21767 else
21768 mustend = TRUE;
21769 }
21770 p = skipwhite(p);
21771 if (mustend && *p != ')')
21772 {
21773 if (!eap->skip)
21774 EMSG2(_(e_invarg2), eap->arg);
21775 break;
21776 }
21777 }
21778 ++p; /* skip the ')' */
21779
Bram Moolenaare9a41262005-01-15 22:18:47 +000021780 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 for (;;)
21782 {
21783 p = skipwhite(p);
21784 if (STRNCMP(p, "range", 5) == 0)
21785 {
21786 flags |= FC_RANGE;
21787 p += 5;
21788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021789 else if (STRNCMP(p, "dict", 4) == 0)
21790 {
21791 flags |= FC_DICT;
21792 p += 4;
21793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 else if (STRNCMP(p, "abort", 5) == 0)
21795 {
21796 flags |= FC_ABORT;
21797 p += 5;
21798 }
21799 else
21800 break;
21801 }
21802
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021803 /* When there is a line break use what follows for the function body.
21804 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21805 if (*p == '\n')
21806 line_arg = p + 1;
21807 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 EMSG(_(e_trailing));
21809
21810 /*
21811 * Read the body of the function, until ":endfunction" is found.
21812 */
21813 if (KeyTyped)
21814 {
21815 /* Check if the function already exists, don't let the user type the
21816 * whole function before telling him it doesn't work! For a script we
21817 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021818 if (!eap->skip && !eap->forceit)
21819 {
21820 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21821 EMSG(_(e_funcdict));
21822 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021823 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021826 if (!eap->skip && did_emsg)
21827 goto erret;
21828
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 msg_putchar('\n'); /* don't overwrite the function name */
21830 cmdline_row = msg_row;
21831 }
21832
21833 indent = 2;
21834 nesting = 0;
21835 for (;;)
21836 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021837 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021838 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021839 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021840 saved_wait_return = FALSE;
21841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021843 sourcing_lnum_off = sourcing_lnum;
21844
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021845 if (line_arg != NULL)
21846 {
21847 /* Use eap->arg, split up in parts by line breaks. */
21848 theline = line_arg;
21849 p = vim_strchr(theline, '\n');
21850 if (p == NULL)
21851 line_arg += STRLEN(line_arg);
21852 else
21853 {
21854 *p = NUL;
21855 line_arg = p + 1;
21856 }
21857 }
21858 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 theline = getcmdline(':', 0L, indent);
21860 else
21861 theline = eap->getline(':', eap->cookie, indent);
21862 if (KeyTyped)
21863 lines_left = Rows - 1;
21864 if (theline == NULL)
21865 {
21866 EMSG(_("E126: Missing :endfunction"));
21867 goto erret;
21868 }
21869
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021870 /* Detect line continuation: sourcing_lnum increased more than one. */
21871 if (sourcing_lnum > sourcing_lnum_off + 1)
21872 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21873 else
21874 sourcing_lnum_off = 0;
21875
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 if (skip_until != NULL)
21877 {
21878 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21879 * don't check for ":endfunc". */
21880 if (STRCMP(theline, skip_until) == 0)
21881 {
21882 vim_free(skip_until);
21883 skip_until = NULL;
21884 }
21885 }
21886 else
21887 {
21888 /* skip ':' and blanks*/
21889 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21890 ;
21891
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021892 /* Check for "endfunction". */
21893 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021895 if (line_arg == NULL)
21896 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 break;
21898 }
21899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021900 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 * at "end". */
21902 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21903 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021904 else if (STRNCMP(p, "if", 2) == 0
21905 || STRNCMP(p, "wh", 2) == 0
21906 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 || STRNCMP(p, "try", 3) == 0)
21908 indent += 2;
21909
21910 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021911 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021913 if (*p == '!')
21914 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 p += eval_fname_script(p);
21916 if (ASCII_ISALPHA(*p))
21917 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021918 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 if (*skipwhite(p) == '(')
21920 {
21921 ++nesting;
21922 indent += 2;
21923 }
21924 }
21925 }
21926
21927 /* Check for ":append" or ":insert". */
21928 p = skip_range(p, NULL);
21929 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21930 || (p[0] == 'i'
21931 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21932 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21933 skip_until = vim_strsave((char_u *)".");
21934
21935 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21936 arg = skipwhite(skiptowhite(p));
21937 if (arg[0] == '<' && arg[1] =='<'
21938 && ((p[0] == 'p' && p[1] == 'y'
21939 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21940 || (p[0] == 'p' && p[1] == 'e'
21941 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21942 || (p[0] == 't' && p[1] == 'c'
21943 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021944 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21945 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21947 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021948 || (p[0] == 'm' && p[1] == 'z'
21949 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 ))
21951 {
21952 /* ":python <<" continues until a dot, like ":append" */
21953 p = skipwhite(arg + 2);
21954 if (*p == NUL)
21955 skip_until = vim_strsave((char_u *)".");
21956 else
21957 skip_until = vim_strsave(p);
21958 }
21959 }
21960
21961 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021962 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021963 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021964 if (line_arg == NULL)
21965 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021967 }
21968
21969 /* Copy the line to newly allocated memory. get_one_sourceline()
21970 * allocates 250 bytes per line, this saves 80% on average. The cost
21971 * is an extra alloc/free. */
21972 p = vim_strsave(theline);
21973 if (p != NULL)
21974 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021975 if (line_arg == NULL)
21976 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021977 theline = p;
21978 }
21979
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021980 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21981
21982 /* Add NULL lines for continuation lines, so that the line count is
21983 * equal to the index in the growarray. */
21984 while (sourcing_lnum_off-- > 0)
21985 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021986
21987 /* Check for end of eap->arg. */
21988 if (line_arg != NULL && *line_arg == NUL)
21989 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 }
21991
21992 /* Don't define the function when skipping commands or when an error was
21993 * detected. */
21994 if (eap->skip || did_emsg)
21995 goto erret;
21996
21997 /*
21998 * If there are no errors, add the function
21999 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022000 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022001 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022002 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022003 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022004 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022005 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022006 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022007 goto erret;
22008 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022010 fp = find_func(name);
22011 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022013 if (!eap->forceit)
22014 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022015 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022016 goto erret;
22017 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022018 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022019 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022020 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022021 name);
22022 goto erret;
22023 }
22024 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022025 ga_clear_strings(&(fp->uf_args));
22026 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022027 vim_free(name);
22028 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 }
22031 else
22032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022033 char numbuf[20];
22034
22035 fp = NULL;
22036 if (fudi.fd_newkey == NULL && !eap->forceit)
22037 {
22038 EMSG(_(e_funcdict));
22039 goto erret;
22040 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022041 if (fudi.fd_di == NULL)
22042 {
22043 /* Can't add a function to a locked dictionary */
22044 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22045 goto erret;
22046 }
22047 /* Can't change an existing function if it is locked */
22048 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22049 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022050
22051 /* Give the function a sequential number. Can only be used with a
22052 * Funcref! */
22053 vim_free(name);
22054 sprintf(numbuf, "%d", ++func_nr);
22055 name = vim_strsave((char_u *)numbuf);
22056 if (name == NULL)
22057 goto erret;
22058 }
22059
22060 if (fp == NULL)
22061 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022062 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022063 {
22064 int slen, plen;
22065 char_u *scriptname;
22066
22067 /* Check that the autoload name matches the script name. */
22068 j = FAIL;
22069 if (sourcing_name != NULL)
22070 {
22071 scriptname = autoload_name(name);
22072 if (scriptname != NULL)
22073 {
22074 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022075 plen = (int)STRLEN(p);
22076 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022077 if (slen > plen && fnamecmp(p,
22078 sourcing_name + slen - plen) == 0)
22079 j = OK;
22080 vim_free(scriptname);
22081 }
22082 }
22083 if (j == FAIL)
22084 {
22085 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22086 goto erret;
22087 }
22088 }
22089
22090 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 if (fp == NULL)
22092 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093
22094 if (fudi.fd_dict != NULL)
22095 {
22096 if (fudi.fd_di == NULL)
22097 {
22098 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022099 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022100 if (fudi.fd_di == NULL)
22101 {
22102 vim_free(fp);
22103 goto erret;
22104 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022105 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22106 {
22107 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022108 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022109 goto erret;
22110 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022111 }
22112 else
22113 /* overwrite existing dict entry */
22114 clear_tv(&fudi.fd_di->di_tv);
22115 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022116 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022117 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022118 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022119
22120 /* behave like "dict" was used */
22121 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022122 }
22123
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022125 STRCPY(fp->uf_name, name);
22126 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022128 fp->uf_args = newargs;
22129 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022130#ifdef FEAT_PROFILE
22131 fp->uf_tml_count = NULL;
22132 fp->uf_tml_total = NULL;
22133 fp->uf_tml_self = NULL;
22134 fp->uf_profiling = FALSE;
22135 if (prof_def_func())
22136 func_do_profile(fp);
22137#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022138 fp->uf_varargs = varargs;
22139 fp->uf_flags = flags;
22140 fp->uf_calls = 0;
22141 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022142 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143
22144erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 ga_clear_strings(&newargs);
22146 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022147ret_free:
22148 vim_free(skip_until);
22149 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022152 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153}
22154
22155/*
22156 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022157 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022159 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022160 * TFN_INT: internal function name OK
22161 * TFN_QUIET: be quiet
22162 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 * Advances "pp" to just after the function name (if no error).
22164 */
22165 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022166trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 char_u **pp;
22168 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022169 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022172 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 char_u *start;
22174 char_u *end;
22175 int lead;
22176 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022179
22180 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022183
22184 /* Check for hard coded <SNR>: already translated function ID (from a user
22185 * command). */
22186 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22187 && (*pp)[2] == (int)KE_SNR)
22188 {
22189 *pp += 3;
22190 len = get_id_len(pp) + 3;
22191 return vim_strnsave(start, len);
22192 }
22193
22194 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22195 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022197 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022199
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022200 /* Note that TFN_ flags use the same values as GLV_ flags. */
22201 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022202 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022203 if (end == start)
22204 {
22205 if (!skip)
22206 EMSG(_("E129: Function name required"));
22207 goto theend;
22208 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022209 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022210 {
22211 /*
22212 * Report an invalid expression in braces, unless the expression
22213 * evaluation has been cancelled due to an aborting error, an
22214 * interrupt, or an exception.
22215 */
22216 if (!aborting())
22217 {
22218 if (end != NULL)
22219 EMSG2(_(e_invarg2), start);
22220 }
22221 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022222 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022223 goto theend;
22224 }
22225
22226 if (lv.ll_tv != NULL)
22227 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022228 if (fdp != NULL)
22229 {
22230 fdp->fd_dict = lv.ll_dict;
22231 fdp->fd_newkey = lv.ll_newkey;
22232 lv.ll_newkey = NULL;
22233 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022235 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22236 {
22237 name = vim_strsave(lv.ll_tv->vval.v_string);
22238 *pp = end;
22239 }
22240 else
22241 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022242 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22243 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022244 EMSG(_(e_funcref));
22245 else
22246 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022247 name = NULL;
22248 }
22249 goto theend;
22250 }
22251
22252 if (lv.ll_name == NULL)
22253 {
22254 /* Error found, but continue after the function name. */
22255 *pp = end;
22256 goto theend;
22257 }
22258
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022259 /* Check if the name is a Funcref. If so, use the value. */
22260 if (lv.ll_exp_name != NULL)
22261 {
22262 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022263 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022264 if (name == lv.ll_exp_name)
22265 name = NULL;
22266 }
22267 else
22268 {
22269 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022270 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022271 if (name == *pp)
22272 name = NULL;
22273 }
22274 if (name != NULL)
22275 {
22276 name = vim_strsave(name);
22277 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022278 if (STRNCMP(name, "<SNR>", 5) == 0)
22279 {
22280 /* Change "<SNR>" to the byte sequence. */
22281 name[0] = K_SPECIAL;
22282 name[1] = KS_EXTRA;
22283 name[2] = (int)KE_SNR;
22284 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22285 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022286 goto theend;
22287 }
22288
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022289 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022290 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022291 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022292 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22293 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22294 {
22295 /* When there was "s:" already or the name expanded to get a
22296 * leading "s:" then remove it. */
22297 lv.ll_name += 2;
22298 len -= 2;
22299 lead = 2;
22300 }
22301 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022302 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022303 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022304 /* skip over "s:" and "g:" */
22305 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022306 lv.ll_name += 2;
22307 len = (int)(end - lv.ll_name);
22308 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022309
22310 /*
22311 * Copy the function name to allocated memory.
22312 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22313 * Accept <SNR>123_name() outside a script.
22314 */
22315 if (skip)
22316 lead = 0; /* do nothing */
22317 else if (lead > 0)
22318 {
22319 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022320 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22321 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022322 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022323 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022324 if (current_SID <= 0)
22325 {
22326 EMSG(_(e_usingsid));
22327 goto theend;
22328 }
22329 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22330 lead += (int)STRLEN(sid_buf);
22331 }
22332 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022333 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022334 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022335 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022336 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022337 goto theend;
22338 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022339 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022340 {
22341 char_u *cp = vim_strchr(lv.ll_name, ':');
22342
22343 if (cp != NULL && cp < end)
22344 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022345 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022346 goto theend;
22347 }
22348 }
22349
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022350 name = alloc((unsigned)(len + lead + 1));
22351 if (name != NULL)
22352 {
22353 if (lead > 0)
22354 {
22355 name[0] = K_SPECIAL;
22356 name[1] = KS_EXTRA;
22357 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022358 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022359 STRCPY(name + 3, sid_buf);
22360 }
22361 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022362 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022363 }
22364 *pp = end;
22365
22366theend:
22367 clear_lval(&lv);
22368 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369}
22370
22371/*
22372 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22373 * Return 2 if "p" starts with "s:".
22374 * Return 0 otherwise.
22375 */
22376 static int
22377eval_fname_script(p)
22378 char_u *p;
22379{
22380 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22381 || STRNICMP(p + 1, "SNR>", 4) == 0))
22382 return 5;
22383 if (p[0] == 's' && p[1] == ':')
22384 return 2;
22385 return 0;
22386}
22387
22388/*
22389 * Return TRUE if "p" starts with "<SID>" or "s:".
22390 * Only works if eval_fname_script() returned non-zero for "p"!
22391 */
22392 static int
22393eval_fname_sid(p)
22394 char_u *p;
22395{
22396 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22397}
22398
22399/*
22400 * List the head of the function: "name(arg1, arg2)".
22401 */
22402 static void
22403list_func_head(fp, indent)
22404 ufunc_T *fp;
22405 int indent;
22406{
22407 int j;
22408
22409 msg_start();
22410 if (indent)
22411 MSG_PUTS(" ");
22412 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022413 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 {
22415 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022416 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
22418 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022419 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
22423 if (j)
22424 MSG_PUTS(", ");
22425 msg_puts(FUNCARG(fp, j));
22426 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022427 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 {
22429 if (j)
22430 MSG_PUTS(", ");
22431 MSG_PUTS("...");
22432 }
22433 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022434 if (fp->uf_flags & FC_ABORT)
22435 MSG_PUTS(" abort");
22436 if (fp->uf_flags & FC_RANGE)
22437 MSG_PUTS(" range");
22438 if (fp->uf_flags & FC_DICT)
22439 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022440 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022441 if (p_verbose > 0)
22442 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443}
22444
22445/*
22446 * Find a function by name, return pointer to it in ufuncs.
22447 * Return NULL for unknown function.
22448 */
22449 static ufunc_T *
22450find_func(name)
22451 char_u *name;
22452{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022453 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022455 hi = hash_find(&func_hashtab, name);
22456 if (!HASHITEM_EMPTY(hi))
22457 return HI2UF(hi);
22458 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459}
22460
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022461#if defined(EXITFREE) || defined(PROTO)
22462 void
22463free_all_functions()
22464{
22465 hashitem_T *hi;
22466
22467 /* Need to start all over every time, because func_free() may change the
22468 * hash table. */
22469 while (func_hashtab.ht_used > 0)
22470 for (hi = func_hashtab.ht_array; ; ++hi)
22471 if (!HASHITEM_EMPTY(hi))
22472 {
22473 func_free(HI2UF(hi));
22474 break;
22475 }
22476}
22477#endif
22478
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022479 int
22480translated_function_exists(name)
22481 char_u *name;
22482{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022483 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022484 return find_internal_func(name) >= 0;
22485 return find_func(name) != NULL;
22486}
22487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488/*
22489 * Return TRUE if a function "name" exists.
22490 */
22491 static int
22492function_exists(name)
22493 char_u *name;
22494{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022495 char_u *nm = name;
22496 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022497 int n = FALSE;
22498
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022499 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22500 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022501 nm = skipwhite(nm);
22502
22503 /* Only accept "funcname", "funcname ", "funcname (..." and
22504 * "funcname(...", not "funcname!...". */
22505 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022506 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022507 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 return n;
22509}
22510
Bram Moolenaara1544c02013-05-30 12:35:52 +020022511 char_u *
22512get_expanded_name(name, check)
22513 char_u *name;
22514 int check;
22515{
22516 char_u *nm = name;
22517 char_u *p;
22518
22519 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22520
22521 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022522 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022523 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022524
Bram Moolenaara1544c02013-05-30 12:35:52 +020022525 vim_free(p);
22526 return NULL;
22527}
22528
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022529/*
22530 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022531 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22532 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022533 */
22534 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022535builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022536 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022537 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022538{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022539 char_u *p;
22540
22541 if (!ASCII_ISLOWER(name[0]))
22542 return FALSE;
22543 p = vim_strchr(name, AUTOLOAD_CHAR);
22544 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022545}
22546
Bram Moolenaar05159a02005-02-26 23:04:13 +000022547#if defined(FEAT_PROFILE) || defined(PROTO)
22548/*
22549 * Start profiling function "fp".
22550 */
22551 static void
22552func_do_profile(fp)
22553 ufunc_T *fp;
22554{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022555 int len = fp->uf_lines.ga_len;
22556
22557 if (len == 0)
22558 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022559 fp->uf_tm_count = 0;
22560 profile_zero(&fp->uf_tm_self);
22561 profile_zero(&fp->uf_tm_total);
22562 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022563 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022564 if (fp->uf_tml_total == NULL)
22565 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022566 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022567 if (fp->uf_tml_self == NULL)
22568 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022569 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022570 fp->uf_tml_idx = -1;
22571 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22572 || fp->uf_tml_self == NULL)
22573 return; /* out of memory */
22574
22575 fp->uf_profiling = TRUE;
22576}
22577
22578/*
22579 * Dump the profiling results for all functions in file "fd".
22580 */
22581 void
22582func_dump_profile(fd)
22583 FILE *fd;
22584{
22585 hashitem_T *hi;
22586 int todo;
22587 ufunc_T *fp;
22588 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022589 ufunc_T **sorttab;
22590 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022592 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022593 if (todo == 0)
22594 return; /* nothing to dump */
22595
Bram Moolenaar73830342005-02-28 22:48:19 +000022596 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22597
Bram Moolenaar05159a02005-02-26 23:04:13 +000022598 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22599 {
22600 if (!HASHITEM_EMPTY(hi))
22601 {
22602 --todo;
22603 fp = HI2UF(hi);
22604 if (fp->uf_profiling)
22605 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022606 if (sorttab != NULL)
22607 sorttab[st_len++] = fp;
22608
Bram Moolenaar05159a02005-02-26 23:04:13 +000022609 if (fp->uf_name[0] == K_SPECIAL)
22610 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22611 else
22612 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22613 if (fp->uf_tm_count == 1)
22614 fprintf(fd, "Called 1 time\n");
22615 else
22616 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22617 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22618 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22619 fprintf(fd, "\n");
22620 fprintf(fd, "count total (s) self (s)\n");
22621
22622 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22623 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022624 if (FUNCLINE(fp, i) == NULL)
22625 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022626 prof_func_line(fd, fp->uf_tml_count[i],
22627 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022628 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22629 }
22630 fprintf(fd, "\n");
22631 }
22632 }
22633 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022634
22635 if (sorttab != NULL && st_len > 0)
22636 {
22637 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22638 prof_total_cmp);
22639 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22640 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22641 prof_self_cmp);
22642 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22643 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022644
22645 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022646}
Bram Moolenaar73830342005-02-28 22:48:19 +000022647
22648 static void
22649prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22650 FILE *fd;
22651 ufunc_T **sorttab;
22652 int st_len;
22653 char *title;
22654 int prefer_self; /* when equal print only self time */
22655{
22656 int i;
22657 ufunc_T *fp;
22658
22659 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22660 fprintf(fd, "count total (s) self (s) function\n");
22661 for (i = 0; i < 20 && i < st_len; ++i)
22662 {
22663 fp = sorttab[i];
22664 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22665 prefer_self);
22666 if (fp->uf_name[0] == K_SPECIAL)
22667 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22668 else
22669 fprintf(fd, " %s()\n", fp->uf_name);
22670 }
22671 fprintf(fd, "\n");
22672}
22673
22674/*
22675 * Print the count and times for one function or function line.
22676 */
22677 static void
22678prof_func_line(fd, count, total, self, prefer_self)
22679 FILE *fd;
22680 int count;
22681 proftime_T *total;
22682 proftime_T *self;
22683 int prefer_self; /* when equal print only self time */
22684{
22685 if (count > 0)
22686 {
22687 fprintf(fd, "%5d ", count);
22688 if (prefer_self && profile_equal(total, self))
22689 fprintf(fd, " ");
22690 else
22691 fprintf(fd, "%s ", profile_msg(total));
22692 if (!prefer_self && profile_equal(total, self))
22693 fprintf(fd, " ");
22694 else
22695 fprintf(fd, "%s ", profile_msg(self));
22696 }
22697 else
22698 fprintf(fd, " ");
22699}
22700
22701/*
22702 * Compare function for total time sorting.
22703 */
22704 static int
22705#ifdef __BORLANDC__
22706_RTLENTRYF
22707#endif
22708prof_total_cmp(s1, s2)
22709 const void *s1;
22710 const void *s2;
22711{
22712 ufunc_T *p1, *p2;
22713
22714 p1 = *(ufunc_T **)s1;
22715 p2 = *(ufunc_T **)s2;
22716 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22717}
22718
22719/*
22720 * Compare function for self time sorting.
22721 */
22722 static int
22723#ifdef __BORLANDC__
22724_RTLENTRYF
22725#endif
22726prof_self_cmp(s1, s2)
22727 const void *s1;
22728 const void *s2;
22729{
22730 ufunc_T *p1, *p2;
22731
22732 p1 = *(ufunc_T **)s1;
22733 p2 = *(ufunc_T **)s2;
22734 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22735}
22736
Bram Moolenaar05159a02005-02-26 23:04:13 +000022737#endif
22738
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022739/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022740 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022741 * Return TRUE if a package was loaded.
22742 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022743 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022744script_autoload(name, reload)
22745 char_u *name;
22746 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022747{
22748 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022749 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022750 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022751 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022752
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022753 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022754 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022755 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022756 return FALSE;
22757
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022758 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022759
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022760 /* Find the name in the list of previously loaded package names. Skip
22761 * "autoload/", it's always the same. */
22762 for (i = 0; i < ga_loaded.ga_len; ++i)
22763 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22764 break;
22765 if (!reload && i < ga_loaded.ga_len)
22766 ret = FALSE; /* was loaded already */
22767 else
22768 {
22769 /* Remember the name if it wasn't loaded already. */
22770 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22771 {
22772 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22773 tofree = NULL;
22774 }
22775
22776 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022777 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022778 ret = TRUE;
22779 }
22780
22781 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022782 return ret;
22783}
22784
22785/*
22786 * Return the autoload script name for a function or variable name.
22787 * Returns NULL when out of memory.
22788 */
22789 static char_u *
22790autoload_name(name)
22791 char_u *name;
22792{
22793 char_u *p;
22794 char_u *scriptname;
22795
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022796 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022797 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22798 if (scriptname == NULL)
22799 return FALSE;
22800 STRCPY(scriptname, "autoload/");
22801 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022802 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022803 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022804 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022805 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022806 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022807}
22808
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22810
22811/*
22812 * Function given to ExpandGeneric() to obtain the list of user defined
22813 * function names.
22814 */
22815 char_u *
22816get_user_func_name(xp, idx)
22817 expand_T *xp;
22818 int idx;
22819{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022820 static long_u done;
22821 static hashitem_T *hi;
22822 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823
22824 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022826 done = 0;
22827 hi = func_hashtab.ht_array;
22828 }
22829 if (done < func_hashtab.ht_used)
22830 {
22831 if (done++ > 0)
22832 ++hi;
22833 while (HASHITEM_EMPTY(hi))
22834 ++hi;
22835 fp = HI2UF(hi);
22836
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022837 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022838 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022839
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022840 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22841 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842
22843 cat_func_name(IObuff, fp);
22844 if (xp->xp_context != EXPAND_USER_FUNC)
22845 {
22846 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022847 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 STRCAT(IObuff, ")");
22849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 return IObuff;
22851 }
22852 return NULL;
22853}
22854
22855#endif /* FEAT_CMDL_COMPL */
22856
22857/*
22858 * Copy the function name of "fp" to buffer "buf".
22859 * "buf" must be able to hold the function name plus three bytes.
22860 * Takes care of script-local function names.
22861 */
22862 static void
22863cat_func_name(buf, fp)
22864 char_u *buf;
22865 ufunc_T *fp;
22866{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022867 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 {
22869 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022870 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
22872 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022873 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874}
22875
22876/*
22877 * ":delfunction {name}"
22878 */
22879 void
22880ex_delfunction(eap)
22881 exarg_T *eap;
22882{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022883 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 char_u *p;
22885 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887
22888 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 name = trans_function_name(&p, eap->skip, 0, &fudi);
22890 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 {
22893 if (fudi.fd_dict != NULL && !eap->skip)
22894 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 if (!ends_excmd(*skipwhite(p)))
22898 {
22899 vim_free(name);
22900 EMSG(_(e_trailing));
22901 return;
22902 }
22903 eap->nextcmd = check_nextcmd(p);
22904 if (eap->nextcmd != NULL)
22905 *p = NUL;
22906
22907 if (!eap->skip)
22908 fp = find_func(name);
22909 vim_free(name);
22910
22911 if (!eap->skip)
22912 {
22913 if (fp == NULL)
22914 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 return;
22917 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022918 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 {
22920 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22921 return;
22922 }
22923
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022924 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 /* Delete the dict item that refers to the function, it will
22927 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022928 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022930 else
22931 func_free(fp);
22932 }
22933}
22934
22935/*
22936 * Free a function and remove it from the list of functions.
22937 */
22938 static void
22939func_free(fp)
22940 ufunc_T *fp;
22941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022942 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022943
22944 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022945 ga_clear_strings(&(fp->uf_args));
22946 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022947#ifdef FEAT_PROFILE
22948 vim_free(fp->uf_tml_count);
22949 vim_free(fp->uf_tml_total);
22950 vim_free(fp->uf_tml_self);
22951#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022952
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022953 /* remove the function from the function hashtable */
22954 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22955 if (HASHITEM_EMPTY(hi))
22956 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022957 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022958 hash_remove(&func_hashtab, hi);
22959
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022960 vim_free(fp);
22961}
22962
22963/*
22964 * Unreference a Function: decrement the reference count and free it when it
22965 * becomes zero. Only for numbered functions.
22966 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022967 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968func_unref(name)
22969 char_u *name;
22970{
22971 ufunc_T *fp;
22972
22973 if (name != NULL && isdigit(*name))
22974 {
22975 fp = find_func(name);
22976 if (fp == NULL)
22977 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022978 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 {
22980 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022981 * when "uf_calls" becomes zero. */
22982 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022983 func_free(fp);
22984 }
22985 }
22986}
22987
22988/*
22989 * Count a reference to a Function.
22990 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022991 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022992func_ref(name)
22993 char_u *name;
22994{
22995 ufunc_T *fp;
22996
22997 if (name != NULL && isdigit(*name))
22998 {
22999 fp = find_func(name);
23000 if (fp == NULL)
23001 EMSG2(_(e_intern2), "func_ref()");
23002 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023003 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 }
23005}
23006
23007/*
23008 * Call a user function.
23009 */
23010 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023011call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 ufunc_T *fp; /* pointer to function */
23013 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 typval_T *argvars; /* arguments */
23015 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 linenr_T firstline; /* first line of range */
23017 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019{
Bram Moolenaar33570922005-01-25 22:26:29 +000023020 char_u *save_sourcing_name;
23021 linenr_T save_sourcing_lnum;
23022 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023023 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 int save_did_emsg;
23025 static int depth = 0;
23026 dictitem_T *v;
23027 int fixvar_idx = 0; /* index in fixvar[] */
23028 int i;
23029 int ai;
23030 char_u numbuf[NUMBUFLEN];
23031 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023032#ifdef FEAT_PROFILE
23033 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023034 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036
23037 /* If depth of calling is getting too high, don't execute the function */
23038 if (depth >= p_mfd)
23039 {
23040 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023041 rettv->v_type = VAR_NUMBER;
23042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 return;
23044 }
23045 ++depth;
23046
23047 line_breakcheck(); /* check for CTRL-C hit */
23048
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023049 fc = (funccall_T *)alloc(sizeof(funccall_T));
23050 fc->caller = current_funccal;
23051 current_funccal = fc;
23052 fc->func = fp;
23053 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023054 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023055 fc->linenr = 0;
23056 fc->returned = FALSE;
23057 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023059 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23060 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023063 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23065 * each argument variable and saves a lot of time.
23066 */
23067 /*
23068 * Init l: variables.
23069 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023070 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023071 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023072 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023073 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23074 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023075 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023076 name = v->di_key;
23077 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023078 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023079 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023080 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023081 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023082 v->di_tv.vval.v_dict = selfdict;
23083 ++selfdict->dv_refcount;
23084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023085
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 /*
23087 * Init a: variables.
23088 * Set a:0 to "argcount".
23089 * Set a:000 to a list with room for the "..." arguments.
23090 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023091 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023092 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023093 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023094 /* Use "name" to avoid a warning from some compiler that checks the
23095 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023096 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023097 name = v->di_key;
23098 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023099 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023100 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023102 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023103 v->di_tv.vval.v_list = &fc->l_varlist;
23104 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23105 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23106 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023107
23108 /*
23109 * Set a:firstline to "firstline" and a:lastline to "lastline".
23110 * Set a:name to named arguments.
23111 * Set a:N to the "..." arguments.
23112 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023113 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023114 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023115 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 (varnumber_T)lastline);
23117 for (i = 0; i < argcount; ++i)
23118 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023119 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 if (ai < 0)
23121 /* named argument a:name */
23122 name = FUNCARG(fp, i);
23123 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023124 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023125 /* "..." argument a:1, a:2, etc. */
23126 sprintf((char *)numbuf, "%d", ai + 1);
23127 name = numbuf;
23128 }
23129 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23130 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023131 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023132 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23133 }
23134 else
23135 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023136 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23137 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 if (v == NULL)
23139 break;
23140 v->di_flags = DI_FLAGS_RO;
23141 }
23142 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023143 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023144
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023145 /* Note: the values are copied directly to avoid alloc/free.
23146 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023148 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023149
23150 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23151 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023152 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23153 fc->l_listitems[ai].li_tv = argvars[i];
23154 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023155 }
23156 }
23157
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 /* Don't redraw while executing the function. */
23159 ++RedrawingDisabled;
23160 save_sourcing_name = sourcing_name;
23161 save_sourcing_lnum = sourcing_lnum;
23162 sourcing_lnum = 1;
23163 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023164 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 if (sourcing_name != NULL)
23166 {
23167 if (save_sourcing_name != NULL
23168 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23169 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23170 else
23171 STRCPY(sourcing_name, "function ");
23172 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23173
23174 if (p_verbose >= 12)
23175 {
23176 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023177 verbose_enter_scroll();
23178
Bram Moolenaar555b2802005-05-19 21:08:39 +000023179 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 if (p_verbose >= 14)
23181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023183 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023184 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023185 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186
23187 msg_puts((char_u *)"(");
23188 for (i = 0; i < argcount; ++i)
23189 {
23190 if (i > 0)
23191 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023192 if (argvars[i].v_type == VAR_NUMBER)
23193 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 else
23195 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023196 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23197 if (s != NULL)
23198 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023199 if (vim_strsize(s) > MSG_BUF_CLEN)
23200 {
23201 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23202 s = buf;
23203 }
23204 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023205 vim_free(tofree);
23206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 }
23208 }
23209 msg_puts((char_u *)")");
23210 }
23211 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023212
23213 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 --no_wait_return;
23215 }
23216 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023217#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023218 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023219 {
23220 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23221 func_do_profile(fp);
23222 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023223 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023224 {
23225 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023226 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023227 profile_zero(&fp->uf_tm_children);
23228 }
23229 script_prof_save(&wait_start);
23230 }
23231#endif
23232
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023234 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 save_did_emsg = did_emsg;
23236 did_emsg = FALSE;
23237
23238 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023239 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23241
23242 --RedrawingDisabled;
23243
23244 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023245 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023247 clear_tv(rettv);
23248 rettv->v_type = VAR_NUMBER;
23249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 }
23251
Bram Moolenaar05159a02005-02-26 23:04:13 +000023252#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023253 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023254 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023255 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023256 profile_end(&call_start);
23257 profile_sub_wait(&wait_start, &call_start);
23258 profile_add(&fp->uf_tm_total, &call_start);
23259 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023260 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023261 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023262 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23263 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023264 }
23265 }
23266#endif
23267
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 /* when being verbose, mention the return value */
23269 if (p_verbose >= 12)
23270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023272 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023275 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023276 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023277 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023278 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023279 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023281 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023282 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023283 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023284 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023285
Bram Moolenaar555b2802005-05-19 21:08:39 +000023286 /* The value may be very long. Skip the middle part, so that we
23287 * have some idea how it starts and ends. smsg() would always
23288 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023289 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023290 if (s != NULL)
23291 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023292 if (vim_strsize(s) > MSG_BUF_CLEN)
23293 {
23294 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23295 s = buf;
23296 }
23297 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023298 vim_free(tofree);
23299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 }
23301 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023302
23303 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 --no_wait_return;
23305 }
23306
23307 vim_free(sourcing_name);
23308 sourcing_name = save_sourcing_name;
23309 sourcing_lnum = save_sourcing_lnum;
23310 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023311#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023312 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023313 script_prof_restore(&wait_start);
23314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315
23316 if (p_verbose >= 12 && sourcing_name != NULL)
23317 {
23318 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023319 verbose_enter_scroll();
23320
Bram Moolenaar555b2802005-05-19 21:08:39 +000023321 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023323
23324 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 --no_wait_return;
23326 }
23327
23328 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023329 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023331
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023332 /* If the a:000 list and the l: and a: dicts are not referenced we can
23333 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023334 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23335 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23336 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23337 {
23338 free_funccal(fc, FALSE);
23339 }
23340 else
23341 {
23342 hashitem_T *hi;
23343 listitem_T *li;
23344 int todo;
23345
23346 /* "fc" is still in use. This can happen when returning "a:000" or
23347 * assigning "l:" to a global variable.
23348 * Link "fc" in the list for garbage collection later. */
23349 fc->caller = previous_funccal;
23350 previous_funccal = fc;
23351
23352 /* Make a copy of the a: variables, since we didn't do that above. */
23353 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23354 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23355 {
23356 if (!HASHITEM_EMPTY(hi))
23357 {
23358 --todo;
23359 v = HI2DI(hi);
23360 copy_tv(&v->di_tv, &v->di_tv);
23361 }
23362 }
23363
23364 /* Make a copy of the a:000 items, since we didn't do that above. */
23365 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23366 copy_tv(&li->li_tv, &li->li_tv);
23367 }
23368}
23369
23370/*
23371 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023372 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023373 */
23374 static int
23375can_free_funccal(fc, copyID)
23376 funccall_T *fc;
23377 int copyID;
23378{
23379 return (fc->l_varlist.lv_copyID != copyID
23380 && fc->l_vars.dv_copyID != copyID
23381 && fc->l_avars.dv_copyID != copyID);
23382}
23383
23384/*
23385 * Free "fc" and what it contains.
23386 */
23387 static void
23388free_funccal(fc, free_val)
23389 funccall_T *fc;
23390 int free_val; /* a: vars were allocated */
23391{
23392 listitem_T *li;
23393
23394 /* The a: variables typevals may not have been allocated, only free the
23395 * allocated variables. */
23396 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23397
23398 /* free all l: variables */
23399 vars_clear(&fc->l_vars.dv_hashtab);
23400
23401 /* Free the a:000 variables if they were allocated. */
23402 if (free_val)
23403 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23404 clear_tv(&li->li_tv);
23405
23406 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407}
23408
23409/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 * Add a number variable "name" to dict "dp" with value "nr".
23411 */
23412 static void
23413add_nr_var(dp, v, name, nr)
23414 dict_T *dp;
23415 dictitem_T *v;
23416 char *name;
23417 varnumber_T nr;
23418{
23419 STRCPY(v->di_key, name);
23420 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23421 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23422 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023423 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023424 v->di_tv.vval.v_number = nr;
23425}
23426
23427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 * ":return [expr]"
23429 */
23430 void
23431ex_return(eap)
23432 exarg_T *eap;
23433{
23434 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023435 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 int returning = FALSE;
23437
23438 if (current_funccal == NULL)
23439 {
23440 EMSG(_("E133: :return not inside a function"));
23441 return;
23442 }
23443
23444 if (eap->skip)
23445 ++emsg_skip;
23446
23447 eap->nextcmd = NULL;
23448 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023449 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 {
23451 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023452 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023454 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 }
23456 /* It's safer to return also on error. */
23457 else if (!eap->skip)
23458 {
23459 /*
23460 * Return unless the expression evaluation has been cancelled due to an
23461 * aborting error, an interrupt, or an exception.
23462 */
23463 if (!aborting())
23464 returning = do_return(eap, FALSE, TRUE, NULL);
23465 }
23466
23467 /* When skipping or the return gets pending, advance to the next command
23468 * in this line (!returning). Otherwise, ignore the rest of the line.
23469 * Following lines will be ignored by get_func_line(). */
23470 if (returning)
23471 eap->nextcmd = NULL;
23472 else if (eap->nextcmd == NULL) /* no argument */
23473 eap->nextcmd = check_nextcmd(arg);
23474
23475 if (eap->skip)
23476 --emsg_skip;
23477}
23478
23479/*
23480 * Return from a function. Possibly makes the return pending. Also called
23481 * for a pending return at the ":endtry" or after returning from an extra
23482 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023483 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023484 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 * FALSE when the return gets pending.
23486 */
23487 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023488do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 exarg_T *eap;
23490 int reanimate;
23491 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023492 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493{
23494 int idx;
23495 struct condstack *cstack = eap->cstack;
23496
23497 if (reanimate)
23498 /* Undo the return. */
23499 current_funccal->returned = FALSE;
23500
23501 /*
23502 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23503 * not in its finally clause (which then is to be executed next) is found.
23504 * In this case, make the ":return" pending for execution at the ":endtry".
23505 * Otherwise, return normally.
23506 */
23507 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23508 if (idx >= 0)
23509 {
23510 cstack->cs_pending[idx] = CSTP_RETURN;
23511
23512 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023513 /* A pending return again gets pending. "rettv" points to an
23514 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023516 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 else
23518 {
23519 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023520 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023522 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023524 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 {
23526 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023527 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023528 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 else
23530 EMSG(_(e_outofmem));
23531 }
23532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023533 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534
23535 if (reanimate)
23536 {
23537 /* The pending return value could be overwritten by a ":return"
23538 * without argument in a finally clause; reset the default
23539 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023540 current_funccal->rettv->v_type = VAR_NUMBER;
23541 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 }
23543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023544 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 }
23546 else
23547 {
23548 current_funccal->returned = TRUE;
23549
23550 /* If the return is carried out now, store the return value. For
23551 * a return immediately after reanimation, the value is already
23552 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023553 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023555 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023556 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023558 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 }
23560 }
23561
23562 return idx < 0;
23563}
23564
23565/*
23566 * Free the variable with a pending return value.
23567 */
23568 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023569discard_pending_return(rettv)
23570 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571{
Bram Moolenaar33570922005-01-25 22:26:29 +000023572 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573}
23574
23575/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023576 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 * is an allocated string. Used by report_pending() for verbose messages.
23578 */
23579 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023580get_return_cmd(rettv)
23581 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023583 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023584 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023585 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023587 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023588 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023589 if (s == NULL)
23590 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023591
23592 STRCPY(IObuff, ":return ");
23593 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23594 if (STRLEN(s) + 8 >= IOSIZE)
23595 STRCPY(IObuff + IOSIZE - 4, "...");
23596 vim_free(tofree);
23597 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598}
23599
23600/*
23601 * Get next function line.
23602 * Called by do_cmdline() to get the next line.
23603 * Returns allocated string, or NULL for end of function.
23604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 char_u *
23606get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023607 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023609 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610{
Bram Moolenaar33570922005-01-25 22:26:29 +000023611 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023612 ufunc_T *fp = fcp->func;
23613 char_u *retval;
23614 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615
23616 /* If breakpoints have been added/deleted need to check for it. */
23617 if (fcp->dbg_tick != debug_tick)
23618 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023619 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 sourcing_lnum);
23621 fcp->dbg_tick = debug_tick;
23622 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023623#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023624 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023625 func_line_end(cookie);
23626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627
Bram Moolenaar05159a02005-02-26 23:04:13 +000023628 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023629 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23630 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 retval = NULL;
23632 else
23633 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023634 /* Skip NULL lines (continuation lines). */
23635 while (fcp->linenr < gap->ga_len
23636 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23637 ++fcp->linenr;
23638 if (fcp->linenr >= gap->ga_len)
23639 retval = NULL;
23640 else
23641 {
23642 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23643 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023644#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023645 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023646 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023647#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 }
23650
23651 /* Did we encounter a breakpoint? */
23652 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23653 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023654 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023656 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 sourcing_lnum);
23658 fcp->dbg_tick = debug_tick;
23659 }
23660
23661 return retval;
23662}
23663
Bram Moolenaar05159a02005-02-26 23:04:13 +000023664#if defined(FEAT_PROFILE) || defined(PROTO)
23665/*
23666 * Called when starting to read a function line.
23667 * "sourcing_lnum" must be correct!
23668 * When skipping lines it may not actually be executed, but we won't find out
23669 * until later and we need to store the time now.
23670 */
23671 void
23672func_line_start(cookie)
23673 void *cookie;
23674{
23675 funccall_T *fcp = (funccall_T *)cookie;
23676 ufunc_T *fp = fcp->func;
23677
23678 if (fp->uf_profiling && sourcing_lnum >= 1
23679 && sourcing_lnum <= fp->uf_lines.ga_len)
23680 {
23681 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023682 /* Skip continuation lines. */
23683 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23684 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023685 fp->uf_tml_execed = FALSE;
23686 profile_start(&fp->uf_tml_start);
23687 profile_zero(&fp->uf_tml_children);
23688 profile_get_wait(&fp->uf_tml_wait);
23689 }
23690}
23691
23692/*
23693 * Called when actually executing a function line.
23694 */
23695 void
23696func_line_exec(cookie)
23697 void *cookie;
23698{
23699 funccall_T *fcp = (funccall_T *)cookie;
23700 ufunc_T *fp = fcp->func;
23701
23702 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23703 fp->uf_tml_execed = TRUE;
23704}
23705
23706/*
23707 * Called when done with a function line.
23708 */
23709 void
23710func_line_end(cookie)
23711 void *cookie;
23712{
23713 funccall_T *fcp = (funccall_T *)cookie;
23714 ufunc_T *fp = fcp->func;
23715
23716 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23717 {
23718 if (fp->uf_tml_execed)
23719 {
23720 ++fp->uf_tml_count[fp->uf_tml_idx];
23721 profile_end(&fp->uf_tml_start);
23722 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023723 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023724 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23725 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023726 }
23727 fp->uf_tml_idx = -1;
23728 }
23729}
23730#endif
23731
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732/*
23733 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023734 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 */
23736 int
23737func_has_ended(cookie)
23738 void *cookie;
23739{
Bram Moolenaar33570922005-01-25 22:26:29 +000023740 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741
23742 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23743 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023744 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 || fcp->returned);
23746}
23747
23748/*
23749 * return TRUE if cookie indicates a function which "abort"s on errors.
23750 */
23751 int
23752func_has_abort(cookie)
23753 void *cookie;
23754{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023755 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756}
23757
23758#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23759typedef enum
23760{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023761 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23762 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23763 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764} var_flavour_T;
23765
23766static var_flavour_T var_flavour __ARGS((char_u *varname));
23767
23768 static var_flavour_T
23769var_flavour(varname)
23770 char_u *varname;
23771{
23772 char_u *p = varname;
23773
23774 if (ASCII_ISUPPER(*p))
23775 {
23776 while (*(++p))
23777 if (ASCII_ISLOWER(*p))
23778 return VAR_FLAVOUR_SESSION;
23779 return VAR_FLAVOUR_VIMINFO;
23780 }
23781 else
23782 return VAR_FLAVOUR_DEFAULT;
23783}
23784#endif
23785
23786#if defined(FEAT_VIMINFO) || defined(PROTO)
23787/*
23788 * Restore global vars that start with a capital from the viminfo file
23789 */
23790 int
23791read_viminfo_varlist(virp, writing)
23792 vir_T *virp;
23793 int writing;
23794{
23795 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023796 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023797 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798
23799 if (!writing && (find_viminfo_parameter('!') != NULL))
23800 {
23801 tab = vim_strchr(virp->vir_line + 1, '\t');
23802 if (tab != NULL)
23803 {
23804 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023805 switch (*tab)
23806 {
23807 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023808#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023809 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023810#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023811 case 'D': type = VAR_DICT; break;
23812 case 'L': type = VAR_LIST; break;
23813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814
23815 tab = vim_strchr(tab, '\t');
23816 if (tab != NULL)
23817 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023818 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023819 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023820 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023822#ifdef FEAT_FLOAT
23823 else if (type == VAR_FLOAT)
23824 (void)string2float(tab + 1, &tv.vval.v_float);
23825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023827 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023828 if (type == VAR_DICT || type == VAR_LIST)
23829 {
23830 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23831
23832 if (etv == NULL)
23833 /* Failed to parse back the dict or list, use it as a
23834 * string. */
23835 tv.v_type = VAR_STRING;
23836 else
23837 {
23838 vim_free(tv.vval.v_string);
23839 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023840 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023841 }
23842 }
23843
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023844 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023845
23846 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023847 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023848 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23849 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 }
23851 }
23852 }
23853
23854 return viminfo_readline(virp);
23855}
23856
23857/*
23858 * Write global vars that start with a capital to the viminfo file
23859 */
23860 void
23861write_viminfo_varlist(fp)
23862 FILE *fp;
23863{
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 hashitem_T *hi;
23865 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023866 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023867 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023868 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023869 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023870 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871
23872 if (find_viminfo_parameter('!') == NULL)
23873 return;
23874
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023875 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023877 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023878 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023880 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023882 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023883 this_var = HI2DI(hi);
23884 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023886 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023887 {
23888 case VAR_STRING: s = "STR"; break;
23889 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023890#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023891 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023892#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023893 case VAR_DICT: s = "DIC"; break;
23894 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023895 default: continue;
23896 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023897 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023898 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023899 if (p != NULL)
23900 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023901 vim_free(tofree);
23902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 }
23904 }
23905}
23906#endif
23907
23908#if defined(FEAT_SESSION) || defined(PROTO)
23909 int
23910store_session_globals(fd)
23911 FILE *fd;
23912{
Bram Moolenaar33570922005-01-25 22:26:29 +000023913 hashitem_T *hi;
23914 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023915 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 char_u *p, *t;
23917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023918 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023921 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023923 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023924 this_var = HI2DI(hi);
23925 if ((this_var->di_tv.v_type == VAR_NUMBER
23926 || this_var->di_tv.v_type == VAR_STRING)
23927 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023928 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023929 /* Escape special characters with a backslash. Turn a LF and
23930 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023931 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023932 (char_u *)"\\\"\n\r");
23933 if (p == NULL) /* out of memory */
23934 break;
23935 for (t = p; *t != NUL; ++t)
23936 if (*t == '\n')
23937 *t = 'n';
23938 else if (*t == '\r')
23939 *t = 'r';
23940 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023941 this_var->di_key,
23942 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23943 : ' ',
23944 p,
23945 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23946 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023947 || put_eol(fd) == FAIL)
23948 {
23949 vim_free(p);
23950 return FAIL;
23951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 vim_free(p);
23953 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023954#ifdef FEAT_FLOAT
23955 else if (this_var->di_tv.v_type == VAR_FLOAT
23956 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23957 {
23958 float_T f = this_var->di_tv.vval.v_float;
23959 int sign = ' ';
23960
23961 if (f < 0)
23962 {
23963 f = -f;
23964 sign = '-';
23965 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023966 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023967 this_var->di_key, sign, f) < 0)
23968 || put_eol(fd) == FAIL)
23969 return FAIL;
23970 }
23971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 }
23973 }
23974 return OK;
23975}
23976#endif
23977
Bram Moolenaar661b1822005-07-28 22:36:45 +000023978/*
23979 * Display script name where an item was last set.
23980 * Should only be invoked when 'verbose' is non-zero.
23981 */
23982 void
23983last_set_msg(scriptID)
23984 scid_T scriptID;
23985{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023986 char_u *p;
23987
Bram Moolenaar661b1822005-07-28 22:36:45 +000023988 if (scriptID != 0)
23989 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023990 p = home_replace_save(NULL, get_scriptname(scriptID));
23991 if (p != NULL)
23992 {
23993 verbose_enter();
23994 MSG_PUTS(_("\n\tLast set from "));
23995 MSG_PUTS(p);
23996 vim_free(p);
23997 verbose_leave();
23998 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023999 }
24000}
24001
Bram Moolenaard812df62008-11-09 12:46:09 +000024002/*
24003 * List v:oldfiles in a nice way.
24004 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024005 void
24006ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024007 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024008{
24009 list_T *l = vimvars[VV_OLDFILES].vv_list;
24010 listitem_T *li;
24011 int nr = 0;
24012
24013 if (l == NULL)
24014 msg((char_u *)_("No old files"));
24015 else
24016 {
24017 msg_start();
24018 msg_scroll = TRUE;
24019 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24020 {
24021 msg_outnum((long)++nr);
24022 MSG_PUTS(": ");
24023 msg_outtrans(get_tv_string(&li->li_tv));
24024 msg_putchar('\n');
24025 out_flush(); /* output one line at a time */
24026 ui_breakcheck();
24027 }
24028 /* Assume "got_int" was set to truncate the listing. */
24029 got_int = FALSE;
24030
24031#ifdef FEAT_BROWSE_CMD
24032 if (cmdmod.browse)
24033 {
24034 quit_more = FALSE;
24035 nr = prompt_for_number(FALSE);
24036 msg_starthere();
24037 if (nr > 0)
24038 {
24039 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24040 (long)nr);
24041
24042 if (p != NULL)
24043 {
24044 p = expand_env_save(p);
24045 eap->arg = p;
24046 eap->cmdidx = CMD_edit;
24047 cmdmod.browse = FALSE;
24048 do_exedit(eap, NULL);
24049 vim_free(p);
24050 }
24051 }
24052 }
24053#endif
24054 }
24055}
24056
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057#endif /* FEAT_EVAL */
24058
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024060#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061
24062#ifdef WIN3264
24063/*
24064 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24065 */
24066static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24067static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24068static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24069
24070/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024071 * Get the short path (8.3) for the filename in "fnamep".
24072 * Only works for a valid file name.
24073 * When the path gets longer "fnamep" is changed and the allocated buffer
24074 * is put in "bufp".
24075 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24076 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 */
24078 static int
24079get_short_pathname(fnamep, bufp, fnamelen)
24080 char_u **fnamep;
24081 char_u **bufp;
24082 int *fnamelen;
24083{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024084 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 char_u *newbuf;
24086
24087 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 l = GetShortPathName(*fnamep, *fnamep, len);
24089 if (l > len - 1)
24090 {
24091 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024092 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 newbuf = vim_strnsave(*fnamep, l);
24094 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024096
24097 vim_free(*bufp);
24098 *fnamep = *bufp = newbuf;
24099
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024100 /* Really should always succeed, as the buffer is big enough. */
24101 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 }
24103
24104 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024105 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106}
24107
24108/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024109 * Get the short path (8.3) for the filename in "fname". The converted
24110 * path is returned in "bufp".
24111 *
24112 * Some of the directories specified in "fname" may not exist. This function
24113 * will shorten the existing directories at the beginning of the path and then
24114 * append the remaining non-existing path.
24115 *
24116 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024117 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024118 * bufp - Pointer to an allocated buffer for the filename.
24119 * fnamelen - Length of the filename pointed to by fname
24120 *
24121 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 */
24123 static int
24124shortpath_for_invalid_fname(fname, bufp, fnamelen)
24125 char_u **fname;
24126 char_u **bufp;
24127 int *fnamelen;
24128{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024129 char_u *short_fname, *save_fname, *pbuf_unused;
24130 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024132 int old_len, len;
24133 int new_len, sfx_len;
24134 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135
24136 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024137 old_len = *fnamelen;
24138 save_fname = vim_strnsave(*fname, old_len);
24139 pbuf_unused = NULL;
24140 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024142 endp = save_fname + old_len - 1; /* Find the end of the copy */
24143 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024145 /*
24146 * Try shortening the supplied path till it succeeds by removing one
24147 * directory at a time from the tail of the path.
24148 */
24149 len = 0;
24150 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024152 /* go back one path-separator */
24153 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24154 --endp;
24155 if (endp <= save_fname)
24156 break; /* processed the complete path */
24157
24158 /*
24159 * Replace the path separator with a NUL and try to shorten the
24160 * resulting path.
24161 */
24162 ch = *endp;
24163 *endp = 0;
24164 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024165 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024166 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24167 {
24168 retval = FAIL;
24169 goto theend;
24170 }
24171 *endp = ch; /* preserve the string */
24172
24173 if (len > 0)
24174 break; /* successfully shortened the path */
24175
24176 /* failed to shorten the path. Skip the path separator */
24177 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 }
24179
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024180 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024182 /*
24183 * Succeeded in shortening the path. Now concatenate the shortened
24184 * path with the remaining path at the tail.
24185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024187 /* Compute the length of the new path. */
24188 sfx_len = (int)(save_endp - endp) + 1;
24189 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024191 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024193 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024195 /* There is not enough space in the currently allocated string,
24196 * copy it to a buffer big enough. */
24197 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024199 {
24200 retval = FAIL;
24201 goto theend;
24202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 }
24204 else
24205 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024206 /* Transfer short_fname to the main buffer (it's big enough),
24207 * unless get_short_pathname() did its work in-place. */
24208 *fname = *bufp = save_fname;
24209 if (short_fname != save_fname)
24210 vim_strncpy(save_fname, short_fname, len);
24211 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024213
24214 /* concat the not-shortened part of the path */
24215 vim_strncpy(*fname + len, endp, sfx_len);
24216 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024218
24219theend:
24220 vim_free(pbuf_unused);
24221 vim_free(save_fname);
24222
24223 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224}
24225
24226/*
24227 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024228 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 */
24230 static int
24231shortpath_for_partial(fnamep, bufp, fnamelen)
24232 char_u **fnamep;
24233 char_u **bufp;
24234 int *fnamelen;
24235{
24236 int sepcount, len, tflen;
24237 char_u *p;
24238 char_u *pbuf, *tfname;
24239 int hasTilde;
24240
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024241 /* Count up the path separators from the RHS.. so we know which part
24242 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024244 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 if (vim_ispathsep(*p))
24246 ++sepcount;
24247
24248 /* Need full path first (use expand_env() to remove a "~/") */
24249 hasTilde = (**fnamep == '~');
24250 if (hasTilde)
24251 pbuf = tfname = expand_env_save(*fnamep);
24252 else
24253 pbuf = tfname = FullName_save(*fnamep, FALSE);
24254
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024255 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024257 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259
24260 if (len == 0)
24261 {
24262 /* Don't have a valid filename, so shorten the rest of the
24263 * path if we can. This CAN give us invalid 8.3 filenames, but
24264 * there's not a lot of point in guessing what it might be.
24265 */
24266 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024267 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270
24271 /* Count the paths backward to find the beginning of the desired string. */
24272 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024273 {
24274#ifdef FEAT_MBYTE
24275 if (has_mbyte)
24276 p -= mb_head_off(tfname, p);
24277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 if (vim_ispathsep(*p))
24279 {
24280 if (sepcount == 0 || (hasTilde && sepcount == 1))
24281 break;
24282 else
24283 sepcount --;
24284 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 if (hasTilde)
24287 {
24288 --p;
24289 if (p >= tfname)
24290 *p = '~';
24291 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 }
24294 else
24295 ++p;
24296
24297 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24298 vim_free(*bufp);
24299 *fnamelen = (int)STRLEN(p);
24300 *bufp = pbuf;
24301 *fnamep = p;
24302
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024303 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304}
24305#endif /* WIN3264 */
24306
24307/*
24308 * Adjust a filename, according to a string of modifiers.
24309 * *fnamep must be NUL terminated when called. When returning, the length is
24310 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024311 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 * When there is an error, *fnamep is set to NULL.
24313 */
24314 int
24315modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24316 char_u *src; /* string with modifiers */
24317 int *usedlen; /* characters after src that are used */
24318 char_u **fnamep; /* file name so far */
24319 char_u **bufp; /* buffer for allocated file name or NULL */
24320 int *fnamelen; /* length of fnamep */
24321{
24322 int valid = 0;
24323 char_u *tail;
24324 char_u *s, *p, *pbuf;
24325 char_u dirname[MAXPATHL];
24326 int c;
24327 int has_fullname = 0;
24328#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024329 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 int has_shortname = 0;
24331#endif
24332
24333repeat:
24334 /* ":p" - full path/file_name */
24335 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24336 {
24337 has_fullname = 1;
24338
24339 valid |= VALID_PATH;
24340 *usedlen += 2;
24341
24342 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24343 if ((*fnamep)[0] == '~'
24344#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24345 && ((*fnamep)[1] == '/'
24346# ifdef BACKSLASH_IN_FILENAME
24347 || (*fnamep)[1] == '\\'
24348# endif
24349 || (*fnamep)[1] == NUL)
24350
24351#endif
24352 )
24353 {
24354 *fnamep = expand_env_save(*fnamep);
24355 vim_free(*bufp); /* free any allocated file name */
24356 *bufp = *fnamep;
24357 if (*fnamep == NULL)
24358 return -1;
24359 }
24360
24361 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024362 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 {
24364 if (vim_ispathsep(*p)
24365 && p[1] == '.'
24366 && (p[2] == NUL
24367 || vim_ispathsep(p[2])
24368 || (p[2] == '.'
24369 && (p[3] == NUL || vim_ispathsep(p[3])))))
24370 break;
24371 }
24372
24373 /* FullName_save() is slow, don't use it when not needed. */
24374 if (*p != NUL || !vim_isAbsName(*fnamep))
24375 {
24376 *fnamep = FullName_save(*fnamep, *p != NUL);
24377 vim_free(*bufp); /* free any allocated file name */
24378 *bufp = *fnamep;
24379 if (*fnamep == NULL)
24380 return -1;
24381 }
24382
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024383#ifdef WIN3264
24384# if _WIN32_WINNT >= 0x0500
24385 if (vim_strchr(*fnamep, '~') != NULL)
24386 {
24387 /* Expand 8.3 filename to full path. Needed to make sure the same
24388 * file does not have two different names.
24389 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24390 p = alloc(_MAX_PATH + 1);
24391 if (p != NULL)
24392 {
24393 if (GetLongPathName(*fnamep, p, MAXPATHL))
24394 {
24395 vim_free(*bufp);
24396 *bufp = *fnamep = p;
24397 }
24398 else
24399 vim_free(p);
24400 }
24401 }
24402# endif
24403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 /* Append a path separator to a directory. */
24405 if (mch_isdir(*fnamep))
24406 {
24407 /* Make room for one or two extra characters. */
24408 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24409 vim_free(*bufp); /* free any allocated file name */
24410 *bufp = *fnamep;
24411 if (*fnamep == NULL)
24412 return -1;
24413 add_pathsep(*fnamep);
24414 }
24415 }
24416
24417 /* ":." - path relative to the current directory */
24418 /* ":~" - path relative to the home directory */
24419 /* ":8" - shortname path - postponed till after */
24420 while (src[*usedlen] == ':'
24421 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24422 {
24423 *usedlen += 2;
24424 if (c == '8')
24425 {
24426#ifdef WIN3264
24427 has_shortname = 1; /* Postpone this. */
24428#endif
24429 continue;
24430 }
24431 pbuf = NULL;
24432 /* Need full path first (use expand_env() to remove a "~/") */
24433 if (!has_fullname)
24434 {
24435 if (c == '.' && **fnamep == '~')
24436 p = pbuf = expand_env_save(*fnamep);
24437 else
24438 p = pbuf = FullName_save(*fnamep, FALSE);
24439 }
24440 else
24441 p = *fnamep;
24442
24443 has_fullname = 0;
24444
24445 if (p != NULL)
24446 {
24447 if (c == '.')
24448 {
24449 mch_dirname(dirname, MAXPATHL);
24450 s = shorten_fname(p, dirname);
24451 if (s != NULL)
24452 {
24453 *fnamep = s;
24454 if (pbuf != NULL)
24455 {
24456 vim_free(*bufp); /* free any allocated file name */
24457 *bufp = pbuf;
24458 pbuf = NULL;
24459 }
24460 }
24461 }
24462 else
24463 {
24464 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24465 /* Only replace it when it starts with '~' */
24466 if (*dirname == '~')
24467 {
24468 s = vim_strsave(dirname);
24469 if (s != NULL)
24470 {
24471 *fnamep = s;
24472 vim_free(*bufp);
24473 *bufp = s;
24474 }
24475 }
24476 }
24477 vim_free(pbuf);
24478 }
24479 }
24480
24481 tail = gettail(*fnamep);
24482 *fnamelen = (int)STRLEN(*fnamep);
24483
24484 /* ":h" - head, remove "/file_name", can be repeated */
24485 /* Don't remove the first "/" or "c:\" */
24486 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24487 {
24488 valid |= VALID_HEAD;
24489 *usedlen += 2;
24490 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024491 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024492 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 *fnamelen = (int)(tail - *fnamep);
24494#ifdef VMS
24495 if (*fnamelen > 0)
24496 *fnamelen += 1; /* the path separator is part of the path */
24497#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024498 if (*fnamelen == 0)
24499 {
24500 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24501 p = vim_strsave((char_u *)".");
24502 if (p == NULL)
24503 return -1;
24504 vim_free(*bufp);
24505 *bufp = *fnamep = tail = p;
24506 *fnamelen = 1;
24507 }
24508 else
24509 {
24510 while (tail > s && !after_pathsep(s, tail))
24511 mb_ptr_back(*fnamep, tail);
24512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 }
24514
24515 /* ":8" - shortname */
24516 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24517 {
24518 *usedlen += 2;
24519#ifdef WIN3264
24520 has_shortname = 1;
24521#endif
24522 }
24523
24524#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024525 /*
24526 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 */
24528 if (has_shortname)
24529 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024530 /* Copy the string if it is shortened by :h and when it wasn't copied
24531 * yet, because we are going to change it in place. Avoids changing
24532 * the buffer name for "%:8". */
24533 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 {
24535 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024536 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 return -1;
24538 vim_free(*bufp);
24539 *bufp = *fnamep = p;
24540 }
24541
24542 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024543 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544 if (!has_fullname && !vim_isAbsName(*fnamep))
24545 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024546 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 return -1;
24548 }
24549 else
24550 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024551 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552
Bram Moolenaardc935552011-08-17 15:23:23 +020024553 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024555 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556 return -1;
24557
24558 if (l == 0)
24559 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024560 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024562 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 return -1;
24564 }
24565 *fnamelen = l;
24566 }
24567 }
24568#endif /* WIN3264 */
24569
24570 /* ":t" - tail, just the basename */
24571 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24572 {
24573 *usedlen += 2;
24574 *fnamelen -= (int)(tail - *fnamep);
24575 *fnamep = tail;
24576 }
24577
24578 /* ":e" - extension, can be repeated */
24579 /* ":r" - root, without extension, can be repeated */
24580 while (src[*usedlen] == ':'
24581 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24582 {
24583 /* find a '.' in the tail:
24584 * - for second :e: before the current fname
24585 * - otherwise: The last '.'
24586 */
24587 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24588 s = *fnamep - 2;
24589 else
24590 s = *fnamep + *fnamelen - 1;
24591 for ( ; s > tail; --s)
24592 if (s[0] == '.')
24593 break;
24594 if (src[*usedlen + 1] == 'e') /* :e */
24595 {
24596 if (s > tail)
24597 {
24598 *fnamelen += (int)(*fnamep - (s + 1));
24599 *fnamep = s + 1;
24600#ifdef VMS
24601 /* cut version from the extension */
24602 s = *fnamep + *fnamelen - 1;
24603 for ( ; s > *fnamep; --s)
24604 if (s[0] == ';')
24605 break;
24606 if (s > *fnamep)
24607 *fnamelen = s - *fnamep;
24608#endif
24609 }
24610 else if (*fnamep <= tail)
24611 *fnamelen = 0;
24612 }
24613 else /* :r */
24614 {
24615 if (s > tail) /* remove one extension */
24616 *fnamelen = (int)(s - *fnamep);
24617 }
24618 *usedlen += 2;
24619 }
24620
24621 /* ":s?pat?foo?" - substitute */
24622 /* ":gs?pat?foo?" - global substitute */
24623 if (src[*usedlen] == ':'
24624 && (src[*usedlen + 1] == 's'
24625 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24626 {
24627 char_u *str;
24628 char_u *pat;
24629 char_u *sub;
24630 int sep;
24631 char_u *flags;
24632 int didit = FALSE;
24633
24634 flags = (char_u *)"";
24635 s = src + *usedlen + 2;
24636 if (src[*usedlen + 1] == 'g')
24637 {
24638 flags = (char_u *)"g";
24639 ++s;
24640 }
24641
24642 sep = *s++;
24643 if (sep)
24644 {
24645 /* find end of pattern */
24646 p = vim_strchr(s, sep);
24647 if (p != NULL)
24648 {
24649 pat = vim_strnsave(s, (int)(p - s));
24650 if (pat != NULL)
24651 {
24652 s = p + 1;
24653 /* find end of substitution */
24654 p = vim_strchr(s, sep);
24655 if (p != NULL)
24656 {
24657 sub = vim_strnsave(s, (int)(p - s));
24658 str = vim_strnsave(*fnamep, *fnamelen);
24659 if (sub != NULL && str != NULL)
24660 {
24661 *usedlen = (int)(p + 1 - src);
24662 s = do_string_sub(str, pat, sub, flags);
24663 if (s != NULL)
24664 {
24665 *fnamep = s;
24666 *fnamelen = (int)STRLEN(s);
24667 vim_free(*bufp);
24668 *bufp = s;
24669 didit = TRUE;
24670 }
24671 }
24672 vim_free(sub);
24673 vim_free(str);
24674 }
24675 vim_free(pat);
24676 }
24677 }
24678 /* after using ":s", repeat all the modifiers */
24679 if (didit)
24680 goto repeat;
24681 }
24682 }
24683
Bram Moolenaar26df0922014-02-23 23:39:13 +010024684 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24685 {
24686 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24687 if (p == NULL)
24688 return -1;
24689 vim_free(*bufp);
24690 *bufp = *fnamep = p;
24691 *fnamelen = (int)STRLEN(p);
24692 *usedlen += 2;
24693 }
24694
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 return valid;
24696}
24697
24698/*
24699 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24700 * "flags" can be "g" to do a global substitute.
24701 * Returns an allocated string, NULL for error.
24702 */
24703 char_u *
24704do_string_sub(str, pat, sub, flags)
24705 char_u *str;
24706 char_u *pat;
24707 char_u *sub;
24708 char_u *flags;
24709{
24710 int sublen;
24711 regmatch_T regmatch;
24712 int i;
24713 int do_all;
24714 char_u *tail;
24715 garray_T ga;
24716 char_u *ret;
24717 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024718 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719
24720 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24721 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024722 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723
24724 ga_init2(&ga, 1, 200);
24725
24726 do_all = (flags[0] == 'g');
24727
24728 regmatch.rm_ic = p_ic;
24729 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24730 if (regmatch.regprog != NULL)
24731 {
24732 tail = str;
24733 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24734 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024735 /* Skip empty match except for first match. */
24736 if (regmatch.startp[0] == regmatch.endp[0])
24737 {
24738 if (zero_width == regmatch.startp[0])
24739 {
24740 /* avoid getting stuck on a match with an empty string */
24741 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24742 ++ga.ga_len;
24743 continue;
24744 }
24745 zero_width = regmatch.startp[0];
24746 }
24747
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 /*
24749 * Get some space for a temporary buffer to do the substitution
24750 * into. It will contain:
24751 * - The text up to where the match is.
24752 * - The substituted text.
24753 * - The text after the match.
24754 */
24755 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24756 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24757 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24758 {
24759 ga_clear(&ga);
24760 break;
24761 }
24762
24763 /* copy the text up to where the match is */
24764 i = (int)(regmatch.startp[0] - tail);
24765 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24766 /* add the substituted text */
24767 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24768 + ga.ga_len + i, TRUE, TRUE, FALSE);
24769 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024770 tail = regmatch.endp[0];
24771 if (*tail == NUL)
24772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 if (!do_all)
24774 break;
24775 }
24776
24777 if (ga.ga_data != NULL)
24778 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24779
Bram Moolenaar473de612013-06-08 18:19:48 +020024780 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
24782
24783 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24784 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024785 if (p_cpo == empty_option)
24786 p_cpo = save_cpo;
24787 else
24788 /* Darn, evaluating {sub} expression changed the value. */
24789 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790
24791 return ret;
24792}
24793
24794#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */