blob: 6ba2dd8aa5509b027419eeeed0a7e5e2217253a5 [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 Moolenaar59838522014-05-13 13:46:33 +02004434 EMSG(_("E692: Invalid operation for List"));
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{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006001 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002 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 Moolenaar3ec7f4e2014-05-07 17:31:37 +02006580 * This used to be called list_remove, but that conflicts with a Sun header
6581 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006583 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006584vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *l;
6586 listitem_T *item;
6587 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588{
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 /* notify watchers */
6592 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006594 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 list_fix_watch(l, ip);
6596 if (ip == item2)
6597 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006599
6600 if (item2->li_next == NULL)
6601 l->lv_last = item->li_prev;
6602 else
6603 item2->li_next->li_prev = item->li_prev;
6604 if (item->li_prev == NULL)
6605 l->lv_first = item2->li_next;
6606 else
6607 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609}
6610
6611/*
6612 * Return an allocated string with the string representation of a list.
6613 * May return NULL.
6614 */
6615 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006618 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619{
6620 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621
6622 if (tv->vval.v_list == NULL)
6623 return NULL;
6624 ga_init2(&ga, (int)sizeof(char), 80);
6625 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 {
6628 vim_free(ga.ga_data);
6629 return NULL;
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 ga_append(&ga, ']');
6632 ga_append(&ga, NUL);
6633 return (char_u *)ga.ga_data;
6634}
6635
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006636typedef struct join_S {
6637 char_u *s;
6638 char_u *tofree;
6639} join_T;
6640
6641 static int
6642list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6643 garray_T *gap; /* to store the result in */
6644 list_T *l;
6645 char_u *sep;
6646 int echo_style;
6647 int copyID;
6648 garray_T *join_gap; /* to keep each list item string */
6649{
6650 int i;
6651 join_T *p;
6652 int len;
6653 int sumlen = 0;
6654 int first = TRUE;
6655 char_u *tofree;
6656 char_u numbuf[NUMBUFLEN];
6657 listitem_T *item;
6658 char_u *s;
6659
6660 /* Stringify each item in the list. */
6661 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6662 {
6663 if (echo_style)
6664 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6665 else
6666 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6667 if (s == NULL)
6668 return FAIL;
6669
6670 len = (int)STRLEN(s);
6671 sumlen += len;
6672
6673 ga_grow(join_gap, 1);
6674 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6675 if (tofree != NULL || s != numbuf)
6676 {
6677 p->s = s;
6678 p->tofree = tofree;
6679 }
6680 else
6681 {
6682 p->s = vim_strnsave(s, len);
6683 p->tofree = p->s;
6684 }
6685
6686 line_breakcheck();
6687 }
6688
6689 /* Allocate result buffer with its total size, avoid re-allocation and
6690 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6691 if (join_gap->ga_len >= 2)
6692 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6693 if (ga_grow(gap, sumlen + 2) == FAIL)
6694 return FAIL;
6695
6696 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6697 {
6698 if (first)
6699 first = FALSE;
6700 else
6701 ga_concat(gap, sep);
6702 p = ((join_T *)join_gap->ga_data) + i;
6703
6704 if (p->s != NULL)
6705 ga_concat(gap, p->s);
6706 line_breakcheck();
6707 }
6708
6709 return OK;
6710}
6711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006714 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006717 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006718list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006721 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006722 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006723 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 garray_T join_ga;
6726 int retval;
6727 join_T *p;
6728 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6731 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6732
6733 /* Dispose each item in join_ga. */
6734 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 p = (join_T *)join_ga.ga_data;
6737 for (i = 0; i < join_ga.ga_len; ++i)
6738 {
6739 vim_free(p->tofree);
6740 ++p;
6741 }
6742 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006744
6745 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746}
6747
6748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 * Garbage collection for lists and dictionaries.
6750 *
6751 * We use reference counts to be able to free most items right away when they
6752 * are no longer used. But for composite items it's possible that it becomes
6753 * unused while the reference count is > 0: When there is a recursive
6754 * reference. Example:
6755 * :let l = [1, 2, 3]
6756 * :let d = {9: l}
6757 * :let l[1] = d
6758 *
6759 * Since this is quite unusual we handle this with garbage collection: every
6760 * once in a while find out which lists and dicts are not referenced from any
6761 * variable.
6762 *
6763 * Here is a good reference text about garbage collection (refers to Python
6764 * but it applies to all reference-counting mechanisms):
6765 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767
6768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 * Do garbage collection for lists and dicts.
6770 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006771 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 int
6773garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006775 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 buf_T *buf;
6777 win_T *wp;
6778 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006779 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006780 int did_free;
6781 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 tabpage_T *tp;
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786 /* Only do this once. */
6787 want_garbage_collect = FALSE;
6788 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006789 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006791 /* We advance by two because we add one for items referenced through
6792 * previous_funccal. */
6793 current_copyID += COPYID_INC;
6794 copyID = current_copyID;
6795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /*
6797 * 1. Go through all accessible variables and mark all lists and dicts
6798 * with copyID.
6799 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800
6801 /* Don't free variables in the previous_funccal list unless they are only
6802 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006803 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006804 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6805 {
6806 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6807 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6808 }
6809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 /* script-local variables */
6811 for (i = 1; i <= ga_scripts.ga_len; ++i)
6812 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6813
6814 /* buffer-local variables */
6815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006816 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817
6818 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006820 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006821#ifdef FEAT_AUTOCMD
6822 if (aucmd_win != NULL)
6823 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006829 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830#endif
6831
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 /* global variables */
6833 set_ref_in_ht(&globvarht, copyID);
6834
6835 /* function-local variables */
6836 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6837 {
6838 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6839 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6840 }
6841
Bram Moolenaard812df62008-11-09 12:46:09 +00006842 /* v: vars */
6843 set_ref_in_ht(&vimvarht, copyID);
6844
Bram Moolenaar1dced572012-04-05 16:54:08 +02006845#ifdef FEAT_LUA
6846 set_ref_in_lua(copyID);
6847#endif
6848
Bram Moolenaardb913952012-06-29 12:54:53 +02006849#ifdef FEAT_PYTHON
6850 set_ref_in_python(copyID);
6851#endif
6852
6853#ifdef FEAT_PYTHON3
6854 set_ref_in_python3(copyID);
6855#endif
6856
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006857 /*
6858 * 2. Free lists and dictionaries that are not referenced.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 did_free = free_unref_items(copyID);
6861
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006862 /*
6863 * 3. Check if any funccal can be freed now.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 for (pfc = &previous_funccal; *pfc != NULL; )
6866 {
6867 if (can_free_funccal(*pfc, copyID))
6868 {
6869 fc = *pfc;
6870 *pfc = fc->caller;
6871 free_funccal(fc, TRUE);
6872 did_free = TRUE;
6873 did_free_funccal = TRUE;
6874 }
6875 else
6876 pfc = &(*pfc)->caller;
6877 }
6878 if (did_free_funccal)
6879 /* When a funccal was freed some more items might be garbage
6880 * collected, so run again. */
6881 (void)garbage_collect();
6882
6883 return did_free;
6884}
6885
6886/*
6887 * Free lists and dictionaries that are no longer referenced.
6888 */
6889 static int
6890free_unref_items(copyID)
6891 int copyID;
6892{
6893 dict_T *dd;
6894 list_T *ll;
6895 int did_free = FALSE;
6896
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 */
6900 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006901 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006903 /* Free the Dictionary and ordinary items it contains, but don't
6904 * recurse into Lists and Dictionaries, they will be in the list
6905 * of dicts or list of lists. */
6906 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 did_free = TRUE;
6908
6909 /* restart, next dict may also have been freed */
6910 dd = first_dict;
6911 }
6912 else
6913 dd = dd->dv_used_next;
6914
6915 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006916 * Go through the list of lists and free items without the copyID.
6917 * But don't free a list that has a watcher (used in a for loop), these
6918 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 */
6920 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6922 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006924 /* Free the List and ordinary items it contains, but don't recurse
6925 * into Lists and Dictionaries, they will be in the list of dicts
6926 * or list of lists. */
6927 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 did_free = TRUE;
6929
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006930 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 ll = first_list;
6932 }
6933 else
6934 ll = ll->lv_used_next;
6935
6936 return did_free;
6937}
6938
6939/*
6940 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6941 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006942 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943set_ref_in_ht(ht, copyID)
6944 hashtab_T *ht;
6945 int copyID;
6946{
6947 int todo;
6948 hashitem_T *hi;
6949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 for (hi = ht->ht_array; todo > 0; ++hi)
6952 if (!HASHITEM_EMPTY(hi))
6953 {
6954 --todo;
6955 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6956 }
6957}
6958
6959/*
6960 * Mark all lists and dicts referenced through list "l" with "copyID".
6961 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006962 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963set_ref_in_list(l, copyID)
6964 list_T *l;
6965 int copyID;
6966{
6967 listitem_T *li;
6968
6969 for (li = l->lv_first; li != NULL; li = li->li_next)
6970 set_ref_in_item(&li->li_tv, copyID);
6971}
6972
6973/*
6974 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006976 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977set_ref_in_item(tv, copyID)
6978 typval_T *tv;
6979 int copyID;
6980{
6981 dict_T *dd;
6982 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 switch (tv->v_type)
6985 {
6986 case VAR_DICT:
6987 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006988 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 /* Didn't see this dict yet. */
6991 dd->dv_copyID = copyID;
6992 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995
6996 case VAR_LIST:
6997 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006998 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /* Didn't see this list yet. */
7001 ll->lv_copyID = copyID;
7002 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Allocate an empty header for a dictionary.
7011 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013dict_alloc()
7014{
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007019 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007020 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 if (first_dict != NULL)
7022 first_dict->dv_used_prev = d;
7023 d->dv_used_next = first_dict;
7024 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007025 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007029 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034}
7035
7036/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007037 * Allocate an empty dict for a return value.
7038 * Returns OK or FAIL.
7039 */
7040 static int
7041rettv_dict_alloc(rettv)
7042 typval_T *rettv;
7043{
7044 dict_T *d = dict_alloc();
7045
7046 if (d == NULL)
7047 return FAIL;
7048
7049 rettv->vval.v_dict = d;
7050 rettv->v_type = VAR_DICT;
7051 ++d->dv_refcount;
7052 return OK;
7053}
7054
7055
7056/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057 * Unreference a Dictionary: decrement the reference count and free it when it
7058 * becomes zero.
7059 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007060 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007064 if (d != NULL && --d->dv_refcount <= 0)
7065 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066}
7067
7068/*
7069 * Free a Dictionary, including all items it contains.
7070 * Ignores the reference count.
7071 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007072 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073dict_free(d, recurse)
7074 dict_T *d;
7075 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007079 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /* Remove the dict from the list of dicts for garbage collection. */
7082 if (d->dv_used_prev == NULL)
7083 first_dict = d->dv_used_next;
7084 else
7085 d->dv_used_prev->dv_used_next = d->dv_used_next;
7086 if (d->dv_used_next != NULL)
7087 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7088
7089 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007090 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (!HASHITEM_EMPTY(hi))
7095 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007096 /* Remove the item before deleting it, just in case there is
7097 * something recursive causing trouble. */
7098 di = HI2DI(hi);
7099 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007100 if (recurse || (di->di_tv.v_type != VAR_LIST
7101 && di->di_tv.v_type != VAR_DICT))
7102 clear_tv(&di->di_tv);
7103 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 --todo;
7105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 vim_free(d);
7109}
7110
7111/*
7112 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * The "key" is copied to the new item.
7114 * Note that the value of the item "di_tv" still needs to be initialized!
7115 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007117 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118dictitem_alloc(key)
7119 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120{
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007123 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 di->di_flags = 0;
7128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130}
7131
7132/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 * Make a copy of a Dictionary item.
7134 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138{
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007141 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7142 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 if (di != NULL)
7144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007146 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 copy_tv(&org->di_tv, &di->di_tv);
7148 }
7149 return di;
7150}
7151
7152/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 * Remove item "item" from Dictionary "dict" and free it.
7154 */
7155 static void
7156dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *dict;
7158 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (HASHITEM_EMPTY(hi))
7164 EMSG2(_(e_intern2), "dictitem_remove()");
7165 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 dictitem_free(item);
7168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Free a dict item. Also clears the value.
7172 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007173 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 clear_tv(&item->di_tv);
7178 vim_free(item);
7179}
7180
7181/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7183 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 * Returns NULL when out of memory.
7186 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192{
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 dict_T *copy;
7194 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197
7198 if (orig == NULL)
7199 return NULL;
7200
7201 copy = dict_alloc();
7202 if (copy != NULL)
7203 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 if (copyID != 0)
7205 {
7206 orig->dv_copyID = copyID;
7207 orig->dv_copydict = copy;
7208 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007209 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007210 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 --todo;
7215
7216 di = dictitem_alloc(hi->hi_key);
7217 if (di == NULL)
7218 break;
7219 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 {
7221 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7222 copyID) == FAIL)
7223 {
7224 vim_free(di);
7225 break;
7226 }
7227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007228 else
7229 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7230 if (dict_add(copy, di) == FAIL)
7231 {
7232 dictitem_free(di);
7233 break;
7234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239 if (todo > 0)
7240 {
7241 dict_unref(copy);
7242 copy = NULL;
7243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
7245
7246 return copy;
7247}
7248
7249/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007251 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007253 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 dict_T *d;
7256 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259}
7260
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007262 * Add a number or string entry to dictionary "d".
7263 * When "str" is NULL use number "nr", otherwise use "str".
7264 * Returns FAIL when out of memory and when key already exists.
7265 */
7266 int
7267dict_add_nr_str(d, key, nr, str)
7268 dict_T *d;
7269 char *key;
7270 long nr;
7271 char_u *str;
7272{
7273 dictitem_T *item;
7274
7275 item = dictitem_alloc((char_u *)key);
7276 if (item == NULL)
7277 return FAIL;
7278 item->di_tv.v_lock = 0;
7279 if (str == NULL)
7280 {
7281 item->di_tv.v_type = VAR_NUMBER;
7282 item->di_tv.vval.v_number = nr;
7283 }
7284 else
7285 {
7286 item->di_tv.v_type = VAR_STRING;
7287 item->di_tv.vval.v_string = vim_strsave(str);
7288 }
7289 if (dict_add(d, item) == FAIL)
7290 {
7291 dictitem_free(item);
7292 return FAIL;
7293 }
7294 return OK;
7295}
7296
7297/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007298 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007299 * Returns FAIL when out of memory and when key already exists.
7300 */
7301 int
7302dict_add_list(d, key, list)
7303 dict_T *d;
7304 char *key;
7305 list_T *list;
7306{
7307 dictitem_T *item;
7308
7309 item = dictitem_alloc((char_u *)key);
7310 if (item == NULL)
7311 return FAIL;
7312 item->di_tv.v_lock = 0;
7313 item->di_tv.v_type = VAR_LIST;
7314 item->di_tv.vval.v_list = list;
7315 if (dict_add(d, item) == FAIL)
7316 {
7317 dictitem_free(item);
7318 return FAIL;
7319 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007320 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007321 return OK;
7322}
7323
7324/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 * Get the number of items in a Dictionary.
7326 */
7327 static long
7328dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 if (d == NULL)
7332 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334}
7335
7336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 * Find item "key[len]" in Dictionary "d".
7338 * If "len" is negative use strlen(key).
7339 * Returns NULL when not found.
7340 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007341 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 char_u *key;
7345 int len;
7346{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347#define AKEYLEN 200
7348 char_u buf[AKEYLEN];
7349 char_u *akey;
7350 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (len < 0)
7354 akey = key;
7355 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 tofree = akey = vim_strnsave(key, len);
7358 if (akey == NULL)
7359 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 else
7362 {
7363 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007364 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 akey = buf;
7366 }
7367
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 vim_free(tofree);
7370 if (HASHITEM_EMPTY(hi))
7371 return NULL;
7372 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373}
7374
7375/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007376 * Get a string item from a dictionary.
7377 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007378 * Returns NULL if the entry doesn't exist or out of memory.
7379 */
7380 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007382 dict_T *d;
7383 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007384 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007385{
7386 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007387 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007388
7389 di = dict_find(d, key, -1);
7390 if (di == NULL)
7391 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007392 s = get_tv_string(&di->di_tv);
7393 if (save && s != NULL)
7394 s = vim_strsave(s);
7395 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007396}
7397
7398/*
7399 * Get a number item from a dictionary.
7400 * Returns 0 if the entry doesn't exist or out of memory.
7401 */
7402 long
7403get_dict_number(d, key)
7404 dict_T *d;
7405 char_u *key;
7406{
7407 dictitem_T *di;
7408
7409 di = dict_find(d, key, -1);
7410 if (di == NULL)
7411 return 0;
7412 return get_tv_number(&di->di_tv);
7413}
7414
7415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 * Return an allocated string with the string representation of a Dictionary.
7417 * May return NULL.
7418 */
7419 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007422 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423{
7424 garray_T ga;
7425 int first = TRUE;
7426 char_u *tofree;
7427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 return NULL;
7435 ga_init2(&ga, (int)sizeof(char), 80);
7436 ga_append(&ga, '{');
7437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007439 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007443 --todo;
7444
7445 if (first)
7446 first = FALSE;
7447 else
7448 ga_concat(&ga, (char_u *)", ");
7449
7450 tofree = string_quote(hi->hi_key, FALSE);
7451 if (tofree != NULL)
7452 {
7453 ga_concat(&ga, tofree);
7454 vim_free(tofree);
7455 }
7456 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 if (s != NULL)
7459 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 if (s == NULL)
7462 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007465 if (todo > 0)
7466 {
7467 vim_free(ga.ga_data);
7468 return NULL;
7469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 ga_append(&ga, '}');
7472 ga_append(&ga, NUL);
7473 return (char_u *)ga.ga_data;
7474}
7475
7476/*
7477 * Allocate a variable for a Dictionary and fill it from "*arg".
7478 * Return OK or FAIL. Returns NOTDONE for {expr}.
7479 */
7480 static int
7481get_dict_tv(arg, rettv, evaluate)
7482 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 int evaluate;
7485{
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dict_T *d = NULL;
7487 typval_T tvkey;
7488 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007489 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
7494 /*
7495 * First check if it's not a curly-braces thing: {expr}.
7496 * Must do this without evaluating, otherwise a function may be called
7497 * twice. Unfortunately this means we need to call eval1() twice for the
7498 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 if (*start != '}')
7502 {
7503 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7504 return FAIL;
7505 if (*start == '}')
7506 return NOTDONE;
7507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 if (evaluate)
7510 {
7511 d = dict_alloc();
7512 if (d == NULL)
7513 return FAIL;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 tvkey.v_type = VAR_UNKNOWN;
7516 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517
7518 *arg = skipwhite(*arg + 1);
7519 while (**arg != '}' && **arg != NUL)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 goto failret;
7523 if (**arg != ':')
7524 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007525 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 goto failret;
7528 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007531 key = get_tv_string_buf_chk(&tvkey, buf);
7532 if (key == NULL || *key == NUL)
7533 {
7534 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7535 if (key != NULL)
7536 EMSG(_(e_emptykey));
7537 clear_tv(&tvkey);
7538 goto failret;
7539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541
7542 *arg = skipwhite(*arg + 1);
7543 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7544 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007545 if (evaluate)
7546 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 goto failret;
7548 }
7549 if (evaluate)
7550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 if (item != NULL)
7553 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007554 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 clear_tv(&tv);
7557 goto failret;
7558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 item = dictitem_alloc(key);
7560 clear_tv(&tvkey);
7561 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007564 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 if (dict_add(d, item) == FAIL)
7566 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 }
7568 }
7569
7570 if (**arg == '}')
7571 break;
7572 if (**arg != ',')
7573 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007574 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 goto failret;
7576 }
7577 *arg = skipwhite(*arg + 1);
7578 }
7579
7580 if (**arg != '}')
7581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007582 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583failret:
7584 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007585 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 return FAIL;
7587 }
7588
7589 *arg = skipwhite(*arg + 1);
7590 if (evaluate)
7591 {
7592 rettv->v_type = VAR_DICT;
7593 rettv->vval.v_dict = d;
7594 ++d->dv_refcount;
7595 }
7596
7597 return OK;
7598}
7599
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 * Return a string with the string representation of a variable.
7602 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007603 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007604 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007606 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 */
7608 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007612 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 static int recurse = 0;
7616 char_u *r = NULL;
7617
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007620 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 *tofree = NULL;
7622 return NULL;
7623 }
7624 ++recurse;
7625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 switch (tv->v_type)
7627 {
7628 case VAR_FUNC:
7629 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 r = tv->vval.v_string;
7631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634 if (tv->vval.v_list == NULL)
7635 {
7636 *tofree = NULL;
7637 r = NULL;
7638 }
7639 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7640 {
7641 *tofree = NULL;
7642 r = (char_u *)"[...]";
7643 }
7644 else
7645 {
7646 tv->vval.v_list->lv_copyID = copyID;
7647 *tofree = list2string(tv, copyID);
7648 r = *tofree;
7649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 if (tv->vval.v_dict == NULL)
7654 {
7655 *tofree = NULL;
7656 r = NULL;
7657 }
7658 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7659 {
7660 *tofree = NULL;
7661 r = (char_u *)"{...}";
7662 }
7663 else
7664 {
7665 tv->vval.v_dict->dv_copyID = copyID;
7666 *tofree = dict2string(tv, copyID);
7667 r = *tofree;
7668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 case VAR_STRING:
7672 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 *tofree = NULL;
7674 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007677#ifdef FEAT_FLOAT
7678 case VAR_FLOAT:
7679 *tofree = NULL;
7680 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7681 r = numbuf;
7682 break;
7683#endif
7684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689
7690 --recurse;
7691 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692}
7693
7694/*
7695 * Return a string with the string representation of a variable.
7696 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7697 * "numbuf" is used for a number.
7698 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007699 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 */
7701 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007702tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 char_u **tofree;
7705 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007706 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007707{
7708 switch (tv->v_type)
7709 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 case VAR_FUNC:
7711 *tofree = string_quote(tv->vval.v_string, TRUE);
7712 return *tofree;
7713 case VAR_STRING:
7714 *tofree = string_quote(tv->vval.v_string, FALSE);
7715 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
7717 case VAR_FLOAT:
7718 *tofree = NULL;
7719 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7720 return numbuf;
7721#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007729 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730}
7731
7732/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 * Return string "str" in ' quotes, doubling ' characters.
7734 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 */
7737 static char_u *
7738string_quote(str, function)
7739 char_u *str;
7740 int function;
7741{
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 char_u *p, *r, *s;
7744
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 len = (function ? 13 : 3);
7746 if (str != NULL)
7747 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007748 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 for (p = str; *p != NUL; mb_ptr_adv(p))
7750 if (*p == '\'')
7751 ++len;
7752 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 s = r = alloc(len);
7754 if (r != NULL)
7755 {
7756 if (function)
7757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 r += 10;
7760 }
7761 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 if (str != NULL)
7764 for (p = str; *p != NUL; )
7765 {
7766 if (*p == '\'')
7767 *r++ = '\'';
7768 MB_COPY_CHAR(p, r);
7769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 if (function)
7772 *r++ = ')';
7773 *r++ = NUL;
7774 }
7775 return s;
7776}
7777
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778#ifdef FEAT_FLOAT
7779/*
7780 * Convert the string "text" to a floating point number.
7781 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7782 * this always uses a decimal point.
7783 * Returns the length of the text that was consumed.
7784 */
7785 static int
7786string2float(text, value)
7787 char_u *text;
7788 float_T *value; /* result stored here */
7789{
7790 char *s = (char *)text;
7791 float_T f;
7792
7793 f = strtod(s, &s);
7794 *value = f;
7795 return (int)((char_u *)s - text);
7796}
7797#endif
7798
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 * Get the value of an environment variable.
7801 * "arg" is pointing to the '$'. It is advanced to after the name.
7802 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007803 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 */
7805 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 int evaluate;
7810{
7811 char_u *string = NULL;
7812 int len;
7813 int cc;
7814 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007815 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 ++*arg;
7818 name = *arg;
7819 len = get_env_len(arg);
7820 if (evaluate)
7821 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007822 if (len == 0)
7823 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007825 cc = name[len];
7826 name[len] = NUL;
7827 /* first try vim_getenv(), fast for normal environment vars */
7828 string = vim_getenv(name, &mustfree);
7829 if (string != NULL && *string != NUL)
7830 {
7831 if (!mustfree)
7832 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007834 else
7835 {
7836 if (mustfree)
7837 vim_free(string);
7838
7839 /* next try expanding things like $VIM and ${HOME} */
7840 string = expand_env_save(name - 1);
7841 if (string != NULL && *string == '$')
7842 {
7843 vim_free(string);
7844 string = NULL;
7845 }
7846 }
7847 name[len] = cc;
7848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->v_type = VAR_STRING;
7850 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
7853 return OK;
7854}
7855
7856/*
7857 * Array with names and number of arguments of all internal functions
7858 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7859 */
7860static struct fst
7861{
7862 char *f_name; /* function name */
7863 char f_min_argc; /* minimal number of arguments */
7864 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007866 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867} functions[] =
7868{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#ifdef FEAT_FLOAT
7870 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007871 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007873 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007874 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"append", 2, 2, f_append},
7876 {"argc", 0, 0, f_argc},
7877 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007878 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007885 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"bufexists", 1, 1, f_bufexists},
7887 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7888 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7889 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7890 {"buflisted", 1, 1, f_buflisted},
7891 {"bufloaded", 1, 1, f_bufloaded},
7892 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007893 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"bufwinnr", 1, 1, f_bufwinnr},
7895 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007896 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007897 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
7900 {"ceil", 1, 1, f_ceil},
7901#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007902 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007903 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007905 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007907#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007908 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007909 {"complete_add", 1, 1, f_complete_add},
7910 {"complete_check", 0, 0, f_complete_check},
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#ifdef FEAT_FLOAT
7915 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007916 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007920 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007921 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"delete", 1, 1, f_delete},
7923 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007924 {"diff_filler", 1, 1, f_diff_filler},
7925 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007926 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"eventhandler", 0, 0, f_eventhandler},
7930 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007931 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007933#ifdef FEAT_FLOAT
7934 {"exp", 1, 1, f_exp},
7935#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007936 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007938 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7940 {"filereadable", 1, 1, f_filereadable},
7941 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007943 {"finddir", 1, 3, f_finddir},
7944 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
7946 {"float2nr", 1, 1, f_float2nr},
7947 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007948 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007950 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"fnamemodify", 2, 2, f_fnamemodify},
7952 {"foldclosed", 1, 1, f_foldclosed},
7953 {"foldclosedend", 1, 1, f_foldclosedend},
7954 {"foldlevel", 1, 1, f_foldlevel},
7955 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007956 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007959 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007961 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007962 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"getchar", 0, 1, f_getchar},
7964 {"getcharmod", 0, 0, f_getcharmod},
7965 {"getcmdline", 0, 0, f_getcmdline},
7966 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007967 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007969 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"getfsize", 1, 1, f_getfsize},
7972 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007973 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007974 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007975 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007976 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007977 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007978 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007979 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007980 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007982 {"gettabvar", 2, 3, f_gettabvar},
7983 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"getwinposx", 0, 0, f_getwinposx},
7985 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007986 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007987 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02007988 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007991 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7994 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7995 {"histadd", 2, 2, f_histadd},
7996 {"histdel", 1, 2, f_histdel},
7997 {"histget", 1, 2, f_histget},
7998 {"histnr", 1, 1, f_histnr},
7999 {"hlID", 1, 1, f_hlID},
8000 {"hlexists", 1, 1, f_hlexists},
8001 {"hostname", 0, 0, f_hostname},
8002 {"iconv", 3, 3, f_iconv},
8003 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008005 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008007 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"inputrestore", 0, 0, f_inputrestore},
8009 {"inputsave", 0, 0, f_inputsave},
8010 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008011 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008012 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008014 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"libcall", 3, 3, f_libcall},
8021 {"libcallnr", 3, 3, f_libcallnr},
8022 {"line", 1, 1, f_line},
8023 {"line2byte", 1, 1, f_line2byte},
8024 {"lispindent", 1, 1, f_lispindent},
8025 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008027 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028 {"log10", 1, 1, f_log10},
8029#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008030#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008031 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008032#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008033 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008034 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008035 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008036 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008037 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008038 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008039 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008040 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008041 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008042 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008043 {"max", 1, 1, f_max},
8044 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008045#ifdef vim_mkdir
8046 {"mkdir", 1, 3, f_mkdir},
8047#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008049#ifdef FEAT_MZSCHEME
8050 {"mzeval", 1, 1, f_mzeval},
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008053 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008054 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008055 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008056#ifdef FEAT_FLOAT
8057 {"pow", 2, 2, f_pow},
8058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008060 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008061 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008062#ifdef FEAT_PYTHON3
8063 {"py3eval", 1, 1, f_py3eval},
8064#endif
8065#ifdef FEAT_PYTHON
8066 {"pyeval", 1, 1, f_pyeval},
8067#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008069 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008070 {"reltime", 0, 2, f_reltime},
8071 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"remote_expr", 2, 3, f_remote_expr},
8073 {"remote_foreground", 1, 1, f_remote_foreground},
8074 {"remote_peek", 1, 2, f_remote_peek},
8075 {"remote_read", 1, 1, f_remote_read},
8076 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008079 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008081 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"round", 1, 1, f_round},
8084#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008085 {"screenattr", 2, 2, f_screenattr},
8086 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008087 {"screencol", 0, 0, f_screencol},
8088 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008089 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008090 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008091 {"searchpair", 3, 7, f_searchpair},
8092 {"searchpairpos", 3, 7, f_searchpairpos},
8093 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"server2client", 2, 2, f_server2client},
8095 {"serverlist", 0, 0, f_serverlist},
8096 {"setbufvar", 3, 3, f_setbufvar},
8097 {"setcmdpos", 1, 1, f_setcmdpos},
8098 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008099 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008100 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008101 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008102 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008104 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008105 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008107#ifdef FEAT_CRYPT
8108 {"sha256", 1, 1, f_sha256},
8109#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008110 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008111 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008115 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008117 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008118 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008119 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008120 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008121 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"sqrt", 1, 1, f_sqrt},
8124 {"str2float", 1, 1, f_str2float},
8125#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008126 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008127 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008128 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129#ifdef HAVE_STRFTIME
8130 {"strftime", 1, 2, f_strftime},
8131#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strlen", 1, 1, f_strlen},
8135 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008136 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008138 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008139 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"substitute", 4, 4, f_substitute},
8141 {"synID", 3, 3, f_synID},
8142 {"synIDattr", 2, 3, f_synIDattr},
8143 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008144 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008145 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008146 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008147 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008148 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008149 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008150 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008151 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008152 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153#ifdef FEAT_FLOAT
8154 {"tan", 1, 1, f_tan},
8155 {"tanh", 1, 1, f_tanh},
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008158 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"tolower", 1, 1, f_tolower},
8160 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008161 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#ifdef FEAT_FLOAT
8163 {"trunc", 1, 1, f_trunc},
8164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008166 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008167 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008168 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008169 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"virtcol", 1, 1, f_virtcol},
8171 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008172 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"winbufnr", 1, 1, f_winbufnr},
8174 {"wincol", 0, 0, f_wincol},
8175 {"winheight", 1, 1, f_winheight},
8176 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008177 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008179 {"winrestview", 1, 1, f_winrestview},
8180 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008182 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008183 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184};
8185
8186#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8187
8188/*
8189 * Function given to ExpandGeneric() to obtain the list of internal
8190 * or user defined function names.
8191 */
8192 char_u *
8193get_function_name(xp, idx)
8194 expand_T *xp;
8195 int idx;
8196{
8197 static int intidx = -1;
8198 char_u *name;
8199
8200 if (idx == 0)
8201 intidx = -1;
8202 if (intidx < 0)
8203 {
8204 name = get_user_func_name(xp, idx);
8205 if (name != NULL)
8206 return name;
8207 }
8208 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8209 {
8210 STRCPY(IObuff, functions[intidx].f_name);
8211 STRCAT(IObuff, "(");
8212 if (functions[intidx].f_max_argc == 0)
8213 STRCAT(IObuff, ")");
8214 return IObuff;
8215 }
8216
8217 return NULL;
8218}
8219
8220/*
8221 * Function given to ExpandGeneric() to obtain the list of internal or
8222 * user defined variable or function names.
8223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 char_u *
8225get_expr_name(xp, idx)
8226 expand_T *xp;
8227 int idx;
8228{
8229 static int intidx = -1;
8230 char_u *name;
8231
8232 if (idx == 0)
8233 intidx = -1;
8234 if (intidx < 0)
8235 {
8236 name = get_function_name(xp, idx);
8237 if (name != NULL)
8238 return name;
8239 }
8240 return get_user_var_name(xp, ++intidx);
8241}
8242
8243#endif /* FEAT_CMDL_COMPL */
8244
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008245#if defined(EBCDIC) || defined(PROTO)
8246/*
8247 * Compare struct fst by function name.
8248 */
8249 static int
8250compare_func_name(s1, s2)
8251 const void *s1;
8252 const void *s2;
8253{
8254 struct fst *p1 = (struct fst *)s1;
8255 struct fst *p2 = (struct fst *)s2;
8256
8257 return STRCMP(p1->f_name, p2->f_name);
8258}
8259
8260/*
8261 * Sort the function table by function name.
8262 * The sorting of the table above is ASCII dependant.
8263 * On machines using EBCDIC we have to sort it.
8264 */
8265 static void
8266sortFunctions()
8267{
8268 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269
8270 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8271}
8272#endif
8273
8274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275/*
8276 * Find internal function in table above.
8277 * Return index, or -1 if not found
8278 */
8279 static int
8280find_internal_func(name)
8281 char_u *name; /* name of the function */
8282{
8283 int first = 0;
8284 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8285 int cmp;
8286 int x;
8287
8288 /*
8289 * Find the function name in the table. Binary search.
8290 */
8291 while (first <= last)
8292 {
8293 x = first + ((unsigned)(last - first) >> 1);
8294 cmp = STRCMP(name, functions[x].f_name);
8295 if (cmp < 0)
8296 last = x - 1;
8297 else if (cmp > 0)
8298 first = x + 1;
8299 else
8300 return x;
8301 }
8302 return -1;
8303}
8304
8305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8307 * name it contains, otherwise return "name".
8308 */
8309 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 char_u *name;
8312 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008313 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314{
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 int cc;
8317
8318 cc = name[*lenp];
8319 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008320 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 {
8326 *lenp = 0;
8327 return (char_u *)""; /* just in case */
8328 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008329 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 }
8332
8333 return name;
8334}
8335
8336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 * Allocate a variable for the result of a function.
8338 * Return OK or FAIL.
8339 */
8340 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8342 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u *name; /* name of the function */
8344 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 char_u **arg; /* argument, pointing to the '(' */
8347 linenr_T firstline; /* first line of range */
8348 linenr_T lastline; /* last line of range */
8349 int *doesrange; /* return: function handled range */
8350 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 char_u *argp;
8354 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008355 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 int argcount = 0; /* number of arguments found */
8357
8358 /*
8359 * Get the arguments.
8360 */
8361 argp = *arg;
8362 while (argcount < MAX_FUNC_ARGS)
8363 {
8364 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8365 if (*argp == ')' || *argp == ',' || *argp == NUL)
8366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8368 {
8369 ret = FAIL;
8370 break;
8371 }
8372 ++argcount;
8373 if (*argp != ',')
8374 break;
8375 }
8376 if (*argp == ')')
8377 ++argp;
8378 else
8379 ret = FAIL;
8380
8381 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 {
8386 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008387 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008389 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 *arg = skipwhite(argp);
8396 return ret;
8397}
8398
8399
8400/*
8401 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008402 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008403 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 */
8405 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008406call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008407 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008408 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008412 typval_T *argvars; /* vars for arguments, must have "argcount"
8413 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 linenr_T firstline; /* first line of range */
8415 linenr_T lastline; /* last line of range */
8416 int *doesrange; /* return: function handled range */
8417 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#define ERROR_UNKNOWN 0
8422#define ERROR_TOOMANY 1
8423#define ERROR_TOOFEW 2
8424#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425#define ERROR_DICT 4
8426#define ERROR_NONE 5
8427#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int error = ERROR_NONE;
8429 int i;
8430 int llen;
8431 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#define FLEN_FIXED 40
8433 char_u fname_buf[FLEN_FIXED + 1];
8434 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008435 char_u *name;
8436
8437 /* Make a copy of the name, if it comes from a funcref variable it could
8438 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008439 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008440 if (name == NULL)
8441 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
8443 /*
8444 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8445 * Change <SNR>123_name() to K_SNR 123_name().
8446 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 llen = eval_fname_script(name);
8449 if (llen > 0)
8450 {
8451 fname_buf[0] = K_SPECIAL;
8452 fname_buf[1] = KS_EXTRA;
8453 fname_buf[2] = (int)KE_SNR;
8454 i = 3;
8455 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8456 {
8457 if (current_SID <= 0)
8458 error = ERROR_SCRIPT;
8459 else
8460 {
8461 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8462 i = (int)STRLEN(fname_buf);
8463 }
8464 }
8465 if (i + STRLEN(name + llen) < FLEN_FIXED)
8466 {
8467 STRCPY(fname_buf + i, name + llen);
8468 fname = fname_buf;
8469 }
8470 else
8471 {
8472 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8473 if (fname == NULL)
8474 error = ERROR_OTHER;
8475 else
8476 {
8477 mch_memmove(fname, fname_buf, (size_t)i);
8478 STRCPY(fname + i, name + llen);
8479 }
8480 }
8481 }
8482 else
8483 fname = name;
8484
8485 *doesrange = FALSE;
8486
8487
8488 /* execute the function if no errors detected and executing */
8489 if (evaluate && error == ERROR_NONE)
8490 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008491 char_u *rfname = fname;
8492
8493 /* Ignore "g:" before a function name. */
8494 if (fname[0] == 'g' && fname[1] == ':')
8495 rfname = fname + 2;
8496
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008497 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 error = ERROR_UNKNOWN;
8500
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008501 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {
8503 /*
8504 * User defined function.
8505 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008506 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008509 /* Trigger FuncUndefined event, may load the function. */
8510 if (fp == NULL
8511 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008512 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008513 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008515 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008516 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008519 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008520 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008521 {
8522 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008523 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008524 }
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (fp != NULL)
8527 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008528 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008530 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 else
8537 {
8538 /*
8539 * Call the user function.
8540 * Save and restore search patterns, script variables and
8541 * redo buffer.
8542 */
8543 save_search_patterns();
8544 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008545 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008548 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8549 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8550 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008551 /* Function was unreferenced while being used, free it
8552 * now. */
8553 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 restoreRedobuff();
8555 restore_search_patterns();
8556 error = ERROR_NONE;
8557 }
8558 }
8559 }
8560 else
8561 {
8562 /*
8563 * Find the function name in the table, call its implementation.
8564 */
8565 i = find_internal_func(fname);
8566 if (i >= 0)
8567 {
8568 if (argcount < functions[i].f_min_argc)
8569 error = ERROR_TOOFEW;
8570 else if (argcount > functions[i].f_max_argc)
8571 error = ERROR_TOOMANY;
8572 else
8573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 error = ERROR_NONE;
8577 }
8578 }
8579 }
8580 /*
8581 * The function call (or "FuncUndefined" autocommand sequence) might
8582 * have been aborted by an error, an interrupt, or an explicitly thrown
8583 * exception that has not been caught so far. This situation can be
8584 * tested for by calling aborting(). For an error in an internal
8585 * function or for the "E132" error in call_user_func(), however, the
8586 * throw point at which the "force_abort" flag (temporarily reset by
8587 * emsg()) is normally updated has not been reached yet. We need to
8588 * update that flag first to make aborting() reliable.
8589 */
8590 update_force_abort();
8591 }
8592 if (error == ERROR_NONE)
8593 ret = OK;
8594
8595 /*
8596 * Report an error unless the argument evaluation or function call has been
8597 * cancelled due to an aborting error, an interrupt, or an exception.
8598 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 if (!aborting())
8600 {
8601 switch (error)
8602 {
8603 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008605 break;
8606 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008607 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008608 break;
8609 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008610 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008611 name);
8612 break;
8613 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008614 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008615 name);
8616 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008618 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 name);
8620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008621 }
8622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (fname != name && fname != fname_buf)
8625 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008626 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 return ret;
8629}
8630
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008631/*
8632 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008633 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008634 */
8635 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008636emsg_funcname(ermsg, name)
8637 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008638 char_u *name;
8639{
8640 char_u *p;
8641
8642 if (*name == K_SPECIAL)
8643 p = concat_str((char_u *)"<SNR>", name + 3);
8644 else
8645 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008646 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008647 if (p != name)
8648 vim_free(p);
8649}
8650
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008651/*
8652 * Return TRUE for a non-zero Number and a non-empty String.
8653 */
8654 static int
8655non_zero_arg(argvars)
8656 typval_T *argvars;
8657{
8658 return ((argvars[0].v_type == VAR_NUMBER
8659 && argvars[0].vval.v_number != 0)
8660 || (argvars[0].v_type == VAR_STRING
8661 && argvars[0].vval.v_string != NULL
8662 && *argvars[0].vval.v_string != NUL));
8663}
8664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665/*********************************************
8666 * Implementation of the built-in functions
8667 */
8668
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008669#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8671
8672/*
8673 * Get the float value of "argvars[0]" into "f".
8674 * Returns FAIL when the argument is not a Number or Float.
8675 */
8676 static int
8677get_float_arg(argvars, f)
8678 typval_T *argvars;
8679 float_T *f;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 *f = argvars[0].vval.v_float;
8684 return OK;
8685 }
8686 if (argvars[0].v_type == VAR_NUMBER)
8687 {
8688 *f = (float_T)argvars[0].vval.v_number;
8689 return OK;
8690 }
8691 EMSG(_("E808: Number or Float required"));
8692 return FAIL;
8693}
8694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695/*
8696 * "abs(expr)" function
8697 */
8698 static void
8699f_abs(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 if (argvars[0].v_type == VAR_FLOAT)
8704 {
8705 rettv->v_type = VAR_FLOAT;
8706 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8707 }
8708 else
8709 {
8710 varnumber_T n;
8711 int error = FALSE;
8712
8713 n = get_tv_number_chk(&argvars[0], &error);
8714 if (error)
8715 rettv->vval.v_number = -1;
8716 else if (n > 0)
8717 rettv->vval.v_number = n;
8718 else
8719 rettv->vval.v_number = -n;
8720 }
8721}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008722
8723/*
8724 * "acos()" function
8725 */
8726 static void
8727f_acos(argvars, rettv)
8728 typval_T *argvars;
8729 typval_T *rettv;
8730{
8731 float_T f;
8732
8733 rettv->v_type = VAR_FLOAT;
8734 if (get_float_arg(argvars, &f) == OK)
8735 rettv->vval.v_float = acos(f);
8736 else
8737 rettv->vval.v_float = 0.0;
8738}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008739#endif
8740
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 */
8744 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008754 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008755 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008756 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008758 }
8759 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 EMSG(_(e_listreq));
8761}
8762
8763/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008764 * "and(expr, expr)" function
8765 */
8766 static void
8767f_and(argvars, rettv)
8768 typval_T *argvars;
8769 typval_T *rettv;
8770{
8771 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8772 & get_tv_number_chk(&argvars[1], NULL);
8773}
8774
8775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 * "append(lnum, string/list)" function
8777 */
8778 static void
8779f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 typval_T *argvars;
8781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008782{
8783 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 list_T *l = NULL;
8786 listitem_T *li = NULL;
8787 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 long added = 0;
8789
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008790 /* When coming here from Insert mode, sync undo, so that this can be
8791 * undone separately from what was previously inserted. */
8792 if (u_sync_once == 2)
8793 {
8794 u_sync_once = 1; /* notify that u_sync() was called */
8795 u_sync(TRUE);
8796 }
8797
Bram Moolenaar0d660222005-01-07 21:51:51 +00008798 lnum = get_tv_lnum(argvars);
8799 if (lnum >= 0
8800 && lnum <= curbuf->b_ml.ml_line_count
8801 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805 l = argvars[1].vval.v_list;
8806 if (l == NULL)
8807 return;
8808 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008810 for (;;)
8811 {
8812 if (l == NULL)
8813 tv = &argvars[1]; /* append a string */
8814 else if (li == NULL)
8815 break; /* end of list */
8816 else
8817 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 line = get_tv_string_chk(tv);
8819 if (line == NULL) /* type error */
8820 {
8821 rettv->vval.v_number = 1; /* Failed */
8822 break;
8823 }
8824 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008825 ++added;
8826 if (l == NULL)
8827 break;
8828 li = li->li_next;
8829 }
8830
8831 appended_lines_mark(lnum, added);
8832 if (curwin->w_cursor.lnum > lnum)
8833 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 else
8836 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argc()" function
8841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008844 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
8850/*
8851 * "argidx()" function
8852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "argv(nr)" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 int idx;
8870
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008871 if (argvars[0].v_type != VAR_UNKNOWN)
8872 {
8873 idx = get_tv_number_chk(&argvars[0], NULL);
8874 if (idx >= 0 && idx < ARGCOUNT)
8875 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8876 else
8877 rettv->vval.v_string = NULL;
8878 rettv->v_type = VAR_STRING;
8879 }
8880 else if (rettv_list_alloc(rettv) == OK)
8881 for (idx = 0; idx < ARGCOUNT; ++idx)
8882 list_append_string(rettv->vval.v_list,
8883 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008888 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008890 static void
8891f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008892 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008893 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008895 float_T f;
8896
8897 rettv->v_type = VAR_FLOAT;
8898 if (get_float_arg(argvars, &f) == OK)
8899 rettv->vval.v_float = asin(f);
8900 else
8901 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008902}
8903
8904/*
8905 * "atan()" function
8906 */
8907 static void
8908f_atan(argvars, rettv)
8909 typval_T *argvars;
8910 typval_T *rettv;
8911{
8912 float_T f;
8913
8914 rettv->v_type = VAR_FLOAT;
8915 if (get_float_arg(argvars, &f) == OK)
8916 rettv->vval.v_float = atan(f);
8917 else
8918 rettv->vval.v_float = 0.0;
8919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920
8921/*
8922 * "atan2()" function
8923 */
8924 static void
8925f_atan2(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 float_T fx, fy;
8930
8931 rettv->v_type = VAR_FLOAT;
8932 if (get_float_arg(argvars, &fx) == OK
8933 && get_float_arg(&argvars[1], &fy) == OK)
8934 rettv->vval.v_float = atan2(fx, fy);
8935 else
8936 rettv->vval.v_float = 0.0;
8937}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#endif
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
8941 * "browse(save, title, initdir, default)" function
8942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008945 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948#ifdef FEAT_BROWSE
8949 int save;
8950 char_u *title;
8951 char_u *initdir;
8952 char_u *defname;
8953 char_u buf[NUMBUFLEN];
8954 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 save = get_tv_number_chk(&argvars[0], &error);
8958 title = get_tv_string_chk(&argvars[1]);
8959 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8960 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 if (error || title == NULL || initdir == NULL || defname == NULL)
8963 rettv->vval.v_string = NULL;
8964 else
8965 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966 do_browse(save ? BROWSE_SAVE : 0,
8967 title, defname, NULL, initdir, NULL, curbuf);
8968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972}
8973
8974/*
8975 * "browsedir(title, initdir)" function
8976 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008981{
8982#ifdef FEAT_BROWSE
8983 char_u *title;
8984 char_u *initdir;
8985 char_u buf[NUMBUFLEN];
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 title = get_tv_string_chk(&argvars[0]);
8988 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (title == NULL || initdir == NULL)
8991 rettv->vval.v_string = NULL;
8992 else
8993 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008994 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
Bram Moolenaar33570922005-01-25 22:26:29 +00009001static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*
9004 * Find a buffer by number or exact name.
9005 */
9006 static buf_T *
9007find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 if (avar->v_type == VAR_NUMBER)
9013 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009014 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009017 if (buf == NULL)
9018 {
9019 /* No full path name match, try a match with a URL or a "nofile"
9020 * buffer, these don't use the full path. */
9021 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9022 if (buf->b_fname != NULL
9023 && (path_with_url(buf->b_fname)
9024#ifdef FEAT_QUICKFIX
9025 || bt_nofile(buf)
9026#endif
9027 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009029 break;
9030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 return buf;
9033}
9034
9035/*
9036 * "bufexists(expr)" function
9037 */
9038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "buflisted(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
9056 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * "bufloaded(expr)" function
9062 */
9063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *argvars;
9066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 buf_T *buf;
9069
9070 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009074static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*
9077 * Get buffer by number or pattern.
9078 */
9079 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009080get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009082 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 int save_magic;
9086 char_u *save_cpo;
9087 buf_T *buf;
9088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 if (tv->v_type == VAR_NUMBER)
9090 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009091 if (tv->v_type != VAR_STRING)
9092 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 if (name == NULL || *name == NUL)
9094 return curbuf;
9095 if (name[0] == '$' && name[1] == NUL)
9096 return lastbuf;
9097
9098 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9099 save_magic = p_magic;
9100 p_magic = TRUE;
9101 save_cpo = p_cpo;
9102 p_cpo = (char_u *)"";
9103
9104 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009105 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106
9107 p_magic = save_magic;
9108 p_cpo = save_cpo;
9109
9110 /* If not found, try expanding the name, like done for bufexists(). */
9111 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 return buf;
9115}
9116
9117/*
9118 * "bufname(expr)" function
9119 */
9120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T *argvars;
9123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 buf_T *buf;
9126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009129 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 --emsg_off;
9136}
9137
9138/*
9139 * "bufnr(expr)" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009147 int error = FALSE;
9148 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009152 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009153 --emsg_off;
9154
9155 /* If the buffer isn't found and the second argument is not zero create a
9156 * new buffer. */
9157 if (buf == NULL
9158 && argvars[1].v_type != VAR_UNKNOWN
9159 && get_tv_number_chk(&argvars[1], &error) != 0
9160 && !error
9161 && (name = get_tv_string_chk(&argvars[0])) != NULL
9162 && !error)
9163 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171/*
9172 * "bufwinnr(nr)" function
9173 */
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179#ifdef FEAT_WINDOWS
9180 win_T *wp;
9181 int winnr = 0;
9182#endif
9183 buf_T *buf;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009187 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_WINDOWS
9189 for (wp = firstwin; wp; wp = wp->w_next)
9190 {
9191 ++winnr;
9192 if (wp->w_buffer == buf)
9193 break;
9194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#endif
9199 --emsg_off;
9200}
9201
9202/*
9203 * "byte2line(byte)" function
9204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#else
9213 long boff = 0;
9214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 (linenr_T)0, &boff);
9221#endif
9222}
9223
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009224 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009225byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009228 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009229{
9230#ifdef FEAT_MBYTE
9231 char_u *t;
9232#endif
9233 char_u *str;
9234 long idx;
9235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 str = get_tv_string_chk(&argvars[0]);
9237 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240 return;
9241
9242#ifdef FEAT_MBYTE
9243 t = str;
9244 for ( ; idx > 0; idx--)
9245 {
9246 if (*t == NUL) /* EOL reached */
9247 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009248 if (enc_utf8 && comp)
9249 t += utf_ptr2len(t);
9250 else
9251 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009252 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009253 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009255 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009257#endif
9258}
9259
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009260/*
9261 * "byteidx()" function
9262 */
9263 static void
9264f_byteidx(argvars, rettv)
9265 typval_T *argvars;
9266 typval_T *rettv;
9267{
9268 byteidx(argvars, rettv, FALSE);
9269}
9270
9271/*
9272 * "byteidxcomp()" function
9273 */
9274 static void
9275f_byteidxcomp(argvars, rettv)
9276 typval_T *argvars;
9277 typval_T *rettv;
9278{
9279 byteidx(argvars, rettv, TRUE);
9280}
9281
Bram Moolenaardb913952012-06-29 12:54:53 +02009282 int
9283func_call(name, args, selfdict, rettv)
9284 char_u *name;
9285 typval_T *args;
9286 dict_T *selfdict;
9287 typval_T *rettv;
9288{
9289 listitem_T *item;
9290 typval_T argv[MAX_FUNC_ARGS + 1];
9291 int argc = 0;
9292 int dummy;
9293 int r = 0;
9294
9295 for (item = args->vval.v_list->lv_first; item != NULL;
9296 item = item->li_next)
9297 {
9298 if (argc == MAX_FUNC_ARGS)
9299 {
9300 EMSG(_("E699: Too many arguments"));
9301 break;
9302 }
9303 /* Make a copy of each argument. This is needed to be able to set
9304 * v_lock to VAR_FIXED in the copy without changing the original list.
9305 */
9306 copy_tv(&item->li_tv, &argv[argc++]);
9307 }
9308
9309 if (item == NULL)
9310 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9311 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9312 &dummy, TRUE, selfdict);
9313
9314 /* Free the arguments. */
9315 while (argc > 0)
9316 clear_tv(&argv[--argc]);
9317
9318 return r;
9319}
9320
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322 * "call(func, arglist)" function
9323 */
9324 static void
9325f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328{
9329 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332 if (argvars[1].v_type != VAR_LIST)
9333 {
9334 EMSG(_(e_listreq));
9335 return;
9336 }
9337 if (argvars[1].vval.v_list == NULL)
9338 return;
9339
9340 if (argvars[0].v_type == VAR_FUNC)
9341 func = argvars[0].vval.v_string;
9342 else
9343 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 if (*func == NUL)
9345 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009346
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 if (argvars[2].v_type != VAR_UNKNOWN)
9348 {
9349 if (argvars[2].v_type != VAR_DICT)
9350 {
9351 EMSG(_(e_dictreq));
9352 return;
9353 }
9354 selfdict = argvars[2].vval.v_dict;
9355 }
9356
Bram Moolenaardb913952012-06-29 12:54:53 +02009357 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009358}
9359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#ifdef FEAT_FLOAT
9361/*
9362 * "ceil({float})" function
9363 */
9364 static void
9365f_ceil(argvars, rettv)
9366 typval_T *argvars;
9367 typval_T *rettv;
9368{
9369 float_T f;
9370
9371 rettv->v_type = VAR_FLOAT;
9372 if (get_float_arg(argvars, &f) == OK)
9373 rettv->vval.v_float = ceil(f);
9374 else
9375 rettv->vval.v_float = 0.0;
9376}
9377#endif
9378
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009379/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009380 * "changenr()" function
9381 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009382 static void
9383f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009384 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009385 typval_T *rettv;
9386{
9387 rettv->vval.v_number = curbuf->b_u_seq_cur;
9388}
9389
9390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 * "char2nr(string)" function
9392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398#ifdef FEAT_MBYTE
9399 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009400 {
9401 int utf8 = 0;
9402
9403 if (argvars[1].v_type != VAR_UNKNOWN)
9404 utf8 = get_tv_number_chk(&argvars[1], NULL);
9405
9406 if (utf8)
9407 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9408 else
9409 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
9412#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415
9416/*
9417 * "cindent(lnum)" function
9418 */
9419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#ifdef FEAT_CINDENT
9425 pos_T pos;
9426 linenr_T lnum;
9427
9428 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9431 {
9432 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 curwin->w_cursor = pos;
9435 }
9436 else
9437#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009442 * "clearmatches()" function
9443 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009444 static void
9445f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009446 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009448{
9449#ifdef FEAT_SEARCH_EXTRA
9450 clear_matches(curwin);
9451#endif
9452}
9453
9454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 * "col(string)" function
9456 */
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 colnr_T col = 0;
9463 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009464 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009466 fp = var2fpos(&argvars[0], FALSE, &fnum);
9467 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
9469 if (fp->col == MAXCOL)
9470 {
9471 /* '> can be MAXCOL, get the length of the line then */
9472 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009473 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 else
9475 col = MAXCOL;
9476 }
9477 else
9478 {
9479 col = fp->col + 1;
9480#ifdef FEAT_VIRTUALEDIT
9481 /* col(".") when the cursor is on the NUL at the end of the line
9482 * because of "coladd" can be seen as an extra column. */
9483 if (virtual_active() && fp == &curwin->w_cursor)
9484 {
9485 char_u *p = ml_get_cursor();
9486
9487 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9488 curwin->w_virtcol - curwin->w_cursor.coladd))
9489 {
9490# ifdef FEAT_MBYTE
9491 int l;
9492
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009493 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 col += l;
9495# else
9496 if (*p != NUL && p[1] == NUL)
9497 ++col;
9498# endif
9499 }
9500 }
9501#endif
9502 }
9503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar572cb562005-08-05 21:35:02 +00009507#if defined(FEAT_INS_EXPAND)
9508/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009509 * "complete()" function
9510 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009511 static void
9512f_complete(argvars, rettv)
9513 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009514 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009515{
9516 int startcol;
9517
9518 if ((State & INSERT) == 0)
9519 {
9520 EMSG(_("E785: complete() can only be used in Insert mode"));
9521 return;
9522 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009523
9524 /* Check for undo allowed here, because if something was already inserted
9525 * the line was already saved for undo and this check isn't done. */
9526 if (!undo_allowed())
9527 return;
9528
Bram Moolenaarade00832006-03-10 21:46:58 +00009529 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9530 {
9531 EMSG(_(e_invarg));
9532 return;
9533 }
9534
9535 startcol = get_tv_number_chk(&argvars[0], NULL);
9536 if (startcol <= 0)
9537 return;
9538
9539 set_completion(startcol - 1, argvars[1].vval.v_list);
9540}
9541
9542/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 * "complete_add()" function
9544 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009545 static void
9546f_complete_add(argvars, rettv)
9547 typval_T *argvars;
9548 typval_T *rettv;
9549{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009550 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009551}
9552
9553/*
9554 * "complete_check()" function
9555 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009556 static void
9557f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009558 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009559 typval_T *rettv;
9560{
9561 int saved = RedrawingDisabled;
9562
9563 RedrawingDisabled = 0;
9564 ins_compl_check_keys(0);
9565 rettv->vval.v_number = compl_interrupted;
9566 RedrawingDisabled = saved;
9567}
9568#endif
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570/*
9571 * "confirm(message, buttons[, default [, type]])" function
9572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009575 typval_T *argvars UNUSED;
9576 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9579 char_u *message;
9580 char_u *buttons = NULL;
9581 char_u buf[NUMBUFLEN];
9582 char_u buf2[NUMBUFLEN];
9583 int def = 1;
9584 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 char_u *typestr;
9586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 message = get_tv_string_chk(&argvars[0]);
9589 if (message == NULL)
9590 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9594 if (buttons == NULL)
9595 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9602 if (typestr == NULL)
9603 error = TRUE;
9604 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 switch (TOUPPER_ASC(*typestr))
9607 {
9608 case 'E': type = VIM_ERROR; break;
9609 case 'Q': type = VIM_QUESTION; break;
9610 case 'I': type = VIM_INFO; break;
9611 case 'W': type = VIM_WARNING; break;
9612 case 'G': type = VIM_GENERIC; break;
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615 }
9616 }
9617 }
9618
9619 if (buttons == NULL || *buttons == NUL)
9620 buttons = (char_u *)_("&Ok");
9621
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009622 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009624 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626}
9627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628/*
9629 * "copy()" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 typval_T *argvars;
9634 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009636 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009639#ifdef FEAT_FLOAT
9640/*
9641 * "cos()" function
9642 */
9643 static void
9644f_cos(argvars, rettv)
9645 typval_T *argvars;
9646 typval_T *rettv;
9647{
9648 float_T f;
9649
9650 rettv->v_type = VAR_FLOAT;
9651 if (get_float_arg(argvars, &f) == OK)
9652 rettv->vval.v_float = cos(f);
9653 else
9654 rettv->vval.v_float = 0.0;
9655}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009656
9657/*
9658 * "cosh()" function
9659 */
9660 static void
9661f_cosh(argvars, rettv)
9662 typval_T *argvars;
9663 typval_T *rettv;
9664{
9665 float_T f;
9666
9667 rettv->v_type = VAR_FLOAT;
9668 if (get_float_arg(argvars, &f) == OK)
9669 rettv->vval.v_float = cosh(f);
9670 else
9671 rettv->vval.v_float = 0.0;
9672}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009673#endif
9674
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009676 * "count()" function
9677 */
9678 static void
9679f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009682{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009683 long n = 0;
9684 int ic = FALSE;
9685
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009688 listitem_T *li;
9689 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692 if ((l = argvars[0].vval.v_list) != NULL)
9693 {
9694 li = l->lv_first;
9695 if (argvars[2].v_type != VAR_UNKNOWN)
9696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 int error = FALSE;
9698
9699 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 if (argvars[3].v_type != VAR_UNKNOWN)
9701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 idx = get_tv_number_chk(&argvars[3], &error);
9703 if (!error)
9704 {
9705 li = list_find(l, idx);
9706 if (li == NULL)
9707 EMSGN(_(e_listidx), idx);
9708 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 if (error)
9711 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009712 }
9713
9714 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009715 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 ++n;
9717 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009718 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009719 else if (argvars[0].v_type == VAR_DICT)
9720 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 int todo;
9722 dict_T *d;
9723 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009725 if ((d = argvars[0].vval.v_dict) != NULL)
9726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 int error = FALSE;
9728
Bram Moolenaare9a41262005-01-15 22:18:47 +00009729 if (argvars[2].v_type != VAR_UNKNOWN)
9730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 if (argvars[3].v_type != VAR_UNKNOWN)
9733 EMSG(_(e_invarg));
9734 }
9735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009736 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009738 {
9739 if (!HASHITEM_EMPTY(hi))
9740 {
9741 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009742 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009743 ++n;
9744 }
9745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 }
9747 }
9748 else
9749 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009750 rettv->vval.v_number = n;
9751}
9752
9753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9755 *
9756 * Checks the existence of a cscope connection.
9757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009760 typval_T *argvars UNUSED;
9761 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
9763#ifdef FEAT_CSCOPE
9764 int num = 0;
9765 char_u *dbpath = NULL;
9766 char_u *prepend = NULL;
9767 char_u buf[NUMBUFLEN];
9768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009769 if (argvars[0].v_type != VAR_UNKNOWN
9770 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 num = (int)get_tv_number(&argvars[0]);
9773 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 }
9777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#endif
9780}
9781
9782/*
9783 * "cursor(lnum, col)" function
9784 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009785 * Moves the cursor to the specified line and column.
9786 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 typval_T *argvars;
9791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009794#ifdef FEAT_VIRTUALEDIT
9795 long coladd = 0;
9796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009798 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009799 if (argvars[1].v_type == VAR_UNKNOWN)
9800 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009801 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009802
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009803 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009804 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009805 line = pos.lnum;
9806 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009807#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009808 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009809#endif
9810 }
9811 else
9812 {
9813 line = get_tv_lnum(argvars);
9814 col = get_tv_number_chk(&argvars[1], NULL);
9815#ifdef FEAT_VIRTUALEDIT
9816 if (argvars[2].v_type != VAR_UNKNOWN)
9817 coladd = get_tv_number_chk(&argvars[2], NULL);
9818#endif
9819 }
9820 if (line < 0 || col < 0
9821#ifdef FEAT_VIRTUALEDIT
9822 || coladd < 0
9823#endif
9824 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (line > 0)
9827 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (col > 0)
9829 curwin->w_cursor.col = col - 1;
9830#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009831 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#endif
9833
9834 /* Make sure the cursor is in a valid position. */
9835 check_cursor();
9836#ifdef FEAT_MBYTE
9837 /* Correct cursor for multi-byte character. */
9838 if (has_mbyte)
9839 mb_adjust_cursor();
9840#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009841
9842 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009843 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844}
9845
9846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009847 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 */
9849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009851 typval_T *argvars;
9852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009854 int noref = 0;
9855
9856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009858 if (noref < 0 || noref > 1)
9859 EMSG(_(e_invarg));
9860 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009861 {
9862 current_copyID += COPYID_INC;
9863 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * "delete()" function
9869 */
9870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009872 typval_T *argvars;
9873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879}
9880
9881/*
9882 * "did_filetype()" function
9883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009886 typval_T *argvars UNUSED;
9887 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#endif
9892}
9893
9894/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 * "diff_filler()" function
9896 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009899 typval_T *argvars UNUSED;
9900 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901{
9902#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009904#endif
9905}
9906
9907/*
9908 * "diff_hlID()" function
9909 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009912 typval_T *argvars UNUSED;
9913 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914{
9915#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009917 static linenr_T prev_lnum = 0;
9918 static int changedtick = 0;
9919 static int fnum = 0;
9920 static int change_start = 0;
9921 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009922 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009923 int filler_lines;
9924 int col;
9925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 if (lnum < 0) /* ignore type error in {lnum} arg */
9927 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009928 if (lnum != prev_lnum
9929 || changedtick != curbuf->b_changedtick
9930 || fnum != curbuf->b_fnum)
9931 {
9932 /* New line, buffer, change: need to get the values. */
9933 filler_lines = diff_check(curwin, lnum);
9934 if (filler_lines < 0)
9935 {
9936 if (filler_lines == -1)
9937 {
9938 change_start = MAXCOL;
9939 change_end = -1;
9940 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9941 hlID = HLF_ADD; /* added line */
9942 else
9943 hlID = HLF_CHD; /* changed line */
9944 }
9945 else
9946 hlID = HLF_ADD; /* added line */
9947 }
9948 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009949 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009950 prev_lnum = lnum;
9951 changedtick = curbuf->b_changedtick;
9952 fnum = curbuf->b_fnum;
9953 }
9954
9955 if (hlID == HLF_CHD || hlID == HLF_TXD)
9956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009958 if (col >= change_start && col <= change_end)
9959 hlID = HLF_TXD; /* changed text */
9960 else
9961 hlID = HLF_CHD; /* changed line */
9962 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009963 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009964#endif
9965}
9966
9967/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009968 * "empty({expr})" function
9969 */
9970 static void
9971f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 typval_T *argvars;
9973 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009974{
9975 int n;
9976
9977 switch (argvars[0].v_type)
9978 {
9979 case VAR_STRING:
9980 case VAR_FUNC:
9981 n = argvars[0].vval.v_string == NULL
9982 || *argvars[0].vval.v_string == NUL;
9983 break;
9984 case VAR_NUMBER:
9985 n = argvars[0].vval.v_number == 0;
9986 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009987#ifdef FEAT_FLOAT
9988 case VAR_FLOAT:
9989 n = argvars[0].vval.v_float == 0.0;
9990 break;
9991#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009992 case VAR_LIST:
9993 n = argvars[0].vval.v_list == NULL
9994 || argvars[0].vval.v_list->lv_first == NULL;
9995 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 case VAR_DICT:
9997 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010000 default:
10001 EMSG2(_(e_intern2), "f_empty()");
10002 n = 0;
10003 }
10004
10005 rettv->vval.v_number = n;
10006}
10007
10008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 * "escape({string}, {chars})" function
10010 */
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 char_u buf[NUMBUFLEN];
10017
Bram Moolenaar758711c2005-02-02 23:11:38 +000010018 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10019 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024 * "eval()" function
10025 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010026 static void
10027f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010030{
10031 char_u *s;
10032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 s = get_tv_string_chk(&argvars[0]);
10034 if (s != NULL)
10035 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010037 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10038 {
10039 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010040 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010042 else if (*s != NUL)
10043 EMSG(_(e_trailing));
10044}
10045
10046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 * "eventhandler()" function
10048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057/*
10058 * "executable()" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010065 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10066}
10067
10068/*
10069 * "exepath()" function
10070 */
10071 static void
10072f_exepath(argvars, rettv)
10073 typval_T *argvars;
10074 typval_T *rettv;
10075{
10076 char_u *p = NULL;
10077
10078 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10079 rettv->v_type = VAR_STRING;
10080 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081}
10082
10083/*
10084 * "exists()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 char_u *p;
10092 char_u *name;
10093 int n = FALSE;
10094 int len = 0;
10095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 if (*p == '$') /* environment variable */
10098 {
10099 /* first try "normal" environment variables (fast) */
10100 if (mch_getenv(p + 1) != NULL)
10101 n = TRUE;
10102 else
10103 {
10104 /* try expanding things like $VIM and ${HOME} */
10105 p = expand_env_save(p);
10106 if (p != NULL && *p != '$')
10107 n = TRUE;
10108 vim_free(p);
10109 }
10110 }
10111 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010114 if (*skipwhite(p) != NUL)
10115 n = FALSE; /* trailing garbage */
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 else if (*p == '*') /* internal or user defined function */
10118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 }
10121 else if (*p == ':')
10122 {
10123 n = cmd_exists(p + 1);
10124 }
10125 else if (*p == '#')
10126 {
10127#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010128 if (p[1] == '#')
10129 n = autocmd_supported(p + 2);
10130 else
10131 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133 }
10134 else /* internal variable */
10135 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010136 char_u *tofree;
10137 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010139 /* get_name_len() takes care of expanding curly braces */
10140 name = p;
10141 len = get_name_len(&p, &tofree, TRUE, FALSE);
10142 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010144 if (tofree != NULL)
10145 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010146 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010147 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010149 /* handle d.key, l[idx], f(expr) */
10150 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10151 if (n)
10152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
10154 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010155 if (*p != NUL)
10156 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010158 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 }
10160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162}
10163
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010164#ifdef FEAT_FLOAT
10165/*
10166 * "exp()" function
10167 */
10168 static void
10169f_exp(argvars, rettv)
10170 typval_T *argvars;
10171 typval_T *rettv;
10172{
10173 float_T f;
10174
10175 rettv->v_type = VAR_FLOAT;
10176 if (get_float_arg(argvars, &f) == OK)
10177 rettv->vval.v_float = exp(f);
10178 else
10179 rettv->vval.v_float = 0.0;
10180}
10181#endif
10182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183/*
10184 * "expand()" function
10185 */
10186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 typval_T *argvars;
10189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 char_u *s;
10192 int len;
10193 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010194 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010197 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010200 if (argvars[1].v_type != VAR_UNKNOWN
10201 && argvars[2].v_type != VAR_UNKNOWN
10202 && get_tv_number_chk(&argvars[2], &error)
10203 && !error)
10204 {
10205 rettv->v_type = VAR_LIST;
10206 rettv->vval.v_list = NULL;
10207 }
10208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 if (*s == '%' || *s == '#' || *s == '<')
10211 {
10212 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010213 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010215 if (rettv->v_type == VAR_LIST)
10216 {
10217 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10218 list_append_string(rettv->vval.v_list, result, -1);
10219 else
10220 vim_free(result);
10221 }
10222 else
10223 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 }
10225 else
10226 {
10227 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010228 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 if (argvars[1].v_type != VAR_UNKNOWN
10230 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010231 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 if (!error)
10233 {
10234 ExpandInit(&xpc);
10235 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010236 if (p_wic)
10237 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010238 if (rettv->v_type == VAR_STRING)
10239 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10240 options, WILD_ALL);
10241 else if (rettv_list_alloc(rettv) != FAIL)
10242 {
10243 int i;
10244
10245 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10246 for (i = 0; i < xpc.xp_numfiles; i++)
10247 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10248 ExpandCleanup(&xpc);
10249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 }
10251 else
10252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 }
10254}
10255
10256/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010257 * Go over all entries in "d2" and add them to "d1".
10258 * When "action" is "error" then a duplicate key is an error.
10259 * When "action" is "force" then a duplicate key is overwritten.
10260 * Otherwise duplicate keys are ignored ("action" is "keep").
10261 */
10262 void
10263dict_extend(d1, d2, action)
10264 dict_T *d1;
10265 dict_T *d2;
10266 char_u *action;
10267{
10268 dictitem_T *di1;
10269 hashitem_T *hi2;
10270 int todo;
10271
10272 todo = (int)d2->dv_hashtab.ht_used;
10273 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10274 {
10275 if (!HASHITEM_EMPTY(hi2))
10276 {
10277 --todo;
10278 di1 = dict_find(d1, hi2->hi_key, -1);
10279 if (d1->dv_scope != 0)
10280 {
10281 /* Disallow replacing a builtin function in l: and g:.
10282 * Check the key to be valid when adding to any
10283 * scope. */
10284 if (d1->dv_scope == VAR_DEF_SCOPE
10285 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10286 && var_check_func_name(hi2->hi_key,
10287 di1 == NULL))
10288 break;
10289 if (!valid_varname(hi2->hi_key))
10290 break;
10291 }
10292 if (di1 == NULL)
10293 {
10294 di1 = dictitem_copy(HI2DI(hi2));
10295 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10296 dictitem_free(di1);
10297 }
10298 else if (*action == 'e')
10299 {
10300 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10301 break;
10302 }
10303 else if (*action == 'f' && HI2DI(hi2) != di1)
10304 {
10305 clear_tv(&di1->di_tv);
10306 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10307 }
10308 }
10309 }
10310}
10311
10312/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010313 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315 */
10316 static void
10317f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 typval_T *argvars;
10319 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010321 char *arg_errmsg = N_("extend() argument");
10322
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010324 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 list_T *l1, *l2;
10326 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010329
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 l1 = argvars[0].vval.v_list;
10331 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010332 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010333 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010334 {
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 before = get_tv_number_chk(&argvars[2], &error);
10338 if (error)
10339 return; /* type error; errmsg already given */
10340
Bram Moolenaar758711c2005-02-02 23:11:38 +000010341 if (before == l1->lv_len)
10342 item = NULL;
10343 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010345 item = list_find(l1, before);
10346 if (item == NULL)
10347 {
10348 EMSGN(_(e_listidx), before);
10349 return;
10350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 }
10352 }
10353 else
10354 item = NULL;
10355 list_extend(l1, l2, item);
10356
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357 copy_tv(&argvars[0], rettv);
10358 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10361 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010362 dict_T *d1, *d2;
10363 char_u *action;
10364 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365
10366 d1 = argvars[0].vval.v_dict;
10367 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010368 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010369 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010370 {
10371 /* Check the third argument. */
10372 if (argvars[2].v_type != VAR_UNKNOWN)
10373 {
10374 static char *(av[]) = {"keep", "force", "error"};
10375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 action = get_tv_string_chk(&argvars[2]);
10377 if (action == NULL)
10378 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 for (i = 0; i < 3; ++i)
10380 if (STRCMP(action, av[i]) == 0)
10381 break;
10382 if (i == 3)
10383 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010384 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 return;
10386 }
10387 }
10388 else
10389 action = (char_u *)"force";
10390
Bram Moolenaara9922d62013-05-30 13:01:18 +020010391 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392
Bram Moolenaare9a41262005-01-15 22:18:47 +000010393 copy_tv(&argvars[0], rettv);
10394 }
10395 }
10396 else
10397 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010398}
10399
10400/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010401 * "feedkeys()" function
10402 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010403 static void
10404f_feedkeys(argvars, rettv)
10405 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010406 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010407{
10408 int remap = TRUE;
10409 char_u *keys, *flags;
10410 char_u nbuf[NUMBUFLEN];
10411 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010412 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010413
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010414 /* This is not allowed in the sandbox. If the commands would still be
10415 * executed in the sandbox it would be OK, but it probably happens later,
10416 * when "sandbox" is no longer set. */
10417 if (check_secure())
10418 return;
10419
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010420 keys = get_tv_string(&argvars[0]);
10421 if (*keys != NUL)
10422 {
10423 if (argvars[1].v_type != VAR_UNKNOWN)
10424 {
10425 flags = get_tv_string_buf(&argvars[1], nbuf);
10426 for ( ; *flags != NUL; ++flags)
10427 {
10428 switch (*flags)
10429 {
10430 case 'n': remap = FALSE; break;
10431 case 'm': remap = TRUE; break;
10432 case 't': typed = TRUE; break;
10433 }
10434 }
10435 }
10436
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010437 /* Need to escape K_SPECIAL and CSI before putting the string in the
10438 * typeahead buffer. */
10439 keys_esc = vim_strsave_escape_csi(keys);
10440 if (keys_esc != NULL)
10441 {
10442 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010443 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010444 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010445 if (vgetc_busy)
10446 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010447 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010448 }
10449}
10450
10451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 * "filereadable()" function
10453 */
10454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010456 typval_T *argvars;
10457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010459 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 char_u *p;
10461 int n;
10462
Bram Moolenaarc236c162008-07-13 17:41:49 +000010463#ifndef O_NONBLOCK
10464# define O_NONBLOCK 0
10465#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010467 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10468 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
10470 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010471 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 }
10473 else
10474 n = FALSE;
10475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
10479/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010480 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 * rights to write into.
10482 */
10483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010485 typval_T *argvars;
10486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010488 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489}
10490
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010491static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010492
10493 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010494findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010497 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498{
10499#ifdef FEAT_SEARCHPATH
10500 char_u *fname;
10501 char_u *fresult = NULL;
10502 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10503 char_u *p;
10504 char_u pathbuf[NUMBUFLEN];
10505 int count = 1;
10506 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010507 int error = FALSE;
10508#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010509
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010510 rettv->vval.v_string = NULL;
10511 rettv->v_type = VAR_STRING;
10512
10513#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010515
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10519 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010520 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 else
10522 {
10523 if (*p != NUL)
10524 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010527 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010529 }
10530
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010531 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10532 error = TRUE;
10533
10534 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 do
10537 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010538 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010539 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 fresult = find_file_in_path_option(first ? fname : NULL,
10541 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010542 0, first, path,
10543 find_what,
10544 curbuf->b_ffname,
10545 find_what == FINDFILE_DIR
10546 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010548
10549 if (fresult != NULL && rettv->v_type == VAR_LIST)
10550 list_append_string(rettv->vval.v_list, fresult, -1);
10551
10552 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010554
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010555 if (rettv->v_type == VAR_STRING)
10556 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010557#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010558}
10559
Bram Moolenaar33570922005-01-25 22:26:29 +000010560static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10561static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562
10563/*
10564 * Implementation of map() and filter().
10565 */
10566 static void
10567filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 int map;
10571{
10572 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 listitem_T *li, *nli;
10575 list_T *l = NULL;
10576 dictitem_T *di;
10577 hashtab_T *ht;
10578 hashitem_T *hi;
10579 dict_T *d = NULL;
10580 typval_T save_val;
10581 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010584 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10585 char *arg_errmsg = (map ? N_("map() argument")
10586 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010587 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010588 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010591 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010592 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010593 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 return;
10595 }
10596 else if (argvars[0].v_type == VAR_DICT)
10597 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010598 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010599 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 return;
10601 }
10602 else
10603 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010604 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 return;
10606 }
10607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 expr = get_tv_string_buf_chk(&argvars[1], buf);
10609 /* On type errors, the preceding call has already displayed an error
10610 * message. Avoid a misleading error message for an empty string that
10611 * was not passed as argument. */
10612 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 prepare_vimvar(VV_VAL, &save_val);
10615 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010616
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010617 /* We reset "did_emsg" to be able to detect whether an error
10618 * occurred during evaluation of the expression. */
10619 save_did_emsg = did_emsg;
10620 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010621
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010622 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 vimvars[VV_KEY].vv_type = VAR_STRING;
10626
10627 ht = &d->dv_hashtab;
10628 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010629 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 if (!HASHITEM_EMPTY(hi))
10633 {
10634 --todo;
10635 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010636 if (tv_check_lock(di->di_tv.v_lock,
10637 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 break;
10639 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010640 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010641 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 break;
10643 if (!map && rem)
10644 dictitem_remove(d, di);
10645 clear_tv(&vimvars[VV_KEY].vv_tv);
10646 }
10647 }
10648 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 }
10650 else
10651 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010652 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010654 for (li = l->lv_first; li != NULL; li = nli)
10655 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010656 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010657 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010659 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010660 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010661 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010662 break;
10663 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010664 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010665 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010667 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010668
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010669 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010671
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010672 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674
10675 copy_tv(&argvars[0], rettv);
10676}
10677
10678 static int
10679filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 char_u *expr;
10682 int map;
10683 int *remp;
10684{
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010687 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010690 s = expr;
10691 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010692 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 if (*s != NUL) /* check for trailing chars after expr */
10694 {
10695 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010696 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 }
10698 if (map)
10699 {
10700 /* map(): replace the list item value */
10701 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010702 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 *tv = rettv;
10704 }
10705 else
10706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 int error = FALSE;
10708
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010711 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 /* On type error, nothing has been removed; return FAIL to stop the
10713 * loop. The error message was given by get_tv_number_chk(). */
10714 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010715 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010716 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010717 retval = OK;
10718theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010720 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010721}
10722
10723/*
10724 * "filter()" function
10725 */
10726 static void
10727f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010730{
10731 filter_map(argvars, rettv, FALSE);
10732}
10733
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735 * "finddir({fname}[, {path}[, {count}]])" function
10736 */
10737 static void
10738f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *argvars;
10740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010742 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010743}
10744
10745/*
10746 * "findfile({fname}[, {path}[, {count}]])" function
10747 */
10748 static void
10749f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010752{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010753 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754}
10755
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010756#ifdef FEAT_FLOAT
10757/*
10758 * "float2nr({float})" function
10759 */
10760 static void
10761f_float2nr(argvars, rettv)
10762 typval_T *argvars;
10763 typval_T *rettv;
10764{
10765 float_T f;
10766
10767 if (get_float_arg(argvars, &f) == OK)
10768 {
10769 if (f < -0x7fffffff)
10770 rettv->vval.v_number = -0x7fffffff;
10771 else if (f > 0x7fffffff)
10772 rettv->vval.v_number = 0x7fffffff;
10773 else
10774 rettv->vval.v_number = (varnumber_T)f;
10775 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010776}
10777
10778/*
10779 * "floor({float})" function
10780 */
10781 static void
10782f_floor(argvars, rettv)
10783 typval_T *argvars;
10784 typval_T *rettv;
10785{
10786 float_T f;
10787
10788 rettv->v_type = VAR_FLOAT;
10789 if (get_float_arg(argvars, &f) == OK)
10790 rettv->vval.v_float = floor(f);
10791 else
10792 rettv->vval.v_float = 0.0;
10793}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010794
10795/*
10796 * "fmod()" function
10797 */
10798 static void
10799f_fmod(argvars, rettv)
10800 typval_T *argvars;
10801 typval_T *rettv;
10802{
10803 float_T fx, fy;
10804
10805 rettv->v_type = VAR_FLOAT;
10806 if (get_float_arg(argvars, &fx) == OK
10807 && get_float_arg(&argvars[1], &fy) == OK)
10808 rettv->vval.v_float = fmod(fx, fy);
10809 else
10810 rettv->vval.v_float = 0.0;
10811}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010812#endif
10813
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010815 * "fnameescape({string})" function
10816 */
10817 static void
10818f_fnameescape(argvars, rettv)
10819 typval_T *argvars;
10820 typval_T *rettv;
10821{
10822 rettv->vval.v_string = vim_strsave_fnameescape(
10823 get_tv_string(&argvars[0]), FALSE);
10824 rettv->v_type = VAR_STRING;
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "fnamemodify({fname}, {mods})" function
10829 */
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 char_u *fname;
10836 char_u *mods;
10837 int usedlen = 0;
10838 int len;
10839 char_u *fbuf = NULL;
10840 char_u buf[NUMBUFLEN];
10841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 fname = get_tv_string_chk(&argvars[0]);
10843 mods = get_tv_string_buf_chk(&argvars[1], buf);
10844 if (fname == NULL || mods == NULL)
10845 fname = NULL;
10846 else
10847 {
10848 len = (int)STRLEN(fname);
10849 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 vim_free(fbuf);
10858}
10859
Bram Moolenaar33570922005-01-25 22:26:29 +000010860static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
10862/*
10863 * "foldclosed()" function
10864 */
10865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010869 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871#ifdef FEAT_FOLDING
10872 linenr_T lnum;
10873 linenr_T first, last;
10874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10877 {
10878 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10879 {
10880 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 return;
10885 }
10886 }
10887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889}
10890
10891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010892 * "foldclosed()" function
10893 */
10894 static void
10895f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 typval_T *argvars;
10897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010898{
10899 foldclosed_both(argvars, rettv, FALSE);
10900}
10901
10902/*
10903 * "foldclosedend()" function
10904 */
10905 static void
10906f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010909{
10910 foldclosed_both(argvars, rettv, TRUE);
10911}
10912
10913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 * "foldlevel()" function
10915 */
10916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010918 typval_T *argvars UNUSED;
10919 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921#ifdef FEAT_FOLDING
10922 linenr_T lnum;
10923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928}
10929
10930/*
10931 * "foldtext()" function
10932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
10938#ifdef FEAT_FOLDING
10939 linenr_T lnum;
10940 char_u *s;
10941 char_u *r;
10942 int len;
10943 char *txt;
10944#endif
10945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946 rettv->v_type = VAR_STRING;
10947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10950 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10951 <= curbuf->b_ml.ml_line_count
10952 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 {
10954 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10956 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
10958 if (!linewhite(lnum))
10959 break;
10960 ++lnum;
10961 }
10962
10963 /* Find interesting text in this line. */
10964 s = skipwhite(ml_get(lnum));
10965 /* skip C comment-start */
10966 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010969 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010971 {
10972 s = skipwhite(ml_get(lnum + 1));
10973 if (*s == '*')
10974 s = skipwhite(s + 1);
10975 }
10976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 txt = _("+-%s%3ld lines: ");
10978 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 + 20 /* for %3ld */
10981 + STRLEN(s))); /* concatenated */
10982 if (r != NULL)
10983 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10985 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10986 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 len = (int)STRLEN(r);
10988 STRCAT(r, s);
10989 /* remove 'foldmarker' and 'commentstring' */
10990 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 }
10993 }
10994#endif
10995}
10996
10997/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010998 * "foldtextresult(lnum)" function
10999 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011004{
11005#ifdef FEAT_FOLDING
11006 linenr_T lnum;
11007 char_u *text;
11008 char_u buf[51];
11009 foldinfo_T foldinfo;
11010 int fold_count;
11011#endif
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->v_type = VAR_STRING;
11014 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011015#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 /* treat illegal types and illegal string values for {lnum} the same */
11018 if (lnum < 0)
11019 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011020 fold_count = foldedCount(curwin, lnum, &foldinfo);
11021 if (fold_count > 0)
11022 {
11023 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11024 &foldinfo, buf);
11025 if (text == buf)
11026 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011028 }
11029#endif
11030}
11031
11032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * "foreground()" function
11034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011037 typval_T *argvars UNUSED;
11038 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040#ifdef FEAT_GUI
11041 if (gui.in_use)
11042 gui_mch_set_foreground();
11043#else
11044# ifdef WIN32
11045 win32_set_foreground();
11046# endif
11047#endif
11048}
11049
11050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051 * "function()" function
11052 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057{
11058 char_u *s;
11059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011061 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011062 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063 /* Don't check an autoload name for existence here. */
11064 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011065 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 else
11067 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011068 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011069 {
11070 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011071 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011072
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011073 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11074 * also be called from another script. Using trans_function_name()
11075 * would also work, but some plugins depend on the name being
11076 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011077 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011078 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011079 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011080 if (rettv->vval.v_string != NULL)
11081 {
11082 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011083 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011084 }
11085 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011086 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011087 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089 }
11090}
11091
11092/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011093 * "garbagecollect()" function
11094 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011095 static void
11096f_garbagecollect(argvars, rettv)
11097 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011098 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011099{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011100 /* This is postponed until we are back at the toplevel, because we may be
11101 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11102 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011103
11104 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11105 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011106}
11107
11108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 * "get()" function
11110 */
11111 static void
11112f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115{
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 listitem_T *li;
11117 list_T *l;
11118 dictitem_T *di;
11119 dict_T *d;
11120 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 int error = FALSE;
11127
11128 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11129 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 else if (argvars[0].v_type == VAR_DICT)
11134 {
11135 if ((d = argvars[0].vval.v_dict) != NULL)
11136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011137 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 if (di != NULL)
11139 tv = &di->di_tv;
11140 }
11141 }
11142 else
11143 EMSG2(_(e_listdictarg), "get()");
11144
11145 if (tv == NULL)
11146 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 copy_tv(&argvars[2], rettv);
11149 }
11150 else
11151 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152}
11153
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154static 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 +000011155
11156/*
11157 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011158 * Return a range (from start to end) of lines in rettv from the specified
11159 * buffer.
11160 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011161 */
11162 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 buf_T *buf;
11165 linenr_T start;
11166 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011167 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011168 typval_T *rettv;
11169{
11170 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011171
Bram Moolenaar959a1432013-12-14 12:17:38 +010011172 rettv->v_type = VAR_STRING;
11173 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011174 if (retlist && rettv_list_alloc(rettv) == FAIL)
11175 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011176
11177 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11178 return;
11179
11180 if (!retlist)
11181 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011182 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11183 p = ml_get_buf(buf, start, FALSE);
11184 else
11185 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011186 rettv->vval.v_string = vim_strsave(p);
11187 }
11188 else
11189 {
11190 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011191 return;
11192
11193 if (start < 1)
11194 start = 1;
11195 if (end > buf->b_ml.ml_line_count)
11196 end = buf->b_ml.ml_line_count;
11197 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011198 if (list_append_string(rettv->vval.v_list,
11199 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011200 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011201 }
11202}
11203
11204/*
11205 * "getbufline()" function
11206 */
11207 static void
11208f_getbufline(argvars, rettv)
11209 typval_T *argvars;
11210 typval_T *rettv;
11211{
11212 linenr_T lnum;
11213 linenr_T end;
11214 buf_T *buf;
11215
11216 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11217 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011218 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011219 --emsg_off;
11220
Bram Moolenaar661b1822005-07-28 22:36:45 +000011221 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011222 if (argvars[2].v_type == VAR_UNKNOWN)
11223 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011224 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011225 end = get_tv_lnum_buf(&argvars[2], buf);
11226
Bram Moolenaar342337a2005-07-21 21:11:17 +000011227 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011228}
11229
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230/*
11231 * "getbufvar()" function
11232 */
11233 static void
11234f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *argvars;
11236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237{
11238 buf_T *buf;
11239 buf_T *save_curbuf;
11240 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011242 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11245 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011247 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011249 rettv->v_type = VAR_STRING;
11250 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251
11252 if (buf != NULL && varname != NULL)
11253 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011254 /* set curbuf to be our buf, temporarily */
11255 save_curbuf = curbuf;
11256 curbuf = buf;
11257
Bram Moolenaar0d660222005-01-07 21:51:51 +000011258 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011259 {
11260 if (get_option_tv(&varname, rettv, TRUE) == OK)
11261 done = TRUE;
11262 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011263 else if (STRCMP(varname, "changedtick") == 0)
11264 {
11265 rettv->v_type = VAR_NUMBER;
11266 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011267 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011268 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011269 else
11270 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011271 /* Look up the variable. */
11272 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11273 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11274 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011275 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011276 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011278 done = TRUE;
11279 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011281
11282 /* restore previous notion of curbuf */
11283 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284 }
11285
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011286 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11287 /* use the default value */
11288 copy_tv(&argvars[2], rettv);
11289
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290 --emsg_off;
11291}
11292
11293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 * "getchar()" function
11295 */
11296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *argvars;
11299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300{
11301 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011304 /* Position the cursor. Needed after a message that ends in a space. */
11305 windgoto(msg_row, msg_col);
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 ++no_mapping;
11308 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011309 for (;;)
11310 {
11311 if (argvars[0].v_type == VAR_UNKNOWN)
11312 /* getchar(): blocking wait. */
11313 n = safe_vgetc();
11314 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11315 /* getchar(1): only check if char avail */
11316 n = vpeekc();
11317 else if (error || vpeekc() == NUL)
11318 /* illegal argument or getchar(0) and no char avail: return zero */
11319 n = 0;
11320 else
11321 /* getchar(0) and char avail: return char */
11322 n = safe_vgetc();
11323 if (n == K_IGNORE)
11324 continue;
11325 break;
11326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 --no_mapping;
11328 --allow_keys;
11329
Bram Moolenaar219b8702006-11-01 14:32:36 +000011330 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11331 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11332 vimvars[VV_MOUSE_COL].vv_nr = 0;
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 if (IS_SPECIAL(n) || mod_mask != 0)
11336 {
11337 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11338 int i = 0;
11339
11340 /* Turn a special key into three bytes, plus modifier. */
11341 if (mod_mask != 0)
11342 {
11343 temp[i++] = K_SPECIAL;
11344 temp[i++] = KS_MODIFIER;
11345 temp[i++] = mod_mask;
11346 }
11347 if (IS_SPECIAL(n))
11348 {
11349 temp[i++] = K_SPECIAL;
11350 temp[i++] = K_SECOND(n);
11351 temp[i++] = K_THIRD(n);
11352 }
11353#ifdef FEAT_MBYTE
11354 else if (has_mbyte)
11355 i += (*mb_char2bytes)(n, temp + i);
11356#endif
11357 else
11358 temp[i++] = n;
11359 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->v_type = VAR_STRING;
11361 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011362
11363#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011364 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011365 {
11366 int row = mouse_row;
11367 int col = mouse_col;
11368 win_T *win;
11369 linenr_T lnum;
11370# ifdef FEAT_WINDOWS
11371 win_T *wp;
11372# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011373 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011374
11375 if (row >= 0 && col >= 0)
11376 {
11377 /* Find the window at the mouse coordinates and compute the
11378 * text position. */
11379 win = mouse_find_win(&row, &col);
11380 (void)mouse_comp_pos(win, &row, &col, &lnum);
11381# ifdef FEAT_WINDOWS
11382 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011383 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011384# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011385 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011386 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11387 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11388 }
11389 }
11390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392}
11393
11394/*
11395 * "getcharmod()" function
11396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403}
11404
11405/*
11406 * "getcmdline()" function
11407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->v_type = VAR_STRING;
11414 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
11418 * "getcmdpos()" function
11419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011422 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426}
11427
11428/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011429 * "getcmdtype()" function
11430 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011431 static void
11432f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011433 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011434 typval_T *rettv;
11435{
11436 rettv->v_type = VAR_STRING;
11437 rettv->vval.v_string = alloc(2);
11438 if (rettv->vval.v_string != NULL)
11439 {
11440 rettv->vval.v_string[0] = get_cmdline_type();
11441 rettv->vval.v_string[1] = NUL;
11442 }
11443}
11444
11445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 * "getcwd()" function
11447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011453 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011456 rettv->vval.v_string = NULL;
11457 cwd = alloc(MAXPATHL);
11458 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011460 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11461 {
11462 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011464 if (rettv->vval.v_string != NULL)
11465 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011467 }
11468 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 }
11470}
11471
11472/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011473 * "getfontname()" function
11474 */
11475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011479{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->v_type = VAR_STRING;
11481 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011482#ifdef FEAT_GUI
11483 if (gui.in_use)
11484 {
11485 GuiFont font;
11486 char_u *name = NULL;
11487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011488 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011489 {
11490 /* Get the "Normal" font. Either the name saved by
11491 * hl_set_font_name() or from the font ID. */
11492 font = gui.norm_font;
11493 name = hl_get_font_name();
11494 }
11495 else
11496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011498 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11499 return;
11500 font = gui_mch_get_font(name, FALSE);
11501 if (font == NOFONT)
11502 return; /* Invalid font name, return empty string. */
11503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011506 gui_mch_free_font(font);
11507 }
11508#endif
11509}
11510
11511/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011512 * "getfperm({fname})" function
11513 */
11514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 typval_T *argvars;
11517 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011518{
11519 char_u *fname;
11520 struct stat st;
11521 char_u *perm = NULL;
11522 char_u flags[] = "rwx";
11523 int i;
11524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011528 if (mch_stat((char *)fname, &st) >= 0)
11529 {
11530 perm = vim_strsave((char_u *)"---------");
11531 if (perm != NULL)
11532 {
11533 for (i = 0; i < 9; i++)
11534 {
11535 if (st.st_mode & (1 << (8 - i)))
11536 perm[i] = flags[i % 3];
11537 }
11538 }
11539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011541}
11542
11543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 * "getfsize({fname})" function
11545 */
11546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 typval_T *argvars;
11549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550{
11551 char_u *fname;
11552 struct stat st;
11553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557
11558 if (mch_stat((char *)fname, &st) >= 0)
11559 {
11560 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011565
11566 /* non-perfect check for overflow */
11567 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11568 rettv->vval.v_number = -2;
11569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 }
11571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573}
11574
11575/*
11576 * "getftime({fname})" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
11583 char_u *fname;
11584 struct stat st;
11585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587
11588 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592}
11593
11594/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011595 * "getftype({fname})" function
11596 */
11597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011599 typval_T *argvars;
11600 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011601{
11602 char_u *fname;
11603 struct stat st;
11604 char_u *type = NULL;
11605 char *t;
11606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011607 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011610 if (mch_lstat((char *)fname, &st) >= 0)
11611 {
11612#ifdef S_ISREG
11613 if (S_ISREG(st.st_mode))
11614 t = "file";
11615 else if (S_ISDIR(st.st_mode))
11616 t = "dir";
11617# ifdef S_ISLNK
11618 else if (S_ISLNK(st.st_mode))
11619 t = "link";
11620# endif
11621# ifdef S_ISBLK
11622 else if (S_ISBLK(st.st_mode))
11623 t = "bdev";
11624# endif
11625# ifdef S_ISCHR
11626 else if (S_ISCHR(st.st_mode))
11627 t = "cdev";
11628# endif
11629# ifdef S_ISFIFO
11630 else if (S_ISFIFO(st.st_mode))
11631 t = "fifo";
11632# endif
11633# ifdef S_ISSOCK
11634 else if (S_ISSOCK(st.st_mode))
11635 t = "fifo";
11636# endif
11637 else
11638 t = "other";
11639#else
11640# ifdef S_IFMT
11641 switch (st.st_mode & S_IFMT)
11642 {
11643 case S_IFREG: t = "file"; break;
11644 case S_IFDIR: t = "dir"; break;
11645# ifdef S_IFLNK
11646 case S_IFLNK: t = "link"; break;
11647# endif
11648# ifdef S_IFBLK
11649 case S_IFBLK: t = "bdev"; break;
11650# endif
11651# ifdef S_IFCHR
11652 case S_IFCHR: t = "cdev"; break;
11653# endif
11654# ifdef S_IFIFO
11655 case S_IFIFO: t = "fifo"; break;
11656# endif
11657# ifdef S_IFSOCK
11658 case S_IFSOCK: t = "socket"; break;
11659# endif
11660 default: t = "other";
11661 }
11662# else
11663 if (mch_isdir(fname))
11664 t = "dir";
11665 else
11666 t = "file";
11667# endif
11668#endif
11669 type = vim_strsave((char_u *)t);
11670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011672}
11673
11674/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011675 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676 */
11677 static void
11678f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011679 typval_T *argvars;
11680 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681{
11682 linenr_T lnum;
11683 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685
11686 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011687 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011688 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011689 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011690 retlist = FALSE;
11691 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011693 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011695 retlist = TRUE;
11696 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011697
Bram Moolenaar342337a2005-07-21 21:11:17 +000011698 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699}
11700
11701/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011702 * "getmatches()" function
11703 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011704 static void
11705f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011706 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011707 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011708{
11709#ifdef FEAT_SEARCH_EXTRA
11710 dict_T *dict;
11711 matchitem_T *cur = curwin->w_match_head;
11712
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011713 if (rettv_list_alloc(rettv) == OK)
11714 {
11715 while (cur != NULL)
11716 {
11717 dict = dict_alloc();
11718 if (dict == NULL)
11719 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011720 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11721 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11722 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11723 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11724 list_append_dict(rettv->vval.v_list, dict);
11725 cur = cur->next;
11726 }
11727 }
11728#endif
11729}
11730
11731/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011732 * "getpid()" function
11733 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011734 static void
11735f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011736 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011737 typval_T *rettv;
11738{
11739 rettv->vval.v_number = mch_get_pid();
11740}
11741
11742/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011743 * "getpos(string)" function
11744 */
11745 static void
11746f_getpos(argvars, rettv)
11747 typval_T *argvars;
11748 typval_T *rettv;
11749{
11750 pos_T *fp;
11751 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011752 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011753
11754 if (rettv_list_alloc(rettv) == OK)
11755 {
11756 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011757 fp = var2fpos(&argvars[0], TRUE, &fnum);
11758 if (fnum != -1)
11759 list_append_number(l, (varnumber_T)fnum);
11760 else
11761 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011762 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11763 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011764 list_append_number(l, (fp != NULL)
11765 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011766 : (varnumber_T)0);
11767 list_append_number(l,
11768#ifdef FEAT_VIRTUALEDIT
11769 (fp != NULL) ? (varnumber_T)fp->coladd :
11770#endif
11771 (varnumber_T)0);
11772 }
11773 else
11774 rettv->vval.v_number = FALSE;
11775}
11776
11777/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011778 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011779 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011780 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011781f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011782 typval_T *argvars UNUSED;
11783 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011784{
11785#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011786 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011787#endif
11788
Bram Moolenaar2641f772005-03-25 21:58:17 +000011789#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011790 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011791 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011792 wp = NULL;
11793 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11794 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011795 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011796 if (wp == NULL)
11797 return;
11798 }
11799
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011800 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011801 }
11802#endif
11803}
11804
11805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 * "getreg()" function
11807 */
11808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011810 typval_T *argvars;
11811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
11813 char_u *strregname;
11814 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011815 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011816 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011819 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 strregname = get_tv_string_chk(&argvars[0]);
11822 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011823 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011826 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11827 return_list = get_tv_number_chk(&argvars[2], &error);
11828 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011831 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011832
11833 if (error)
11834 return;
11835
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 regname = (strregname == NULL ? '"' : *strregname);
11837 if (regname == 0)
11838 regname = '"';
11839
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011840 if (return_list)
11841 {
11842 rettv->v_type = VAR_LIST;
11843 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11844 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11845 }
11846 else
11847 {
11848 rettv->v_type = VAR_STRING;
11849 rettv->vval.v_string = get_reg_contents(regname,
11850 arg2 ? GREG_EXPR_SRC : 0);
11851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853
11854/*
11855 * "getregtype()" function
11856 */
11857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 typval_T *argvars;
11860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
11862 char_u *strregname;
11863 int regname;
11864 char_u buf[NUMBUFLEN + 2];
11865 long reglen = 0;
11866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 {
11869 strregname = get_tv_string_chk(&argvars[0]);
11870 if (strregname == NULL) /* type error; errmsg already given */
11871 {
11872 rettv->v_type = VAR_STRING;
11873 rettv->vval.v_string = NULL;
11874 return;
11875 }
11876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 else
11878 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011879 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880
11881 regname = (strregname == NULL ? '"' : *strregname);
11882 if (regname == 0)
11883 regname = '"';
11884
11885 buf[0] = NUL;
11886 buf[1] = NUL;
11887 switch (get_reg_type(regname, &reglen))
11888 {
11889 case MLINE: buf[0] = 'V'; break;
11890 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 case MBLOCK:
11892 buf[0] = Ctrl_V;
11893 sprintf((char *)buf + 1, "%ld", reglen + 1);
11894 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 rettv->v_type = VAR_STRING;
11897 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
11900/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011901 * "gettabvar()" function
11902 */
11903 static void
11904f_gettabvar(argvars, rettv)
11905 typval_T *argvars;
11906 typval_T *rettv;
11907{
11908 tabpage_T *tp;
11909 dictitem_T *v;
11910 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011911 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011912
11913 rettv->v_type = VAR_STRING;
11914 rettv->vval.v_string = NULL;
11915
11916 varname = get_tv_string_chk(&argvars[1]);
11917 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11918 if (tp != NULL && varname != NULL)
11919 {
11920 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011921 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011922 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011923 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011924 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011925 done = TRUE;
11926 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011927 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011928
11929 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011930 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011931}
11932
11933/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011934 * "gettabwinvar()" function
11935 */
11936 static void
11937f_gettabwinvar(argvars, rettv)
11938 typval_T *argvars;
11939 typval_T *rettv;
11940{
11941 getwinvar(argvars, rettv, 1);
11942}
11943
11944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 * "getwinposx()" function
11946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011949 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953#ifdef FEAT_GUI
11954 if (gui.in_use)
11955 {
11956 int x, y;
11957
11958 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 }
11961#endif
11962}
11963
11964/*
11965 * "getwinposy()" function
11966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973#ifdef FEAT_GUI
11974 if (gui.in_use)
11975 {
11976 int x, y;
11977
11978 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 }
11981#endif
11982}
11983
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011984/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011985 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011986 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011987 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011988find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011989 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011990 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011991{
11992#ifdef FEAT_WINDOWS
11993 win_T *wp;
11994#endif
11995 int nr;
11996
11997 nr = get_tv_number_chk(vp, NULL);
11998
11999#ifdef FEAT_WINDOWS
12000 if (nr < 0)
12001 return NULL;
12002 if (nr == 0)
12003 return curwin;
12004
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012005 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12006 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012007 if (--nr <= 0)
12008 break;
12009 return wp;
12010#else
12011 if (nr == 0 || nr == 1)
12012 return curwin;
12013 return NULL;
12014#endif
12015}
12016
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017/*
12018 * "getwinvar()" function
12019 */
12020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012025 getwinvar(argvars, rettv, 0);
12026}
12027
12028/*
12029 * getwinvar() and gettabwinvar()
12030 */
12031 static void
12032getwinvar(argvars, rettv, off)
12033 typval_T *argvars;
12034 typval_T *rettv;
12035 int off; /* 1 for gettabwinvar() */
12036{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 win_T *win, *oldcurwin;
12038 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012039 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012040 tabpage_T *tp = NULL;
12041 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012044#ifdef FEAT_WINDOWS
12045 if (off == 1)
12046 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12047 else
12048 tp = curtab;
12049#endif
12050 win = find_win_by_nr(&argvars[off], tp);
12051 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012054 rettv->v_type = VAR_STRING;
12055 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
12057 if (win != NULL && varname != NULL)
12058 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012059 /* Set curwin to be our win, temporarily. Also set the tabpage,
12060 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012061 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012062
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012064 {
12065 if (get_option_tv(&varname, rettv, 1) == OK)
12066 done = TRUE;
12067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 else
12069 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012070 /* Look up the variable. */
12071 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12072 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012074 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012075 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012076 done = TRUE;
12077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012079
12080 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012081 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 }
12083
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012084 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12085 /* use the default return value */
12086 copy_tv(&argvars[off + 2], rettv);
12087
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 --emsg_off;
12089}
12090
12091/*
12092 * "glob()" function
12093 */
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012099 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012101 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012103 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012104 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012106 if (argvars[1].v_type != VAR_UNKNOWN)
12107 {
12108 if (get_tv_number_chk(&argvars[1], &error))
12109 options |= WILD_KEEP_ALL;
12110 if (argvars[2].v_type != VAR_UNKNOWN
12111 && get_tv_number_chk(&argvars[2], &error))
12112 {
12113 rettv->v_type = VAR_LIST;
12114 rettv->vval.v_list = NULL;
12115 }
12116 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012117 if (!error)
12118 {
12119 ExpandInit(&xpc);
12120 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012121 if (p_wic)
12122 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012123 if (rettv->v_type == VAR_STRING)
12124 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012125 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012126 else if (rettv_list_alloc(rettv) != FAIL)
12127 {
12128 int i;
12129
12130 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12131 NULL, options, WILD_ALL_KEEP);
12132 for (i = 0; i < xpc.xp_numfiles; i++)
12133 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12134
12135 ExpandCleanup(&xpc);
12136 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012137 }
12138 else
12139 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140}
12141
12142/*
12143 * "globpath()" function
12144 */
12145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 typval_T *argvars;
12148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012150 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012153 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012154 garray_T ga;
12155 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012157 /* When the optional second argument is non-zero, don't remove matches
12158 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012160 if (argvars[2].v_type != VAR_UNKNOWN)
12161 {
12162 if (get_tv_number_chk(&argvars[2], &error))
12163 flags |= WILD_KEEP_ALL;
12164 if (argvars[3].v_type != VAR_UNKNOWN
12165 && get_tv_number_chk(&argvars[3], &error))
12166 {
12167 rettv->v_type = VAR_LIST;
12168 rettv->vval.v_list = NULL;
12169 }
12170 }
12171 if (file != NULL && !error)
12172 {
12173 ga_init2(&ga, (int)sizeof(char_u *), 10);
12174 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12175 if (rettv->v_type == VAR_STRING)
12176 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12177 else if (rettv_list_alloc(rettv) != FAIL)
12178 for (i = 0; i < ga.ga_len; ++i)
12179 list_append_string(rettv->vval.v_list,
12180 ((char_u **)(ga.ga_data))[i], -1);
12181 ga_clear_strings(&ga);
12182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012183 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185}
12186
12187/*
12188 * "has()" function
12189 */
12190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012192 typval_T *argvars;
12193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195 int i;
12196 char_u *name;
12197 int n = FALSE;
12198 static char *(has_list[]) =
12199 {
12200#ifdef AMIGA
12201 "amiga",
12202# ifdef FEAT_ARP
12203 "arp",
12204# endif
12205#endif
12206#ifdef __BEOS__
12207 "beos",
12208#endif
12209#ifdef MSDOS
12210# ifdef DJGPP
12211 "dos32",
12212# else
12213 "dos16",
12214# endif
12215#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012216#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 "mac",
12218#endif
12219#if defined(MACOS_X_UNIX)
12220 "macunix",
12221#endif
12222#ifdef OS2
12223 "os2",
12224#endif
12225#ifdef __QNX__
12226 "qnx",
12227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#ifdef UNIX
12229 "unix",
12230#endif
12231#ifdef VMS
12232 "vms",
12233#endif
12234#ifdef WIN16
12235 "win16",
12236#endif
12237#ifdef WIN32
12238 "win32",
12239#endif
12240#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12241 "win32unix",
12242#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012243#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 "win64",
12245#endif
12246#ifdef EBCDIC
12247 "ebcdic",
12248#endif
12249#ifndef CASE_INSENSITIVE_FILENAME
12250 "fname_case",
12251#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012252#ifdef HAVE_ACL
12253 "acl",
12254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255#ifdef FEAT_ARABIC
12256 "arabic",
12257#endif
12258#ifdef FEAT_AUTOCMD
12259 "autocmd",
12260#endif
12261#ifdef FEAT_BEVAL
12262 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012263# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12264 "balloon_multiline",
12265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#endif
12267#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12268 "builtin_terms",
12269# ifdef ALL_BUILTIN_TCAPS
12270 "all_builtin_terms",
12271# endif
12272#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012273#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12274 || defined(FEAT_GUI_W32) \
12275 || defined(FEAT_GUI_MOTIF))
12276 "browsefilter",
12277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#ifdef FEAT_BYTEOFF
12279 "byte_offset",
12280#endif
12281#ifdef FEAT_CINDENT
12282 "cindent",
12283#endif
12284#ifdef FEAT_CLIENTSERVER
12285 "clientserver",
12286#endif
12287#ifdef FEAT_CLIPBOARD
12288 "clipboard",
12289#endif
12290#ifdef FEAT_CMDL_COMPL
12291 "cmdline_compl",
12292#endif
12293#ifdef FEAT_CMDHIST
12294 "cmdline_hist",
12295#endif
12296#ifdef FEAT_COMMENTS
12297 "comments",
12298#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012299#ifdef FEAT_CONCEAL
12300 "conceal",
12301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302#ifdef FEAT_CRYPT
12303 "cryptv",
12304#endif
12305#ifdef FEAT_CSCOPE
12306 "cscope",
12307#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012308#ifdef FEAT_CURSORBIND
12309 "cursorbind",
12310#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012311#ifdef CURSOR_SHAPE
12312 "cursorshape",
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef DEBUG
12315 "debug",
12316#endif
12317#ifdef FEAT_CON_DIALOG
12318 "dialog_con",
12319#endif
12320#ifdef FEAT_GUI_DIALOG
12321 "dialog_gui",
12322#endif
12323#ifdef FEAT_DIFF
12324 "diff",
12325#endif
12326#ifdef FEAT_DIGRAPHS
12327 "digraphs",
12328#endif
12329#ifdef FEAT_DND
12330 "dnd",
12331#endif
12332#ifdef FEAT_EMACS_TAGS
12333 "emacs_tags",
12334#endif
12335 "eval", /* always present, of course! */
12336#ifdef FEAT_EX_EXTRA
12337 "ex_extra",
12338#endif
12339#ifdef FEAT_SEARCH_EXTRA
12340 "extra_search",
12341#endif
12342#ifdef FEAT_FKMAP
12343 "farsi",
12344#endif
12345#ifdef FEAT_SEARCHPATH
12346 "file_in_path",
12347#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012348#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012349 "filterpipe",
12350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_FIND_ID
12352 "find_in_path",
12353#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012354#ifdef FEAT_FLOAT
12355 "float",
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef FEAT_FOLDING
12358 "folding",
12359#endif
12360#ifdef FEAT_FOOTER
12361 "footer",
12362#endif
12363#if !defined(USE_SYSTEM) && defined(UNIX)
12364 "fork",
12365#endif
12366#ifdef FEAT_GETTEXT
12367 "gettext",
12368#endif
12369#ifdef FEAT_GUI
12370 "gui",
12371#endif
12372#ifdef FEAT_GUI_ATHENA
12373# ifdef FEAT_GUI_NEXTAW
12374 "gui_neXtaw",
12375# else
12376 "gui_athena",
12377# endif
12378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379#ifdef FEAT_GUI_GTK
12380 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012383#ifdef FEAT_GUI_GNOME
12384 "gui_gnome",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_GUI_MAC
12387 "gui_mac",
12388#endif
12389#ifdef FEAT_GUI_MOTIF
12390 "gui_motif",
12391#endif
12392#ifdef FEAT_GUI_PHOTON
12393 "gui_photon",
12394#endif
12395#ifdef FEAT_GUI_W16
12396 "gui_win16",
12397#endif
12398#ifdef FEAT_GUI_W32
12399 "gui_win32",
12400#endif
12401#ifdef FEAT_HANGULIN
12402 "hangul_input",
12403#endif
12404#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12405 "iconv",
12406#endif
12407#ifdef FEAT_INS_EXPAND
12408 "insert_expand",
12409#endif
12410#ifdef FEAT_JUMPLIST
12411 "jumplist",
12412#endif
12413#ifdef FEAT_KEYMAP
12414 "keymap",
12415#endif
12416#ifdef FEAT_LANGMAP
12417 "langmap",
12418#endif
12419#ifdef FEAT_LIBCALL
12420 "libcall",
12421#endif
12422#ifdef FEAT_LINEBREAK
12423 "linebreak",
12424#endif
12425#ifdef FEAT_LISP
12426 "lispindent",
12427#endif
12428#ifdef FEAT_LISTCMDS
12429 "listcmds",
12430#endif
12431#ifdef FEAT_LOCALMAP
12432 "localmap",
12433#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012434#ifdef FEAT_LUA
12435# ifndef DYNAMIC_LUA
12436 "lua",
12437# endif
12438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef FEAT_MENU
12440 "menu",
12441#endif
12442#ifdef FEAT_SESSION
12443 "mksession",
12444#endif
12445#ifdef FEAT_MODIFY_FNAME
12446 "modify_fname",
12447#endif
12448#ifdef FEAT_MOUSE
12449 "mouse",
12450#endif
12451#ifdef FEAT_MOUSESHAPE
12452 "mouseshape",
12453#endif
12454#if defined(UNIX) || defined(VMS)
12455# ifdef FEAT_MOUSE_DEC
12456 "mouse_dec",
12457# endif
12458# ifdef FEAT_MOUSE_GPM
12459 "mouse_gpm",
12460# endif
12461# ifdef FEAT_MOUSE_JSB
12462 "mouse_jsbterm",
12463# endif
12464# ifdef FEAT_MOUSE_NET
12465 "mouse_netterm",
12466# endif
12467# ifdef FEAT_MOUSE_PTERM
12468 "mouse_pterm",
12469# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012470# ifdef FEAT_MOUSE_SGR
12471 "mouse_sgr",
12472# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012473# ifdef FEAT_SYSMOUSE
12474 "mouse_sysmouse",
12475# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012476# ifdef FEAT_MOUSE_URXVT
12477 "mouse_urxvt",
12478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479# ifdef FEAT_MOUSE_XTERM
12480 "mouse_xterm",
12481# endif
12482#endif
12483#ifdef FEAT_MBYTE
12484 "multi_byte",
12485#endif
12486#ifdef FEAT_MBYTE_IME
12487 "multi_byte_ime",
12488#endif
12489#ifdef FEAT_MULTI_LANG
12490 "multi_lang",
12491#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012492#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012493#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012494 "mzscheme",
12495#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497#ifdef FEAT_OLE
12498 "ole",
12499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#ifdef FEAT_PATH_EXTRA
12501 "path_extra",
12502#endif
12503#ifdef FEAT_PERL
12504#ifndef DYNAMIC_PERL
12505 "perl",
12506#endif
12507#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012508#ifdef FEAT_PERSISTENT_UNDO
12509 "persistent_undo",
12510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511#ifdef FEAT_PYTHON
12512#ifndef DYNAMIC_PYTHON
12513 "python",
12514#endif
12515#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012516#ifdef FEAT_PYTHON3
12517#ifndef DYNAMIC_PYTHON3
12518 "python3",
12519#endif
12520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521#ifdef FEAT_POSTSCRIPT
12522 "postscript",
12523#endif
12524#ifdef FEAT_PRINTER
12525 "printer",
12526#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012527#ifdef FEAT_PROFILE
12528 "profile",
12529#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012530#ifdef FEAT_RELTIME
12531 "reltime",
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533#ifdef FEAT_QUICKFIX
12534 "quickfix",
12535#endif
12536#ifdef FEAT_RIGHTLEFT
12537 "rightleft",
12538#endif
12539#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12540 "ruby",
12541#endif
12542#ifdef FEAT_SCROLLBIND
12543 "scrollbind",
12544#endif
12545#ifdef FEAT_CMDL_INFO
12546 "showcmd",
12547 "cmdline_info",
12548#endif
12549#ifdef FEAT_SIGNS
12550 "signs",
12551#endif
12552#ifdef FEAT_SMARTINDENT
12553 "smartindent",
12554#endif
12555#ifdef FEAT_SNIFF
12556 "sniff",
12557#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012558#ifdef STARTUPTIME
12559 "startuptime",
12560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561#ifdef FEAT_STL_OPT
12562 "statusline",
12563#endif
12564#ifdef FEAT_SUN_WORKSHOP
12565 "sun_workshop",
12566#endif
12567#ifdef FEAT_NETBEANS_INTG
12568 "netbeans_intg",
12569#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012570#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012571 "spell",
12572#endif
12573#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 "syntax",
12575#endif
12576#if defined(USE_SYSTEM) || !defined(UNIX)
12577 "system",
12578#endif
12579#ifdef FEAT_TAG_BINS
12580 "tag_binary",
12581#endif
12582#ifdef FEAT_TAG_OLDSTATIC
12583 "tag_old_static",
12584#endif
12585#ifdef FEAT_TAG_ANYWHITE
12586 "tag_any_white",
12587#endif
12588#ifdef FEAT_TCL
12589# ifndef DYNAMIC_TCL
12590 "tcl",
12591# endif
12592#endif
12593#ifdef TERMINFO
12594 "terminfo",
12595#endif
12596#ifdef FEAT_TERMRESPONSE
12597 "termresponse",
12598#endif
12599#ifdef FEAT_TEXTOBJ
12600 "textobjects",
12601#endif
12602#ifdef HAVE_TGETENT
12603 "tgetent",
12604#endif
12605#ifdef FEAT_TITLE
12606 "title",
12607#endif
12608#ifdef FEAT_TOOLBAR
12609 "toolbar",
12610#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012611#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12612 "unnamedplus",
12613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614#ifdef FEAT_USR_CMDS
12615 "user-commands", /* was accidentally included in 5.4 */
12616 "user_commands",
12617#endif
12618#ifdef FEAT_VIMINFO
12619 "viminfo",
12620#endif
12621#ifdef FEAT_VERTSPLIT
12622 "vertsplit",
12623#endif
12624#ifdef FEAT_VIRTUALEDIT
12625 "virtualedit",
12626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628#ifdef FEAT_VISUALEXTRA
12629 "visualextra",
12630#endif
12631#ifdef FEAT_VREPLACE
12632 "vreplace",
12633#endif
12634#ifdef FEAT_WILDIGN
12635 "wildignore",
12636#endif
12637#ifdef FEAT_WILDMENU
12638 "wildmenu",
12639#endif
12640#ifdef FEAT_WINDOWS
12641 "windows",
12642#endif
12643#ifdef FEAT_WAK
12644 "winaltkeys",
12645#endif
12646#ifdef FEAT_WRITEBACKUP
12647 "writebackup",
12648#endif
12649#ifdef FEAT_XIM
12650 "xim",
12651#endif
12652#ifdef FEAT_XFONTSET
12653 "xfontset",
12654#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012655#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012656 "xpm",
12657 "xpm_w32", /* for backward compatibility */
12658#else
12659# if defined(HAVE_XPM)
12660 "xpm",
12661# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663#ifdef USE_XSMP
12664 "xsmp",
12665#endif
12666#ifdef USE_XSMP_INTERACT
12667 "xsmp_interact",
12668#endif
12669#ifdef FEAT_XCLIPBOARD
12670 "xterm_clipboard",
12671#endif
12672#ifdef FEAT_XTERM_SAVE
12673 "xterm_save",
12674#endif
12675#if defined(UNIX) && defined(FEAT_X11)
12676 "X11",
12677#endif
12678 NULL
12679 };
12680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 for (i = 0; has_list[i] != NULL; ++i)
12683 if (STRICMP(name, has_list[i]) == 0)
12684 {
12685 n = TRUE;
12686 break;
12687 }
12688
12689 if (n == FALSE)
12690 {
12691 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012692 {
12693 if (name[5] == '-'
12694 && STRLEN(name) > 11
12695 && vim_isdigit(name[6])
12696 && vim_isdigit(name[8])
12697 && vim_isdigit(name[10]))
12698 {
12699 int major = atoi((char *)name + 6);
12700 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012701
12702 /* Expect "patch-9.9.01234". */
12703 n = (major < VIM_VERSION_MAJOR
12704 || (major == VIM_VERSION_MAJOR
12705 && (minor < VIM_VERSION_MINOR
12706 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012707 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012708 }
12709 else
12710 n = has_patch(atoi((char *)name + 5));
12711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else if (STRICMP(name, "vim_starting") == 0)
12713 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012714#ifdef FEAT_MBYTE
12715 else if (STRICMP(name, "multi_byte_encoding") == 0)
12716 n = has_mbyte;
12717#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012718#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12719 else if (STRICMP(name, "balloon_multiline") == 0)
12720 n = multiline_balloon_available();
12721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef DYNAMIC_TCL
12723 else if (STRICMP(name, "tcl") == 0)
12724 n = tcl_enabled(FALSE);
12725#endif
12726#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12727 else if (STRICMP(name, "iconv") == 0)
12728 n = iconv_enabled(FALSE);
12729#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012730#ifdef DYNAMIC_LUA
12731 else if (STRICMP(name, "lua") == 0)
12732 n = lua_enabled(FALSE);
12733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012734#ifdef DYNAMIC_MZSCHEME
12735 else if (STRICMP(name, "mzscheme") == 0)
12736 n = mzscheme_enabled(FALSE);
12737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738#ifdef DYNAMIC_RUBY
12739 else if (STRICMP(name, "ruby") == 0)
12740 n = ruby_enabled(FALSE);
12741#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012742#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743#ifdef DYNAMIC_PYTHON
12744 else if (STRICMP(name, "python") == 0)
12745 n = python_enabled(FALSE);
12746#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012747#endif
12748#ifdef FEAT_PYTHON3
12749#ifdef DYNAMIC_PYTHON3
12750 else if (STRICMP(name, "python3") == 0)
12751 n = python3_enabled(FALSE);
12752#endif
12753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754#ifdef DYNAMIC_PERL
12755 else if (STRICMP(name, "perl") == 0)
12756 n = perl_enabled(FALSE);
12757#endif
12758#ifdef FEAT_GUI
12759 else if (STRICMP(name, "gui_running") == 0)
12760 n = (gui.in_use || gui.starting);
12761# ifdef FEAT_GUI_W32
12762 else if (STRICMP(name, "gui_win32s") == 0)
12763 n = gui_is_win32s();
12764# endif
12765# ifdef FEAT_BROWSE
12766 else if (STRICMP(name, "browse") == 0)
12767 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12768# endif
12769#endif
12770#ifdef FEAT_SYN_HL
12771 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012772 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773#endif
12774#if defined(WIN3264)
12775 else if (STRICMP(name, "win95") == 0)
12776 n = mch_windows95();
12777#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012778#ifdef FEAT_NETBEANS_INTG
12779 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012780 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 }
12783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785}
12786
12787/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012788 * "has_key()" function
12789 */
12790 static void
12791f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012794{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012795 if (argvars[0].v_type != VAR_DICT)
12796 {
12797 EMSG(_(e_dictreq));
12798 return;
12799 }
12800 if (argvars[0].vval.v_dict == NULL)
12801 return;
12802
12803 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012804 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012805}
12806
12807/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012808 * "haslocaldir()" function
12809 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012810 static void
12811f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012812 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012813 typval_T *rettv;
12814{
12815 rettv->vval.v_number = (curwin->w_localdir != NULL);
12816}
12817
12818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 * "hasmapto()" function
12820 */
12821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *argvars;
12824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
12826 char_u *name;
12827 char_u *mode;
12828 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012829 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 mode = (char_u *)"nvo";
12834 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012837 if (argvars[2].v_type != VAR_UNKNOWN)
12838 abbr = get_tv_number(&argvars[2]);
12839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
Bram Moolenaar2c932302006-03-18 21:42:09 +000012841 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845}
12846
12847/*
12848 * "histadd()" function
12849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854{
12855#ifdef FEAT_CMDHIST
12856 int histype;
12857 char_u *str;
12858 char_u buf[NUMBUFLEN];
12859#endif
12860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 if (check_restricted() || check_secure())
12863 return;
12864#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12866 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 if (histype >= 0)
12868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 if (*str != NUL)
12871 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012872 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 return;
12876 }
12877 }
12878#endif
12879}
12880
12881/*
12882 * "histdel()" function
12883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012886 typval_T *argvars UNUSED;
12887 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
12889#ifdef FEAT_CMDHIST
12890 int n;
12891 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12895 if (str == NULL)
12896 n = 0;
12897 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012899 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012900 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012902 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 else
12905 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012906 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 get_tv_string_buf(&argvars[1], buf));
12908 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909#endif
12910}
12911
12912/*
12913 * "histget()" function
12914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919{
12920#ifdef FEAT_CMDHIST
12921 int type;
12922 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012925 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12926 if (str == NULL)
12927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 {
12930 type = get_histtype(str);
12931 if (argvars[1].v_type == VAR_UNKNOWN)
12932 idx = get_history_idx(type);
12933 else
12934 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12935 /* -1 on type error */
12936 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942}
12943
12944/*
12945 * "histnr()" function
12946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012949 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951{
12952 int i;
12953
12954#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012955 char_u *history = get_tv_string_chk(&argvars[0]);
12956
12957 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 if (i >= HIST_CMD && i < HIST_COUNT)
12959 i = get_history_idx(i);
12960 else
12961#endif
12962 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964}
12965
12966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 * "highlightID(name)" function
12968 */
12969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012971 typval_T *argvars;
12972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975}
12976
12977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978 * "highlight_exists()" function
12979 */
12980 static void
12981f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012982 typval_T *argvars;
12983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012984{
12985 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12986}
12987
12988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 * "hostname()" function
12990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012993 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996 char_u hostname[256];
12997
12998 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->v_type = VAR_STRING;
13000 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001}
13002
13003/*
13004 * iconv() function
13005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013007f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013008 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010{
13011#ifdef FEAT_MBYTE
13012 char_u buf1[NUMBUFLEN];
13013 char_u buf2[NUMBUFLEN];
13014 char_u *from, *to, *str;
13015 vimconv_T vimconv;
13016#endif
13017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 rettv->v_type = VAR_STRING;
13019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
13021#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 str = get_tv_string(&argvars[0]);
13023 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13024 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 vimconv.vc_type = CONV_NONE;
13026 convert_setup(&vimconv, from, to);
13027
13028 /* If the encodings are equal, no conversion needed. */
13029 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013030 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
13034 convert_setup(&vimconv, NULL, NULL);
13035 vim_free(from);
13036 vim_free(to);
13037#endif
13038}
13039
13040/*
13041 * "indent()" function
13042 */
13043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
13048 linenr_T lnum;
13049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055}
13056
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013057/*
13058 * "index()" function
13059 */
13060 static void
13061f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013064{
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 list_T *l;
13066 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013067 long idx = 0;
13068 int ic = FALSE;
13069
13070 rettv->vval.v_number = -1;
13071 if (argvars[0].v_type != VAR_LIST)
13072 {
13073 EMSG(_(e_listreq));
13074 return;
13075 }
13076 l = argvars[0].vval.v_list;
13077 if (l != NULL)
13078 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013079 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013080 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013082 int error = FALSE;
13083
Bram Moolenaar758711c2005-02-02 23:11:38 +000013084 /* Start at specified item. Use the cached index that list_find()
13085 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013087 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013088 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 ic = get_tv_number_chk(&argvars[3], &error);
13090 if (error)
13091 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013092 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013093
Bram Moolenaar758711c2005-02-02 23:11:38 +000013094 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013095 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013096 {
13097 rettv->vval.v_number = idx;
13098 break;
13099 }
13100 }
13101}
13102
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103static int inputsecret_flag = 0;
13104
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013105static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13106
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013108 * This function is used by f_input() and f_inputdialog() functions. The third
13109 * argument to f_input() specifies the type of completion to use at the
13110 * prompt. The third argument to f_inputdialog() specifies the value to return
13111 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 */
13113 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013114get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *argvars;
13116 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013117 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013119 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 char_u *p = NULL;
13121 int c;
13122 char_u buf[NUMBUFLEN];
13123 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013125 int xp_type = EXPAND_NOTHING;
13126 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
13131#ifdef NO_CONSOLE_INPUT
13132 /* While starting up, there is no place to enter text. */
13133 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#endif
13136
13137 cmd_silent = FALSE; /* Want to see the prompt. */
13138 if (prompt != NULL)
13139 {
13140 /* Only the part of the message after the last NL is considered as
13141 * prompt for the command line */
13142 p = vim_strrchr(prompt, '\n');
13143 if (p == NULL)
13144 p = prompt;
13145 else
13146 {
13147 ++p;
13148 c = *p;
13149 *p = NUL;
13150 msg_start();
13151 msg_clr_eos();
13152 msg_puts_attr(prompt, echo_attr);
13153 msg_didout = FALSE;
13154 msg_starthere();
13155 *p = c;
13156 }
13157 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 if (argvars[1].v_type != VAR_UNKNOWN)
13160 {
13161 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13162 if (defstr != NULL)
13163 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013165 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013166 {
13167 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013168 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013169 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013170
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013171 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013172 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013173
Bram Moolenaar4463f292005-09-25 22:20:24 +000013174 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13175 if (xp_name == NULL)
13176 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013177
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013178 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013179
Bram Moolenaar4463f292005-09-25 22:20:24 +000013180 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13181 &xp_arg) == FAIL)
13182 return;
13183 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013184 }
13185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013187 {
13188# ifdef FEAT_EX_EXTRA
13189 int save_ex_normal_busy = ex_normal_busy;
13190 ex_normal_busy = 0;
13191# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013193 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13194 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013195# ifdef FEAT_EX_EXTRA
13196 ex_normal_busy = save_ex_normal_busy;
13197# endif
13198 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013199 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013200 && argvars[1].v_type != VAR_UNKNOWN
13201 && argvars[2].v_type != VAR_UNKNOWN)
13202 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13203 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013204
13205 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 /* since the user typed this, no need to wait for return */
13208 need_wait_return = FALSE;
13209 msg_didout = FALSE;
13210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 cmd_silent = cmd_silent_save;
13212}
13213
13214/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013215 * "input()" function
13216 * Also handles inputsecret() when inputsecret is set.
13217 */
13218 static void
13219f_input(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223 get_user_input(argvars, rettv, FALSE);
13224}
13225
13226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 * "inputdialog()" function
13228 */
13229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
13234#if defined(FEAT_GUI_TEXTDIALOG)
13235 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13236 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13237 {
13238 char_u *message;
13239 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 message = get_tv_string_chk(&argvars[0]);
13243 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013244 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013245 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 else
13247 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013248 if (message != NULL && defstr != NULL
13249 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013250 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 else
13253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 if (message != NULL && defstr != NULL
13255 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013256 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_string = vim_strsave(
13258 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
13264 else
13265#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013266 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267}
13268
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013269/*
13270 * "inputlist()" function
13271 */
13272 static void
13273f_inputlist(argvars, rettv)
13274 typval_T *argvars;
13275 typval_T *rettv;
13276{
13277 listitem_T *li;
13278 int selected;
13279 int mouse_used;
13280
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013281#ifdef NO_CONSOLE_INPUT
13282 /* While starting up, there is no place to enter text. */
13283 if (no_console_input())
13284 return;
13285#endif
13286 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13287 {
13288 EMSG2(_(e_listarg), "inputlist()");
13289 return;
13290 }
13291
13292 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013293 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013294 lines_left = Rows; /* avoid more prompt */
13295 msg_scroll = TRUE;
13296 msg_clr_eos();
13297
13298 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13299 {
13300 msg_puts(get_tv_string(&li->li_tv));
13301 msg_putchar('\n');
13302 }
13303
13304 /* Ask for choice. */
13305 selected = prompt_for_number(&mouse_used);
13306 if (mouse_used)
13307 selected -= lines_left;
13308
13309 rettv->vval.v_number = selected;
13310}
13311
13312
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13314
13315/*
13316 * "inputrestore()" function
13317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319f_inputrestore(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{
13323 if (ga_userinput.ga_len > 0)
13324 {
13325 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13327 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013328 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 }
13330 else if (p_verbose > 1)
13331 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013332 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 }
13335}
13336
13337/*
13338 * "inputsave()" function
13339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013342 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013345 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 if (ga_grow(&ga_userinput, 1) == OK)
13347 {
13348 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13349 + ga_userinput.ga_len);
13350 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013351 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 }
13353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357/*
13358 * "inputsecret()" function
13359 */
13360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013361f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
13365 ++cmdline_star;
13366 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 --cmdline_star;
13369 --inputsecret_flag;
13370}
13371
13372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013373 * "insert()" function
13374 */
13375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379{
13380 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013381 listitem_T *item;
13382 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013384
13385 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013386 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013387 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013388 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013389 {
13390 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 before = get_tv_number_chk(&argvars[2], &error);
13392 if (error)
13393 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013394
Bram Moolenaar758711c2005-02-02 23:11:38 +000013395 if (before == l->lv_len)
13396 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013397 else
13398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013399 item = list_find(l, before);
13400 if (item == NULL)
13401 {
13402 EMSGN(_(e_listidx), before);
13403 l = NULL;
13404 }
13405 }
13406 if (l != NULL)
13407 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013408 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013409 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013410 }
13411 }
13412}
13413
13414/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013415 * "invert(expr)" function
13416 */
13417 static void
13418f_invert(argvars, rettv)
13419 typval_T *argvars;
13420 typval_T *rettv;
13421{
13422 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13423}
13424
13425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 * "isdirectory()" function
13427 */
13428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013430 typval_T *argvars;
13431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013433 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434}
13435
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013436/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013437 * "islocked()" function
13438 */
13439 static void
13440f_islocked(argvars, rettv)
13441 typval_T *argvars;
13442 typval_T *rettv;
13443{
13444 lval_T lv;
13445 char_u *end;
13446 dictitem_T *di;
13447
13448 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013449 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13450 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013451 if (end != NULL && lv.ll_name != NULL)
13452 {
13453 if (*end != NUL)
13454 EMSG(_(e_trailing));
13455 else
13456 {
13457 if (lv.ll_tv == NULL)
13458 {
13459 if (check_changedtick(lv.ll_name))
13460 rettv->vval.v_number = 1; /* always locked */
13461 else
13462 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013463 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013464 if (di != NULL)
13465 {
13466 /* Consider a variable locked when:
13467 * 1. the variable itself is locked
13468 * 2. the value of the variable is locked.
13469 * 3. the List or Dict value is locked.
13470 */
13471 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13472 || tv_islocked(&di->di_tv));
13473 }
13474 }
13475 }
13476 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013477 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013478 else if (lv.ll_newkey != NULL)
13479 EMSG2(_(e_dictkey), lv.ll_newkey);
13480 else if (lv.ll_list != NULL)
13481 /* List item. */
13482 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13483 else
13484 /* Dictionary item. */
13485 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13486 }
13487 }
13488
13489 clear_lval(&lv);
13490}
13491
Bram Moolenaar33570922005-01-25 22:26:29 +000013492static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013493
13494/*
13495 * Turn a dict into a list:
13496 * "what" == 0: list of keys
13497 * "what" == 1: list of values
13498 * "what" == 2: list of items
13499 */
13500 static void
13501dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013502 typval_T *argvars;
13503 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013504 int what;
13505{
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 list_T *l2;
13507 dictitem_T *di;
13508 hashitem_T *hi;
13509 listitem_T *li;
13510 listitem_T *li2;
13511 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013512 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013513
Bram Moolenaar8c711452005-01-14 21:53:12 +000013514 if (argvars[0].v_type != VAR_DICT)
13515 {
13516 EMSG(_(e_dictreq));
13517 return;
13518 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013519 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013520 return;
13521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013523 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013524
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013525 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013528 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013530 --todo;
13531 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013532
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013533 li = listitem_alloc();
13534 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013535 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013536 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013537
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013538 if (what == 0)
13539 {
13540 /* keys() */
13541 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013542 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013543 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13544 }
13545 else if (what == 1)
13546 {
13547 /* values() */
13548 copy_tv(&di->di_tv, &li->li_tv);
13549 }
13550 else
13551 {
13552 /* items() */
13553 l2 = list_alloc();
13554 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013555 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013556 li->li_tv.vval.v_list = l2;
13557 if (l2 == NULL)
13558 break;
13559 ++l2->lv_refcount;
13560
13561 li2 = listitem_alloc();
13562 if (li2 == NULL)
13563 break;
13564 list_append(l2, li2);
13565 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013566 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013567 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13568
13569 li2 = listitem_alloc();
13570 if (li2 == NULL)
13571 break;
13572 list_append(l2, li2);
13573 copy_tv(&di->di_tv, &li2->li_tv);
13574 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013575 }
13576 }
13577}
13578
13579/*
13580 * "items(dict)" function
13581 */
13582 static void
13583f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *argvars;
13585 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013586{
13587 dict_list(argvars, rettv, 2);
13588}
13589
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013591 * "join()" function
13592 */
13593 static void
13594f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 typval_T *argvars;
13596 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013597{
13598 garray_T ga;
13599 char_u *sep;
13600
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013601 if (argvars[0].v_type != VAR_LIST)
13602 {
13603 EMSG(_(e_listreq));
13604 return;
13605 }
13606 if (argvars[0].vval.v_list == NULL)
13607 return;
13608 if (argvars[1].v_type == VAR_UNKNOWN)
13609 sep = (char_u *)" ";
13610 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013611 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013612
13613 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614
13615 if (sep != NULL)
13616 {
13617 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013618 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 ga_append(&ga, NUL);
13620 rettv->vval.v_string = (char_u *)ga.ga_data;
13621 }
13622 else
13623 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013624}
13625
13626/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013627 * "keys()" function
13628 */
13629 static void
13630f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 typval_T *argvars;
13632 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013633{
13634 dict_list(argvars, rettv, 0);
13635}
13636
13637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 * "last_buffer_nr()" function.
13639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
13645 int n = 0;
13646 buf_T *buf;
13647
13648 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13649 if (n < buf->b_fnum)
13650 n = buf->b_fnum;
13651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013653}
13654
13655/*
13656 * "len()" function
13657 */
13658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013660 typval_T *argvars;
13661 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013662{
13663 switch (argvars[0].v_type)
13664 {
13665 case VAR_STRING:
13666 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_number = (varnumber_T)STRLEN(
13668 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013669 break;
13670 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013672 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013673 case VAR_DICT:
13674 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13675 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013676 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013677 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 break;
13679 }
13680}
13681
Bram Moolenaar33570922005-01-25 22:26:29 +000013682static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013683
13684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013686 typval_T *argvars;
13687 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013688 int type;
13689{
13690#ifdef FEAT_LIBCALL
13691 char_u *string_in;
13692 char_u **string_result;
13693 int nr_result;
13694#endif
13695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013697 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013699
13700 if (check_restricted() || check_secure())
13701 return;
13702
13703#ifdef FEAT_LIBCALL
13704 /* The first two args must be strings, otherwise its meaningless */
13705 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13706 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013707 string_in = NULL;
13708 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013709 string_in = argvars[2].vval.v_string;
13710 if (type == VAR_NUMBER)
13711 string_result = NULL;
13712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013714 if (mch_libcall(argvars[0].vval.v_string,
13715 argvars[1].vval.v_string,
13716 string_in,
13717 argvars[2].vval.v_number,
13718 string_result,
13719 &nr_result) == OK
13720 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013722 }
13723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724}
13725
13726/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013727 * "libcall()" function
13728 */
13729 static void
13730f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733{
13734 libcall_common(argvars, rettv, VAR_STRING);
13735}
13736
13737/*
13738 * "libcallnr()" function
13739 */
13740 static void
13741f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 typval_T *argvars;
13743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744{
13745 libcall_common(argvars, rettv, VAR_NUMBER);
13746}
13747
13748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 * "line(string)" function
13750 */
13751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
13754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
13756 linenr_T lnum = 0;
13757 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013758 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013760 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 if (fp != NULL)
13762 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764}
13765
13766/*
13767 * "line2byte(lnum)" function
13768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773{
13774#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#else
13777 linenr_T lnum;
13778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13784 if (rettv->vval.v_number >= 0)
13785 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#endif
13787}
13788
13789/*
13790 * "lispindent(lnum)" function
13791 */
13792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013794 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
13797#ifdef FEAT_LISP
13798 pos_T pos;
13799 linenr_T lnum;
13800
13801 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13804 {
13805 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 curwin->w_cursor = pos;
13808 }
13809 else
13810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812}
13813
13814/*
13815 * "localtime()" function
13816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823}
13824
Bram Moolenaar33570922005-01-25 22:26:29 +000013825static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
13827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 typval_T *argvars;
13830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 int exact;
13832{
13833 char_u *keys;
13834 char_u *which;
13835 char_u buf[NUMBUFLEN];
13836 char_u *keys_buf = NULL;
13837 char_u *rhs;
13838 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013839 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013840 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013841 mapblock_T *mp;
13842 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843
13844 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 rettv->v_type = VAR_STRING;
13846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013848 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 if (*keys == NUL)
13850 return;
13851
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013852 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013856 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013857 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013858 if (argvars[3].v_type != VAR_UNKNOWN)
13859 get_dict = get_tv_number(&argvars[3]);
13860 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 else
13863 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 if (which == NULL)
13865 return;
13866
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 mode = get_map_mode(&which, 0);
13868
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013869 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013870 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013872
13873 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013875 /* Return a string. */
13876 if (rhs != NULL)
13877 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878
Bram Moolenaarbd743252010-10-20 21:23:33 +020013879 }
13880 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13881 {
13882 /* Return a dictionary. */
13883 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13884 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13885 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
Bram Moolenaarbd743252010-10-20 21:23:33 +020013887 dict_add_nr_str(dict, "lhs", 0L, lhs);
13888 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13889 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13890 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13891 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13892 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13893 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013894 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013895 dict_add_nr_str(dict, "mode", 0L, mapmode);
13896
13897 vim_free(lhs);
13898 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 }
13900}
13901
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013902#ifdef FEAT_FLOAT
13903/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013904 * "log()" function
13905 */
13906 static void
13907f_log(argvars, rettv)
13908 typval_T *argvars;
13909 typval_T *rettv;
13910{
13911 float_T f;
13912
13913 rettv->v_type = VAR_FLOAT;
13914 if (get_float_arg(argvars, &f) == OK)
13915 rettv->vval.v_float = log(f);
13916 else
13917 rettv->vval.v_float = 0.0;
13918}
13919
13920/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013921 * "log10()" function
13922 */
13923 static void
13924f_log10(argvars, rettv)
13925 typval_T *argvars;
13926 typval_T *rettv;
13927{
13928 float_T f;
13929
13930 rettv->v_type = VAR_FLOAT;
13931 if (get_float_arg(argvars, &f) == OK)
13932 rettv->vval.v_float = log10(f);
13933 else
13934 rettv->vval.v_float = 0.0;
13935}
13936#endif
13937
Bram Moolenaar1dced572012-04-05 16:54:08 +020013938#ifdef FEAT_LUA
13939/*
13940 * "luaeval()" function
13941 */
13942 static void
13943f_luaeval(argvars, rettv)
13944 typval_T *argvars;
13945 typval_T *rettv;
13946{
13947 char_u *str;
13948 char_u buf[NUMBUFLEN];
13949
13950 str = get_tv_string_buf(&argvars[0], buf);
13951 do_luaeval(str, argvars + 1, rettv);
13952}
13953#endif
13954
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956 * "map()" function
13957 */
13958 static void
13959f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *argvars;
13961 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962{
13963 filter_map(argvars, rettv, TRUE);
13964}
13965
13966/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013967 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 */
13969 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 typval_T *argvars;
13972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013974 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 */
13980 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013982 typval_T *argvars;
13983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986}
13987
Bram Moolenaar33570922005-01-25 22:26:29 +000013988static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989
13990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013992 typval_T *argvars;
13993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 int type;
13995{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013996 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013997 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013998 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 char_u *pat;
14000 regmatch_T regmatch;
14001 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 char_u *save_cpo;
14004 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014005 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014006 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014007 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 list_T *l = NULL;
14009 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014011 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012
14013 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14014 save_cpo = p_cpo;
14015 p_cpo = (char_u *)"";
14016
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014017 rettv->vval.v_number = -1;
14018 if (type == 3)
14019 {
14020 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014021 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014022 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014023 }
14024 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->v_type = VAR_STRING;
14027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014030 if (argvars[0].v_type == VAR_LIST)
14031 {
14032 if ((l = argvars[0].vval.v_list) == NULL)
14033 goto theend;
14034 li = l->lv_first;
14035 }
14036 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014037 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014038 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014039 len = (long)STRLEN(str);
14040 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14043 if (pat == NULL)
14044 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 int error = FALSE;
14049
14050 start = get_tv_number_chk(&argvars[2], &error);
14051 if (error)
14052 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014053 if (l != NULL)
14054 {
14055 li = list_find(l, start);
14056 if (li == NULL)
14057 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014058 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014059 }
14060 else
14061 {
14062 if (start < 0)
14063 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014064 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014066 /* When "count" argument is there ignore matches before "start",
14067 * otherwise skip part of the string. Differs when pattern is "^"
14068 * or "\<". */
14069 if (argvars[3].v_type != VAR_UNKNOWN)
14070 startcol = start;
14071 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014072 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014073 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014074 len -= start;
14075 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014076 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014077
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014078 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 nth = get_tv_number_chk(&argvars[3], &error);
14080 if (error)
14081 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 }
14083
14084 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14085 if (regmatch.regprog != NULL)
14086 {
14087 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014088
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014089 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014090 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014091 if (l != NULL)
14092 {
14093 if (li == NULL)
14094 {
14095 match = FALSE;
14096 break;
14097 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014098 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014099 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014100 if (str == NULL)
14101 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014102 }
14103
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014104 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014105
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014106 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014107 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014108 if (l == NULL && !match)
14109 break;
14110
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014111 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014112 if (l != NULL)
14113 {
14114 li = li->li_next;
14115 ++idx;
14116 }
14117 else
14118 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014119#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014120 startcol = (colnr_T)(regmatch.startp[0]
14121 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014122#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014123 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014124#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014125 if (startcol > (colnr_T)len
14126 || str + startcol <= regmatch.startp[0])
14127 {
14128 match = FALSE;
14129 break;
14130 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014131 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014132 }
14133
14134 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014136 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014137 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014138 int i;
14139
14140 /* return list with matched string and submatches */
14141 for (i = 0; i < NSUBEXP; ++i)
14142 {
14143 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014144 {
14145 if (list_append_string(rettv->vval.v_list,
14146 (char_u *)"", 0) == FAIL)
14147 break;
14148 }
14149 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014150 regmatch.startp[i],
14151 (int)(regmatch.endp[i] - regmatch.startp[i]))
14152 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014153 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014154 }
14155 }
14156 else if (type == 2)
14157 {
14158 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014159 if (l != NULL)
14160 copy_tv(&li->li_tv, rettv);
14161 else
14162 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014164 }
14165 else if (l != NULL)
14166 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 else
14168 {
14169 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 (varnumber_T)(regmatch.startp[0] - str);
14172 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014175 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 }
14177 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014178 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 }
14180
14181theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014182 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 p_cpo = save_cpo;
14184}
14185
14186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014187 * "match()" function
14188 */
14189 static void
14190f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193{
14194 find_some_match(argvars, rettv, 1);
14195}
14196
14197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014198 * "matchadd()" function
14199 */
14200 static void
14201f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014202 typval_T *argvars UNUSED;
14203 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014204{
14205#ifdef FEAT_SEARCH_EXTRA
14206 char_u buf[NUMBUFLEN];
14207 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14208 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14209 int prio = 10; /* default priority */
14210 int id = -1;
14211 int error = FALSE;
14212
14213 rettv->vval.v_number = -1;
14214
14215 if (grp == NULL || pat == NULL)
14216 return;
14217 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014218 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014219 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014220 if (argvars[3].v_type != VAR_UNKNOWN)
14221 id = get_tv_number_chk(&argvars[3], &error);
14222 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014223 if (error == TRUE)
14224 return;
14225 if (id >= 1 && id <= 3)
14226 {
14227 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14228 return;
14229 }
14230
14231 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14232#endif
14233}
14234
14235/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014236 * "matcharg()" function
14237 */
14238 static void
14239f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014240 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014241 typval_T *rettv;
14242{
14243 if (rettv_list_alloc(rettv) == OK)
14244 {
14245#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014246 int id = get_tv_number(&argvars[0]);
14247 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014248
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014249 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014250 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014251 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14252 {
14253 list_append_string(rettv->vval.v_list,
14254 syn_id2name(m->hlg_id), -1);
14255 list_append_string(rettv->vval.v_list, m->pattern, -1);
14256 }
14257 else
14258 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014259 list_append_string(rettv->vval.v_list, NULL, -1);
14260 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014261 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014262 }
14263#endif
14264 }
14265}
14266
14267/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014268 * "matchdelete()" function
14269 */
14270 static void
14271f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014272 typval_T *argvars UNUSED;
14273 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014274{
14275#ifdef FEAT_SEARCH_EXTRA
14276 rettv->vval.v_number = match_delete(curwin,
14277 (int)get_tv_number(&argvars[0]), TRUE);
14278#endif
14279}
14280
14281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282 * "matchend()" function
14283 */
14284 static void
14285f_matchend(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, 0);
14290}
14291
14292/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014293 * "matchlist()" function
14294 */
14295 static void
14296f_matchlist(argvars, rettv)
14297 typval_T *argvars;
14298 typval_T *rettv;
14299{
14300 find_some_match(argvars, rettv, 3);
14301}
14302
14303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014304 * "matchstr()" function
14305 */
14306 static void
14307f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014308 typval_T *argvars;
14309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310{
14311 find_some_match(argvars, rettv, 2);
14312}
14313
Bram Moolenaar33570922005-01-25 22:26:29 +000014314static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014315
14316 static void
14317max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014318 typval_T *argvars;
14319 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014320 int domax;
14321{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014322 long n = 0;
14323 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014324 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014325
14326 if (argvars[0].v_type == VAR_LIST)
14327 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 list_T *l;
14329 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014330
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014331 l = argvars[0].vval.v_list;
14332 if (l != NULL)
14333 {
14334 li = l->lv_first;
14335 if (li != NULL)
14336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014338 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014339 {
14340 li = li->li_next;
14341 if (li == NULL)
14342 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014344 if (domax ? i > n : i < n)
14345 n = i;
14346 }
14347 }
14348 }
14349 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014350 else if (argvars[0].v_type == VAR_DICT)
14351 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014353 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014355 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014356
14357 d = argvars[0].vval.v_dict;
14358 if (d != NULL)
14359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014360 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014365 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014367 if (first)
14368 {
14369 n = i;
14370 first = FALSE;
14371 }
14372 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014373 n = i;
14374 }
14375 }
14376 }
14377 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014378 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014379 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014381}
14382
14383/*
14384 * "max()" function
14385 */
14386 static void
14387f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014390{
14391 max_min(argvars, rettv, TRUE);
14392}
14393
14394/*
14395 * "min()" function
14396 */
14397 static void
14398f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014399 typval_T *argvars;
14400 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014401{
14402 max_min(argvars, rettv, FALSE);
14403}
14404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014405static int mkdir_recurse __ARGS((char_u *dir, int prot));
14406
14407/*
14408 * Create the directory in which "dir" is located, and higher levels when
14409 * needed.
14410 */
14411 static int
14412mkdir_recurse(dir, prot)
14413 char_u *dir;
14414 int prot;
14415{
14416 char_u *p;
14417 char_u *updir;
14418 int r = FAIL;
14419
14420 /* Get end of directory name in "dir".
14421 * We're done when it's "/" or "c:/". */
14422 p = gettail_sep(dir);
14423 if (p <= get_past_head(dir))
14424 return OK;
14425
14426 /* If the directory exists we're done. Otherwise: create it.*/
14427 updir = vim_strnsave(dir, (int)(p - dir));
14428 if (updir == NULL)
14429 return FAIL;
14430 if (mch_isdir(updir))
14431 r = OK;
14432 else if (mkdir_recurse(updir, prot) == OK)
14433 r = vim_mkdir_emsg(updir, prot);
14434 vim_free(updir);
14435 return r;
14436}
14437
14438#ifdef vim_mkdir
14439/*
14440 * "mkdir()" function
14441 */
14442 static void
14443f_mkdir(argvars, rettv)
14444 typval_T *argvars;
14445 typval_T *rettv;
14446{
14447 char_u *dir;
14448 char_u buf[NUMBUFLEN];
14449 int prot = 0755;
14450
14451 rettv->vval.v_number = FAIL;
14452 if (check_restricted() || check_secure())
14453 return;
14454
14455 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014456 if (*dir == NUL)
14457 rettv->vval.v_number = FAIL;
14458 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014459 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014460 if (*gettail(dir) == NUL)
14461 /* remove trailing slashes */
14462 *gettail_sep(dir) = NUL;
14463
14464 if (argvars[1].v_type != VAR_UNKNOWN)
14465 {
14466 if (argvars[2].v_type != VAR_UNKNOWN)
14467 prot = get_tv_number_chk(&argvars[2], NULL);
14468 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14469 mkdir_recurse(dir, prot);
14470 }
14471 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014472 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014473}
14474#endif
14475
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 * "mode()" function
14478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 typval_T *argvars;
14482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014484 char_u buf[3];
14485
14486 buf[1] = NUL;
14487 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (VIsual_active)
14490 {
14491 if (VIsual_select)
14492 buf[0] = VIsual_mode + 's' - 'v';
14493 else
14494 buf[0] = VIsual_mode;
14495 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014496 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014497 || State == CONFIRM)
14498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014500 if (State == ASKMORE)
14501 buf[1] = 'm';
14502 else if (State == CONFIRM)
14503 buf[1] = '?';
14504 }
14505 else if (State == EXTERNCMD)
14506 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 else if (State & INSERT)
14508 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014509#ifdef FEAT_VREPLACE
14510 if (State & VREPLACE_FLAG)
14511 {
14512 buf[0] = 'R';
14513 buf[1] = 'v';
14514 }
14515 else
14516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 if (State & REPLACE_FLAG)
14518 buf[0] = 'R';
14519 else
14520 buf[0] = 'i';
14521 }
14522 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014525 if (exmode_active)
14526 buf[1] = 'v';
14527 }
14528 else if (exmode_active)
14529 {
14530 buf[0] = 'c';
14531 buf[1] = 'e';
14532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014536 if (finish_op)
14537 buf[1] = 'o';
14538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014540 /* Clear out the minor mode when the argument is not a non-zero number or
14541 * non-empty string. */
14542 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014543 buf[1] = NUL;
14544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545 rettv->vval.v_string = vim_strsave(buf);
14546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547}
14548
Bram Moolenaar429fa852013-04-15 12:27:36 +020014549#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014550/*
14551 * "mzeval()" function
14552 */
14553 static void
14554f_mzeval(argvars, rettv)
14555 typval_T *argvars;
14556 typval_T *rettv;
14557{
14558 char_u *str;
14559 char_u buf[NUMBUFLEN];
14560
14561 str = get_tv_string_buf(&argvars[0], buf);
14562 do_mzeval(str, rettv);
14563}
Bram Moolenaar75676462013-01-30 14:55:42 +010014564
14565 void
14566mzscheme_call_vim(name, args, rettv)
14567 char_u *name;
14568 typval_T *args;
14569 typval_T *rettv;
14570{
14571 typval_T argvars[3];
14572
14573 argvars[0].v_type = VAR_STRING;
14574 argvars[0].vval.v_string = name;
14575 copy_tv(args, &argvars[1]);
14576 argvars[2].v_type = VAR_UNKNOWN;
14577 f_call(argvars, rettv);
14578 clear_tv(&argvars[1]);
14579}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014580#endif
14581
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 * "nextnonblank()" function
14584 */
14585 static void
14586f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 typval_T *argvars;
14588 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589{
14590 linenr_T lnum;
14591
14592 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595 {
14596 lnum = 0;
14597 break;
14598 }
14599 if (*skipwhite(ml_get(lnum)) != NUL)
14600 break;
14601 }
14602 rettv->vval.v_number = lnum;
14603}
14604
14605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 * "nr2char()" function
14607 */
14608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612{
14613 char_u buf[NUMBUFLEN];
14614
14615#ifdef FEAT_MBYTE
14616 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014617 {
14618 int utf8 = 0;
14619
14620 if (argvars[1].v_type != VAR_UNKNOWN)
14621 utf8 = get_tv_number_chk(&argvars[1], NULL);
14622 if (utf8)
14623 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14624 else
14625 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 else
14628#endif
14629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 buf[1] = NUL;
14632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 rettv->v_type = VAR_STRING;
14634 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014635}
14636
14637/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014638 * "or(expr, expr)" function
14639 */
14640 static void
14641f_or(argvars, rettv)
14642 typval_T *argvars;
14643 typval_T *rettv;
14644{
14645 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14646 | get_tv_number_chk(&argvars[1], NULL);
14647}
14648
14649/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014650 * "pathshorten()" function
14651 */
14652 static void
14653f_pathshorten(argvars, rettv)
14654 typval_T *argvars;
14655 typval_T *rettv;
14656{
14657 char_u *p;
14658
14659 rettv->v_type = VAR_STRING;
14660 p = get_tv_string_chk(&argvars[0]);
14661 if (p == NULL)
14662 rettv->vval.v_string = NULL;
14663 else
14664 {
14665 p = vim_strsave(p);
14666 rettv->vval.v_string = p;
14667 if (p != NULL)
14668 shorten_dir(p);
14669 }
14670}
14671
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014672#ifdef FEAT_FLOAT
14673/*
14674 * "pow()" function
14675 */
14676 static void
14677f_pow(argvars, rettv)
14678 typval_T *argvars;
14679 typval_T *rettv;
14680{
14681 float_T fx, fy;
14682
14683 rettv->v_type = VAR_FLOAT;
14684 if (get_float_arg(argvars, &fx) == OK
14685 && get_float_arg(&argvars[1], &fy) == OK)
14686 rettv->vval.v_float = pow(fx, fy);
14687 else
14688 rettv->vval.v_float = 0.0;
14689}
14690#endif
14691
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 * "prevnonblank()" function
14694 */
14695 static void
14696f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014697 typval_T *argvars;
14698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699{
14700 linenr_T lnum;
14701
14702 lnum = get_tv_lnum(argvars);
14703 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14704 lnum = 0;
14705 else
14706 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14707 --lnum;
14708 rettv->vval.v_number = lnum;
14709}
14710
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014711#ifdef HAVE_STDARG_H
14712/* This dummy va_list is here because:
14713 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14714 * - locally in the function results in a "used before set" warning
14715 * - using va_start() to initialize it gives "function with fixed args" error */
14716static va_list ap;
14717#endif
14718
Bram Moolenaar8c711452005-01-14 21:53:12 +000014719/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014720 * "printf()" function
14721 */
14722 static void
14723f_printf(argvars, rettv)
14724 typval_T *argvars;
14725 typval_T *rettv;
14726{
14727 rettv->v_type = VAR_STRING;
14728 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014729#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014730 {
14731 char_u buf[NUMBUFLEN];
14732 int len;
14733 char_u *s;
14734 int saved_did_emsg = did_emsg;
14735 char *fmt;
14736
14737 /* Get the required length, allocate the buffer and do it for real. */
14738 did_emsg = FALSE;
14739 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014740 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014741 if (!did_emsg)
14742 {
14743 s = alloc(len + 1);
14744 if (s != NULL)
14745 {
14746 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014747 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014748 }
14749 }
14750 did_emsg |= saved_did_emsg;
14751 }
14752#endif
14753}
14754
14755/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014756 * "pumvisible()" function
14757 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014758 static void
14759f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014760 typval_T *argvars UNUSED;
14761 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014762{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014763#ifdef FEAT_INS_EXPAND
14764 if (pum_visible())
14765 rettv->vval.v_number = 1;
14766#endif
14767}
14768
Bram Moolenaardb913952012-06-29 12:54:53 +020014769#ifdef FEAT_PYTHON3
14770/*
14771 * "py3eval()" function
14772 */
14773 static void
14774f_py3eval(argvars, rettv)
14775 typval_T *argvars;
14776 typval_T *rettv;
14777{
14778 char_u *str;
14779 char_u buf[NUMBUFLEN];
14780
14781 str = get_tv_string_buf(&argvars[0], buf);
14782 do_py3eval(str, rettv);
14783}
14784#endif
14785
14786#ifdef FEAT_PYTHON
14787/*
14788 * "pyeval()" function
14789 */
14790 static void
14791f_pyeval(argvars, rettv)
14792 typval_T *argvars;
14793 typval_T *rettv;
14794{
14795 char_u *str;
14796 char_u buf[NUMBUFLEN];
14797
14798 str = get_tv_string_buf(&argvars[0], buf);
14799 do_pyeval(str, rettv);
14800}
14801#endif
14802
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014803/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 * "range()" function
14805 */
14806 static void
14807f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014808 typval_T *argvars;
14809 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014810{
14811 long start;
14812 long end;
14813 long stride = 1;
14814 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014818 if (argvars[1].v_type == VAR_UNKNOWN)
14819 {
14820 end = start - 1;
14821 start = 0;
14822 }
14823 else
14824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014826 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014828 }
14829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014830 if (error)
14831 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014832 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014833 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014834 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014835 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014836 else
14837 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014838 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014839 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014840 if (list_append_number(rettv->vval.v_list,
14841 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014843 }
14844}
14845
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014846/*
14847 * "readfile()" function
14848 */
14849 static void
14850f_readfile(argvars, rettv)
14851 typval_T *argvars;
14852 typval_T *rettv;
14853{
14854 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014855 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014856 char_u *fname;
14857 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14859 int io_size = sizeof(buf);
14860 int readlen; /* size of last fread() */
14861 char_u *prev = NULL; /* previously read bytes, if any */
14862 long prevlen = 0; /* length of data in prev */
14863 long prevsize = 0; /* size of prev buffer */
14864 long maxline = MAXLNUM;
14865 long cnt = 0;
14866 char_u *p; /* position in buf */
14867 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014868
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014869 if (argvars[1].v_type != VAR_UNKNOWN)
14870 {
14871 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14872 binary = TRUE;
14873 if (argvars[2].v_type != VAR_UNKNOWN)
14874 maxline = get_tv_number(&argvars[2]);
14875 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014876
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014877 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014878 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014879
14880 /* Always open the file in binary mode, library functions have a mind of
14881 * their own about CR-LF conversion. */
14882 fname = get_tv_string(&argvars[0]);
14883 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14884 {
14885 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14886 return;
14887 }
14888
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014889 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014890 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014891 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014892
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014893 /* This for loop processes what was read, but is also entered at end
14894 * of file so that either:
14895 * - an incomplete line gets written
14896 * - a "binary" file gets an empty line at the end if it ends in a
14897 * newline. */
14898 for (p = buf, start = buf;
14899 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14900 ++p)
14901 {
14902 if (*p == '\n' || readlen <= 0)
14903 {
14904 listitem_T *li;
14905 char_u *s = NULL;
14906 long_u len = p - start;
14907
14908 /* Finished a line. Remove CRs before NL. */
14909 if (readlen > 0 && !binary)
14910 {
14911 while (len > 0 && start[len - 1] == '\r')
14912 --len;
14913 /* removal may cross back to the "prev" string */
14914 if (len == 0)
14915 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14916 --prevlen;
14917 }
14918 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014919 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014920 else
14921 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014922 /* Change "prev" buffer to be the right size. This way
14923 * the bytes are only copied once, and very long lines are
14924 * allocated only once. */
14925 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014926 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014928 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014929 prev = NULL; /* the list will own the string */
14930 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014931 }
14932 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014933 if (s == NULL)
14934 {
14935 do_outofmem_msg((long_u) prevlen + len + 1);
14936 failed = TRUE;
14937 break;
14938 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014939
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014940 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014941 {
14942 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014943 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014944 break;
14945 }
14946 li->li_tv.v_type = VAR_STRING;
14947 li->li_tv.v_lock = 0;
14948 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014949 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014950
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014951 start = p + 1; /* step over newline */
14952 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014953 break;
14954 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014955 else if (*p == NUL)
14956 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014957#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014958 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14959 * when finding the BF and check the previous two bytes. */
14960 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014961 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014962 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14963 * + 1, these may be in the "prev" string. */
14964 char_u back1 = p >= buf + 1 ? p[-1]
14965 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14966 char_u back2 = p >= buf + 2 ? p[-2]
14967 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14968 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014969
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014970 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014971 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014972 char_u *dest = p - 2;
14973
14974 /* Usually a BOM is at the beginning of a file, and so at
14975 * the beginning of a line; then we can just step over it.
14976 */
14977 if (start == dest)
14978 start = p + 1;
14979 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014980 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014981 /* have to shuffle buf to close gap */
14982 int adjust_prevlen = 0;
14983
14984 if (dest < buf)
14985 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014986 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014987 dest = buf;
14988 }
14989 if (readlen > p - buf + 1)
14990 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14991 readlen -= 3 - adjust_prevlen;
14992 prevlen -= adjust_prevlen;
14993 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014994 }
14995 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014996 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014997#endif
14998 } /* for */
14999
15000 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15001 break;
15002 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015003 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015004 /* There's part of a line in buf, store it in "prev". */
15005 if (p - start + prevlen >= prevsize)
15006 {
15007 /* need bigger "prev" buffer */
15008 char_u *newprev;
15009
15010 /* A common use case is ordinary text files and "prev" gets a
15011 * fragment of a line, so the first allocation is made
15012 * small, to avoid repeatedly 'allocing' large and
15013 * 'reallocing' small. */
15014 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015015 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015016 else
15017 {
15018 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015019 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015020 prevsize = grow50pc > growmin ? grow50pc : growmin;
15021 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015022 newprev = prev == NULL ? alloc(prevsize)
15023 : vim_realloc(prev, prevsize);
15024 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015025 {
15026 do_outofmem_msg((long_u)prevsize);
15027 failed = TRUE;
15028 break;
15029 }
15030 prev = newprev;
15031 }
15032 /* Add the line part to end of "prev". */
15033 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015034 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015036 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015037
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015038 /*
15039 * For a negative line count use only the lines at the end of the file,
15040 * free the rest.
15041 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015042 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015043 while (cnt > -maxline)
15044 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015045 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015046 --cnt;
15047 }
15048
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015049 if (failed)
15050 {
15051 list_free(rettv->vval.v_list, TRUE);
15052 /* readfile doc says an empty list is returned on error */
15053 rettv->vval.v_list = list_alloc();
15054 }
15055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015056 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015057 fclose(fd);
15058}
15059
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015060#if defined(FEAT_RELTIME)
15061static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15062
15063/*
15064 * Convert a List to proftime_T.
15065 * Return FAIL when there is something wrong.
15066 */
15067 static int
15068list2proftime(arg, tm)
15069 typval_T *arg;
15070 proftime_T *tm;
15071{
15072 long n1, n2;
15073 int error = FALSE;
15074
15075 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15076 || arg->vval.v_list->lv_len != 2)
15077 return FAIL;
15078 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15079 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15080# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015081 tm->HighPart = n1;
15082 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015083# else
15084 tm->tv_sec = n1;
15085 tm->tv_usec = n2;
15086# endif
15087 return error ? FAIL : OK;
15088}
15089#endif /* FEAT_RELTIME */
15090
15091/*
15092 * "reltime()" function
15093 */
15094 static void
15095f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015096 typval_T *argvars UNUSED;
15097 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015098{
15099#ifdef FEAT_RELTIME
15100 proftime_T res;
15101 proftime_T start;
15102
15103 if (argvars[0].v_type == VAR_UNKNOWN)
15104 {
15105 /* No arguments: get current time. */
15106 profile_start(&res);
15107 }
15108 else if (argvars[1].v_type == VAR_UNKNOWN)
15109 {
15110 if (list2proftime(&argvars[0], &res) == FAIL)
15111 return;
15112 profile_end(&res);
15113 }
15114 else
15115 {
15116 /* Two arguments: compute the difference. */
15117 if (list2proftime(&argvars[0], &start) == FAIL
15118 || list2proftime(&argvars[1], &res) == FAIL)
15119 return;
15120 profile_sub(&res, &start);
15121 }
15122
15123 if (rettv_list_alloc(rettv) == OK)
15124 {
15125 long n1, n2;
15126
15127# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015128 n1 = res.HighPart;
15129 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015130# else
15131 n1 = res.tv_sec;
15132 n2 = res.tv_usec;
15133# endif
15134 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15135 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15136 }
15137#endif
15138}
15139
15140/*
15141 * "reltimestr()" function
15142 */
15143 static void
15144f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015145 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015146 typval_T *rettv;
15147{
15148#ifdef FEAT_RELTIME
15149 proftime_T tm;
15150#endif
15151
15152 rettv->v_type = VAR_STRING;
15153 rettv->vval.v_string = NULL;
15154#ifdef FEAT_RELTIME
15155 if (list2proftime(&argvars[0], &tm) == OK)
15156 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15157#endif
15158}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015159
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15161static void make_connection __ARGS((void));
15162static int check_connection __ARGS((void));
15163
15164 static void
15165make_connection()
15166{
15167 if (X_DISPLAY == NULL
15168# ifdef FEAT_GUI
15169 && !gui.in_use
15170# endif
15171 )
15172 {
15173 x_force_connect = TRUE;
15174 setup_term_clip();
15175 x_force_connect = FALSE;
15176 }
15177}
15178
15179 static int
15180check_connection()
15181{
15182 make_connection();
15183 if (X_DISPLAY == NULL)
15184 {
15185 EMSG(_("E240: No connection to Vim server"));
15186 return FAIL;
15187 }
15188 return OK;
15189}
15190#endif
15191
15192#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015193static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194
15195 static void
15196remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 typval_T *argvars;
15198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199 int expr;
15200{
15201 char_u *server_name;
15202 char_u *keys;
15203 char_u *r = NULL;
15204 char_u buf[NUMBUFLEN];
15205# ifdef WIN32
15206 HWND w;
15207# else
15208 Window w;
15209# endif
15210
15211 if (check_restricted() || check_secure())
15212 return;
15213
15214# ifdef FEAT_X11
15215 if (check_connection() == FAIL)
15216 return;
15217# endif
15218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 server_name = get_tv_string_chk(&argvars[0]);
15220 if (server_name == NULL)
15221 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222 keys = get_tv_string_buf(&argvars[1], buf);
15223# ifdef WIN32
15224 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15225# else
15226 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15227 < 0)
15228# endif
15229 {
15230 if (r != NULL)
15231 EMSG(r); /* sending worked but evaluation failed */
15232 else
15233 EMSG2(_("E241: Unable to send to %s"), server_name);
15234 return;
15235 }
15236
15237 rettv->vval.v_string = r;
15238
15239 if (argvars[2].v_type != VAR_UNKNOWN)
15240 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015241 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015242 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015244
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015245 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015246 v.di_tv.v_type = VAR_STRING;
15247 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 idvar = get_tv_string_chk(&argvars[2]);
15249 if (idvar != NULL)
15250 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 }
15253}
15254#endif
15255
15256/*
15257 * "remote_expr()" function
15258 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259 static void
15260f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015261 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263{
15264 rettv->v_type = VAR_STRING;
15265 rettv->vval.v_string = NULL;
15266#ifdef FEAT_CLIENTSERVER
15267 remote_common(argvars, rettv, TRUE);
15268#endif
15269}
15270
15271/*
15272 * "remote_foreground()" function
15273 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 static void
15275f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015276 typval_T *argvars UNUSED;
15277 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279#ifdef FEAT_CLIENTSERVER
15280# ifdef WIN32
15281 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 {
15283 char_u *server_name = get_tv_string_chk(&argvars[0]);
15284
15285 if (server_name != NULL)
15286 serverForeground(server_name);
15287 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288# else
15289 /* Send a foreground() expression to the server. */
15290 argvars[1].v_type = VAR_STRING;
15291 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15292 argvars[2].v_type = VAR_UNKNOWN;
15293 remote_common(argvars, rettv, TRUE);
15294 vim_free(argvars[1].vval.v_string);
15295# endif
15296#endif
15297}
15298
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299 static void
15300f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015301 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015302 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015303{
15304#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306 char_u *s = NULL;
15307# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015308 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015311
15312 if (check_restricted() || check_secure())
15313 {
15314 rettv->vval.v_number = -1;
15315 return;
15316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 serverid = get_tv_string_chk(&argvars[0]);
15318 if (serverid == NULL)
15319 {
15320 rettv->vval.v_number = -1;
15321 return; /* type error; errmsg already given */
15322 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015323# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015324 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 if (n == 0)
15326 rettv->vval.v_number = -1;
15327 else
15328 {
15329 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15330 rettv->vval.v_number = (s != NULL);
15331 }
15332# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333 if (check_connection() == FAIL)
15334 return;
15335
15336 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015337 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338# endif
15339
15340 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015342 char_u *retvar;
15343
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 v.di_tv.v_type = VAR_STRING;
15345 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 retvar = get_tv_string_chk(&argvars[1]);
15347 if (retvar != NULL)
15348 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015349 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350 }
15351#else
15352 rettv->vval.v_number = -1;
15353#endif
15354}
15355
Bram Moolenaar0d660222005-01-07 21:51:51 +000015356 static void
15357f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360{
15361 char_u *r = NULL;
15362
15363#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015364 char_u *serverid = get_tv_string_chk(&argvars[0]);
15365
15366 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367 {
15368# ifdef WIN32
15369 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015370 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015371
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015372 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373 if (n != 0)
15374 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15375 if (r == NULL)
15376# else
15377 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015378 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379# endif
15380 EMSG(_("E277: Unable to read a server reply"));
15381 }
15382#endif
15383 rettv->v_type = VAR_STRING;
15384 rettv->vval.v_string = r;
15385}
15386
15387/*
15388 * "remote_send()" function
15389 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015390 static void
15391f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015392 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015394{
15395 rettv->v_type = VAR_STRING;
15396 rettv->vval.v_string = NULL;
15397#ifdef FEAT_CLIENTSERVER
15398 remote_common(argvars, rettv, FALSE);
15399#endif
15400}
15401
15402/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015403 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404 */
15405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015407 typval_T *argvars;
15408 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409{
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 list_T *l;
15411 listitem_T *item, *item2;
15412 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015413 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015414 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015415 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015416 dict_T *d;
15417 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015418 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015419
Bram Moolenaar8c711452005-01-14 21:53:12 +000015420 if (argvars[0].v_type == VAR_DICT)
15421 {
15422 if (argvars[2].v_type != VAR_UNKNOWN)
15423 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015424 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015425 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015427 key = get_tv_string_chk(&argvars[1]);
15428 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430 di = dict_find(d, key, -1);
15431 if (di == NULL)
15432 EMSG2(_(e_dictkey), key);
15433 else
15434 {
15435 *rettv = di->di_tv;
15436 init_tv(&di->di_tv);
15437 dictitem_remove(d, di);
15438 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015440 }
15441 }
15442 else if (argvars[0].v_type != VAR_LIST)
15443 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015444 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015445 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 int error = FALSE;
15448
15449 idx = get_tv_number_chk(&argvars[1], &error);
15450 if (error)
15451 ; /* type error: do nothing, errmsg already given */
15452 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015453 EMSGN(_(e_listidx), idx);
15454 else
15455 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015456 if (argvars[2].v_type == VAR_UNKNOWN)
15457 {
15458 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015459 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015460 *rettv = item->li_tv;
15461 vim_free(item);
15462 }
15463 else
15464 {
15465 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 end = get_tv_number_chk(&argvars[2], &error);
15467 if (error)
15468 ; /* type error: do nothing */
15469 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015470 EMSGN(_(e_listidx), end);
15471 else
15472 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015473 int cnt = 0;
15474
15475 for (li = item; li != NULL; li = li->li_next)
15476 {
15477 ++cnt;
15478 if (li == item2)
15479 break;
15480 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015481 if (li == NULL) /* didn't find "item2" after "item" */
15482 EMSG(_(e_invrange));
15483 else
15484 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015485 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015487 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015488 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015489 l->lv_first = item;
15490 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015491 item->li_prev = NULL;
15492 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015493 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015494 }
15495 }
15496 }
15497 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015498 }
15499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500}
15501
15502/*
15503 * "rename({from}, {to})" function
15504 */
15505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015507 typval_T *argvars;
15508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509{
15510 char_u buf[NUMBUFLEN];
15511
15512 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15516 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517}
15518
15519/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015520 * "repeat()" function
15521 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015522 static void
15523f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 typval_T *argvars;
15525 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015526{
15527 char_u *p;
15528 int n;
15529 int slen;
15530 int len;
15531 char_u *r;
15532 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015533
15534 n = get_tv_number(&argvars[1]);
15535 if (argvars[0].v_type == VAR_LIST)
15536 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015537 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015538 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015539 if (list_extend(rettv->vval.v_list,
15540 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015541 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015542 }
15543 else
15544 {
15545 p = get_tv_string(&argvars[0]);
15546 rettv->v_type = VAR_STRING;
15547 rettv->vval.v_string = NULL;
15548
15549 slen = (int)STRLEN(p);
15550 len = slen * n;
15551 if (len <= 0)
15552 return;
15553
15554 r = alloc(len + 1);
15555 if (r != NULL)
15556 {
15557 for (i = 0; i < n; i++)
15558 mch_memmove(r + i * slen, p, (size_t)slen);
15559 r[len] = NUL;
15560 }
15561
15562 rettv->vval.v_string = r;
15563 }
15564}
15565
15566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 * "resolve()" function
15568 */
15569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 typval_T *argvars;
15572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573{
15574 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015575#ifdef HAVE_READLINK
15576 char_u *buf = NULL;
15577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580#ifdef FEAT_SHORTCUT
15581 {
15582 char_u *v = NULL;
15583
15584 v = mch_resolve_shortcut(p);
15585 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 }
15590#else
15591# ifdef HAVE_READLINK
15592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 char_u *cpy;
15594 int len;
15595 char_u *remain = NULL;
15596 char_u *q;
15597 int is_relative_to_current = FALSE;
15598 int has_trailing_pathsep = FALSE;
15599 int limit = 100;
15600
15601 p = vim_strsave(p);
15602
15603 if (p[0] == '.' && (vim_ispathsep(p[1])
15604 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15605 is_relative_to_current = TRUE;
15606
15607 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015608 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015611 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613
15614 q = getnextcomp(p);
15615 if (*q != NUL)
15616 {
15617 /* Separate the first path component in "p", and keep the
15618 * remainder (beginning with the path separator). */
15619 remain = vim_strsave(q - 1);
15620 q[-1] = NUL;
15621 }
15622
Bram Moolenaard9462e32011-04-11 21:35:11 +020015623 buf = alloc(MAXPATHL + 1);
15624 if (buf == NULL)
15625 goto fail;
15626
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 for (;;)
15628 {
15629 for (;;)
15630 {
15631 len = readlink((char *)p, (char *)buf, MAXPATHL);
15632 if (len <= 0)
15633 break;
15634 buf[len] = NUL;
15635
15636 if (limit-- == 0)
15637 {
15638 vim_free(p);
15639 vim_free(remain);
15640 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 goto fail;
15643 }
15644
15645 /* Ensure that the result will have a trailing path separator
15646 * if the argument has one. */
15647 if (remain == NULL && has_trailing_pathsep)
15648 add_pathsep(buf);
15649
15650 /* Separate the first path component in the link value and
15651 * concatenate the remainders. */
15652 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15653 if (*q != NUL)
15654 {
15655 if (remain == NULL)
15656 remain = vim_strsave(q - 1);
15657 else
15658 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015659 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 if (cpy != NULL)
15661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 vim_free(remain);
15663 remain = cpy;
15664 }
15665 }
15666 q[-1] = NUL;
15667 }
15668
15669 q = gettail(p);
15670 if (q > p && *q == NUL)
15671 {
15672 /* Ignore trailing path separator. */
15673 q[-1] = NUL;
15674 q = gettail(p);
15675 }
15676 if (q > p && !mch_isFullName(buf))
15677 {
15678 /* symlink is relative to directory of argument */
15679 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15680 if (cpy != NULL)
15681 {
15682 STRCPY(cpy, p);
15683 STRCPY(gettail(cpy), buf);
15684 vim_free(p);
15685 p = cpy;
15686 }
15687 }
15688 else
15689 {
15690 vim_free(p);
15691 p = vim_strsave(buf);
15692 }
15693 }
15694
15695 if (remain == NULL)
15696 break;
15697
15698 /* Append the first path component of "remain" to "p". */
15699 q = getnextcomp(remain + 1);
15700 len = q - remain - (*q != NUL);
15701 cpy = vim_strnsave(p, STRLEN(p) + len);
15702 if (cpy != NULL)
15703 {
15704 STRNCAT(cpy, remain, len);
15705 vim_free(p);
15706 p = cpy;
15707 }
15708 /* Shorten "remain". */
15709 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015710 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 else
15712 {
15713 vim_free(remain);
15714 remain = NULL;
15715 }
15716 }
15717
15718 /* If the result is a relative path name, make it explicitly relative to
15719 * the current directory if and only if the argument had this form. */
15720 if (!vim_ispathsep(*p))
15721 {
15722 if (is_relative_to_current
15723 && *p != NUL
15724 && !(p[0] == '.'
15725 && (p[1] == NUL
15726 || vim_ispathsep(p[1])
15727 || (p[1] == '.'
15728 && (p[2] == NUL
15729 || vim_ispathsep(p[2]))))))
15730 {
15731 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015732 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 if (cpy != NULL)
15734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 vim_free(p);
15736 p = cpy;
15737 }
15738 }
15739 else if (!is_relative_to_current)
15740 {
15741 /* Strip leading "./". */
15742 q = p;
15743 while (q[0] == '.' && vim_ispathsep(q[1]))
15744 q += 2;
15745 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015746 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 }
15748 }
15749
15750 /* Ensure that the result will have no trailing path separator
15751 * if the argument had none. But keep "/" or "//". */
15752 if (!has_trailing_pathsep)
15753 {
15754 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015755 if (after_pathsep(p, q))
15756 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 }
15758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 }
15761# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763# endif
15764#endif
15765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767
15768#ifdef HAVE_READLINK
15769fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015770 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773}
15774
15775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 */
15778 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015780 typval_T *argvars;
15781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782{
Bram Moolenaar33570922005-01-25 22:26:29 +000015783 list_T *l;
15784 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785
Bram Moolenaar0d660222005-01-07 21:51:51 +000015786 if (argvars[0].v_type != VAR_LIST)
15787 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015788 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015789 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015790 {
15791 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015792 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015793 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794 while (li != NULL)
15795 {
15796 ni = li->li_prev;
15797 list_append(l, li);
15798 li = ni;
15799 }
15800 rettv->vval.v_list = l;
15801 rettv->v_type = VAR_LIST;
15802 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015803 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805}
15806
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015807#define SP_NOMOVE 0x01 /* don't move cursor */
15808#define SP_REPEAT 0x02 /* repeat to find outer pair */
15809#define SP_RETCOUNT 0x04 /* return matchcount */
15810#define SP_SETPCMARK 0x08 /* set previous context mark */
15811#define SP_START 0x10 /* accept match at start position */
15812#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15813#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015814
Bram Moolenaar33570922005-01-25 22:26:29 +000015815static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816
15817/*
15818 * Get flags for a search function.
15819 * Possibly sets "p_ws".
15820 * Returns BACKWARD, FORWARD or zero (for an error).
15821 */
15822 static int
15823get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825 int *flagsp;
15826{
15827 int dir = FORWARD;
15828 char_u *flags;
15829 char_u nbuf[NUMBUFLEN];
15830 int mask;
15831
15832 if (varp->v_type != VAR_UNKNOWN)
15833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015834 flags = get_tv_string_buf_chk(varp, nbuf);
15835 if (flags == NULL)
15836 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015837 while (*flags != NUL)
15838 {
15839 switch (*flags)
15840 {
15841 case 'b': dir = BACKWARD; break;
15842 case 'w': p_ws = TRUE; break;
15843 case 'W': p_ws = FALSE; break;
15844 default: mask = 0;
15845 if (flagsp != NULL)
15846 switch (*flags)
15847 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015848 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015849 case 'e': mask = SP_END; break;
15850 case 'm': mask = SP_RETCOUNT; break;
15851 case 'n': mask = SP_NOMOVE; break;
15852 case 'p': mask = SP_SUBPAT; break;
15853 case 'r': mask = SP_REPEAT; break;
15854 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 }
15856 if (mask == 0)
15857 {
15858 EMSG2(_(e_invarg2), flags);
15859 dir = 0;
15860 }
15861 else
15862 *flagsp |= mask;
15863 }
15864 if (dir == 0)
15865 break;
15866 ++flags;
15867 }
15868 }
15869 return dir;
15870}
15871
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015873 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015875 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015876search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015878 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015879 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015881 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 char_u *pat;
15883 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015884 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 int save_p_ws = p_ws;
15886 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015887 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015888 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015889 proftime_T tm;
15890#ifdef FEAT_RELTIME
15891 long time_limit = 0;
15892#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015893 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015894 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015897 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015898 if (dir == 0)
15899 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015900 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015901 if (flags & SP_START)
15902 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015903 if (flags & SP_END)
15904 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015905
Bram Moolenaar76929292008-01-06 19:07:36 +000015906 /* Optional arguments: line number to stop searching and timeout. */
15907 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015908 {
15909 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15910 if (lnum_stop < 0)
15911 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015912#ifdef FEAT_RELTIME
15913 if (argvars[3].v_type != VAR_UNKNOWN)
15914 {
15915 time_limit = get_tv_number_chk(&argvars[3], NULL);
15916 if (time_limit < 0)
15917 goto theend;
15918 }
15919#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015920 }
15921
Bram Moolenaar76929292008-01-06 19:07:36 +000015922#ifdef FEAT_RELTIME
15923 /* Set the time limit, if there is one. */
15924 profile_setlimit(time_limit, &tm);
15925#endif
15926
Bram Moolenaar231334e2005-07-25 20:46:57 +000015927 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015928 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015929 * Check to make sure only those flags are set.
15930 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15931 * flags cannot be set. Check for that condition also.
15932 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015933 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015934 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015936 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015937 goto theend;
15938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015940 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015941 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015942 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015943 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015945 if (flags & SP_SUBPAT)
15946 retval = subpatnum;
15947 else
15948 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015949 if (flags & SP_SETPCMARK)
15950 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015952 if (match_pos != NULL)
15953 {
15954 /* Store the match cursor position */
15955 match_pos->lnum = pos.lnum;
15956 match_pos->col = pos.col + 1;
15957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 /* "/$" will put the cursor after the end of the line, may need to
15959 * correct that here */
15960 check_cursor();
15961 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015962
15963 /* If 'n' flag is used: restore cursor position. */
15964 if (flags & SP_NOMOVE)
15965 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015966 else
15967 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015968theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015970
15971 return retval;
15972}
15973
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015974#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015975
15976/*
15977 * round() is not in C90, use ceil() or floor() instead.
15978 */
15979 float_T
15980vim_round(f)
15981 float_T f;
15982{
15983 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15984}
15985
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015986/*
15987 * "round({float})" function
15988 */
15989 static void
15990f_round(argvars, rettv)
15991 typval_T *argvars;
15992 typval_T *rettv;
15993{
15994 float_T f;
15995
15996 rettv->v_type = VAR_FLOAT;
15997 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015998 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015999 else
16000 rettv->vval.v_float = 0.0;
16001}
16002#endif
16003
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016004/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016005 * "screenattr()" function
16006 */
16007 static void
16008f_screenattr(argvars, rettv)
16009 typval_T *argvars UNUSED;
16010 typval_T *rettv;
16011{
16012 int row;
16013 int col;
16014 int c;
16015
16016 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16017 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16018 if (row < 0 || row >= screen_Rows
16019 || col < 0 || col >= screen_Columns)
16020 c = -1;
16021 else
16022 c = ScreenAttrs[LineOffset[row] + col];
16023 rettv->vval.v_number = c;
16024}
16025
16026/*
16027 * "screenchar()" function
16028 */
16029 static void
16030f_screenchar(argvars, rettv)
16031 typval_T *argvars UNUSED;
16032 typval_T *rettv;
16033{
16034 int row;
16035 int col;
16036 int off;
16037 int c;
16038
16039 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16040 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16041 if (row < 0 || row >= screen_Rows
16042 || col < 0 || col >= screen_Columns)
16043 c = -1;
16044 else
16045 {
16046 off = LineOffset[row] + col;
16047#ifdef FEAT_MBYTE
16048 if (enc_utf8 && ScreenLinesUC[off] != 0)
16049 c = ScreenLinesUC[off];
16050 else
16051#endif
16052 c = ScreenLines[off];
16053 }
16054 rettv->vval.v_number = c;
16055}
16056
16057/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016058 * "screencol()" function
16059 *
16060 * First column is 1 to be consistent with virtcol().
16061 */
16062 static void
16063f_screencol(argvars, rettv)
16064 typval_T *argvars UNUSED;
16065 typval_T *rettv;
16066{
16067 rettv->vval.v_number = screen_screencol() + 1;
16068}
16069
16070/*
16071 * "screenrow()" function
16072 */
16073 static void
16074f_screenrow(argvars, rettv)
16075 typval_T *argvars UNUSED;
16076 typval_T *rettv;
16077{
16078 rettv->vval.v_number = screen_screenrow() + 1;
16079}
16080
16081/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016082 * "search()" function
16083 */
16084 static void
16085f_search(argvars, rettv)
16086 typval_T *argvars;
16087 typval_T *rettv;
16088{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016089 int flags = 0;
16090
16091 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092}
16093
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016095 * "searchdecl()" function
16096 */
16097 static void
16098f_searchdecl(argvars, rettv)
16099 typval_T *argvars;
16100 typval_T *rettv;
16101{
16102 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016103 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016104 int error = FALSE;
16105 char_u *name;
16106
16107 rettv->vval.v_number = 1; /* default: FAIL */
16108
16109 name = get_tv_string_chk(&argvars[0]);
16110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016111 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016112 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016113 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16114 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16115 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016116 if (!error && name != NULL)
16117 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016118 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016119}
16120
16121/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016122 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016124 static int
16125searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128{
16129 char_u *spat, *mpat, *epat;
16130 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 int dir;
16133 int flags = 0;
16134 char_u nbuf1[NUMBUFLEN];
16135 char_u nbuf2[NUMBUFLEN];
16136 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016139 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 spat = get_tv_string_chk(&argvars[0]);
16143 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16144 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16145 if (spat == NULL || mpat == NULL || epat == NULL)
16146 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 /* Handle the optional fourth argument: flags */
16149 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016150 if (dir == 0)
16151 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016152
16153 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016154 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16155 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016156 if ((flags & (SP_END | SP_SUBPAT)) != 0
16157 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016158 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016159 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016160 goto theend;
16161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016163 /* Using 'r' implies 'W', otherwise it doesn't work. */
16164 if (flags & SP_REPEAT)
16165 p_ws = FALSE;
16166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016167 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016168 if (argvars[3].v_type == VAR_UNKNOWN
16169 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 skip = (char_u *)"";
16171 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016174 if (argvars[5].v_type != VAR_UNKNOWN)
16175 {
16176 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16177 if (lnum_stop < 0)
16178 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016179#ifdef FEAT_RELTIME
16180 if (argvars[6].v_type != VAR_UNKNOWN)
16181 {
16182 time_limit = get_tv_number_chk(&argvars[6], NULL);
16183 if (time_limit < 0)
16184 goto theend;
16185 }
16186#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016187 }
16188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016189 if (skip == NULL)
16190 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016192 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016193 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016194
16195theend:
16196 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016197
16198 return retval;
16199}
16200
16201/*
16202 * "searchpair()" function
16203 */
16204 static void
16205f_searchpair(argvars, rettv)
16206 typval_T *argvars;
16207 typval_T *rettv;
16208{
16209 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16210}
16211
16212/*
16213 * "searchpairpos()" function
16214 */
16215 static void
16216f_searchpairpos(argvars, rettv)
16217 typval_T *argvars;
16218 typval_T *rettv;
16219{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016220 pos_T match_pos;
16221 int lnum = 0;
16222 int col = 0;
16223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016225 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016226
16227 if (searchpair_cmn(argvars, &match_pos) > 0)
16228 {
16229 lnum = match_pos.lnum;
16230 col = match_pos.col;
16231 }
16232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016233 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16234 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016235}
16236
16237/*
16238 * Search for a start/middle/end thing.
16239 * Used by searchpair(), see its documentation for the details.
16240 * Returns 0 or -1 for no match,
16241 */
16242 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016243do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16244 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016245 char_u *spat; /* start pattern */
16246 char_u *mpat; /* middle pattern */
16247 char_u *epat; /* end pattern */
16248 int dir; /* BACKWARD or FORWARD */
16249 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016250 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016251 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016252 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016253 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016254{
16255 char_u *save_cpo;
16256 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16257 long retval = 0;
16258 pos_T pos;
16259 pos_T firstpos;
16260 pos_T foundpos;
16261 pos_T save_cursor;
16262 pos_T save_pos;
16263 int n;
16264 int r;
16265 int nest = 1;
16266 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016267 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016268 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016269
16270 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16271 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016272 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016273
Bram Moolenaar76929292008-01-06 19:07:36 +000016274#ifdef FEAT_RELTIME
16275 /* Set the time limit, if there is one. */
16276 profile_setlimit(time_limit, &tm);
16277#endif
16278
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016279 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16280 * start/middle/end (pat3, for the top pair). */
16281 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16282 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16283 if (pat2 == NULL || pat3 == NULL)
16284 goto theend;
16285 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16286 if (*mpat == NUL)
16287 STRCPY(pat3, pat2);
16288 else
16289 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16290 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016291 if (flags & SP_START)
16292 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 save_cursor = curwin->w_cursor;
16295 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016296 clearpos(&firstpos);
16297 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 pat = pat3;
16299 for (;;)
16300 {
16301 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016302 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16304 /* didn't find it or found the first match again: FAIL */
16305 break;
16306
16307 if (firstpos.lnum == 0)
16308 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016309 if (equalpos(pos, foundpos))
16310 {
16311 /* Found the same position again. Can happen with a pattern that
16312 * has "\zs" at the end and searching backwards. Advance one
16313 * character and try again. */
16314 if (dir == BACKWARD)
16315 decl(&pos);
16316 else
16317 incl(&pos);
16318 }
16319 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016321 /* clear the start flag to avoid getting stuck here */
16322 options &= ~SEARCH_START;
16323
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 /* If the skip pattern matches, ignore this match. */
16325 if (*skip != NUL)
16326 {
16327 save_pos = curwin->w_cursor;
16328 curwin->w_cursor = pos;
16329 r = eval_to_bool(skip, &err, NULL, FALSE);
16330 curwin->w_cursor = save_pos;
16331 if (err)
16332 {
16333 /* Evaluating {skip} caused an error, break here. */
16334 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016335 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 break;
16337 }
16338 if (r)
16339 continue;
16340 }
16341
16342 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16343 {
16344 /* Found end when searching backwards or start when searching
16345 * forward: nested pair. */
16346 ++nest;
16347 pat = pat2; /* nested, don't search for middle */
16348 }
16349 else
16350 {
16351 /* Found end when searching forward or start when searching
16352 * backward: end of (nested) pair; or found middle in outer pair. */
16353 if (--nest == 1)
16354 pat = pat3; /* outer level, search for middle */
16355 }
16356
16357 if (nest == 0)
16358 {
16359 /* Found the match: return matchcount or line number. */
16360 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016361 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016363 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016364 if (flags & SP_SETPCMARK)
16365 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 curwin->w_cursor = pos;
16367 if (!(flags & SP_REPEAT))
16368 break;
16369 nest = 1; /* search for next unmatched */
16370 }
16371 }
16372
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373 if (match_pos != NULL)
16374 {
16375 /* Store the match cursor position */
16376 match_pos->lnum = curwin->w_cursor.lnum;
16377 match_pos->col = curwin->w_cursor.col + 1;
16378 }
16379
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016381 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 curwin->w_cursor = save_cursor;
16383
16384theend:
16385 vim_free(pat2);
16386 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016387 if (p_cpo == empty_option)
16388 p_cpo = save_cpo;
16389 else
16390 /* Darn, evaluating the {skip} expression changed the value. */
16391 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016392
16393 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394}
16395
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016396/*
16397 * "searchpos()" function
16398 */
16399 static void
16400f_searchpos(argvars, rettv)
16401 typval_T *argvars;
16402 typval_T *rettv;
16403{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016404 pos_T match_pos;
16405 int lnum = 0;
16406 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016407 int n;
16408 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016411 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016412
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016413 n = search_cmn(argvars, &match_pos, &flags);
16414 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016415 {
16416 lnum = match_pos.lnum;
16417 col = match_pos.col;
16418 }
16419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016420 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16421 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016422 if (flags & SP_SUBPAT)
16423 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016424}
16425
16426
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 static void
16428f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016432#ifdef FEAT_CLIENTSERVER
16433 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 char_u *server = get_tv_string_chk(&argvars[0]);
16435 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 if (server == NULL || reply == NULL)
16439 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440 if (check_restricted() || check_secure())
16441 return;
16442# ifdef FEAT_X11
16443 if (check_connection() == FAIL)
16444 return;
16445# endif
16446
16447 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 EMSG(_("E258: Unable to send to client"));
16450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 rettv->vval.v_number = 0;
16453#else
16454 rettv->vval.v_number = -1;
16455#endif
16456}
16457
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 static void
16459f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462{
16463 char_u *r = NULL;
16464
16465#ifdef FEAT_CLIENTSERVER
16466# ifdef WIN32
16467 r = serverGetVimNames();
16468# else
16469 make_connection();
16470 if (X_DISPLAY != NULL)
16471 r = serverGetVimNames(X_DISPLAY);
16472# endif
16473#endif
16474 rettv->v_type = VAR_STRING;
16475 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476}
16477
16478/*
16479 * "setbufvar()" function
16480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016482f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016484 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
16486 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 char_u nbuf[NUMBUFLEN];
16491
16492 if (check_restricted() || check_secure())
16493 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016494 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16495 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016496 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 varp = &argvars[2];
16498
16499 if (buf != NULL && varname != NULL && varp != NULL)
16500 {
16501 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503
16504 if (*varname == '&')
16505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 long numval;
16507 char_u *strval;
16508 int error = FALSE;
16509
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511 numval = get_tv_number_chk(varp, &error);
16512 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 if (!error && strval != NULL)
16514 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 }
16516 else
16517 {
16518 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16519 if (bufvarname != NULL)
16520 {
16521 STRCPY(bufvarname, "b:");
16522 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016523 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 vim_free(bufvarname);
16525 }
16526 }
16527
16528 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531}
16532
16533/*
16534 * "setcmdpos()" function
16535 */
16536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016537f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016538 typval_T *argvars;
16539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 int pos = (int)get_tv_number(&argvars[0]) - 1;
16542
16543 if (pos >= 0)
16544 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545}
16546
16547/*
16548 * "setline()" function
16549 */
16550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 typval_T *argvars;
16553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554{
16555 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016556 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 list_T *l = NULL;
16558 listitem_T *li = NULL;
16559 long added = 0;
16560 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016562 lnum = get_tv_lnum(&argvars[0]);
16563 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016565 l = argvars[1].vval.v_list;
16566 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016568 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016569 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016570
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016571 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016572 for (;;)
16573 {
16574 if (l != NULL)
16575 {
16576 /* list argument, get next string */
16577 if (li == NULL)
16578 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016580 li = li->li_next;
16581 }
16582
16583 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016585 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016586
16587 /* When coming here from Insert mode, sync undo, so that this can be
16588 * undone separately from what was previously inserted. */
16589 if (u_sync_once == 2)
16590 {
16591 u_sync_once = 1; /* notify that u_sync() was called */
16592 u_sync(TRUE);
16593 }
16594
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016595 if (lnum <= curbuf->b_ml.ml_line_count)
16596 {
16597 /* existing line, replace it */
16598 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16599 {
16600 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016601 if (lnum == curwin->w_cursor.lnum)
16602 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016603 rettv->vval.v_number = 0; /* OK */
16604 }
16605 }
16606 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16607 {
16608 /* lnum is one past the last line, append the line */
16609 ++added;
16610 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16611 rettv->vval.v_number = 0; /* OK */
16612 }
16613
16614 if (l == NULL) /* only one string argument */
16615 break;
16616 ++lnum;
16617 }
16618
16619 if (added > 0)
16620 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621}
16622
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016623static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16624
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016626 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016627 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016628 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016629set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016630 win_T *wp UNUSED;
16631 typval_T *list_arg UNUSED;
16632 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016633 typval_T *rettv;
16634{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016635#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016636 char_u *act;
16637 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016638#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016639
Bram Moolenaar2641f772005-03-25 21:58:17 +000016640 rettv->vval.v_number = -1;
16641
16642#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016643 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016644 EMSG(_(e_listreq));
16645 else
16646 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016647 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016648
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016649 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016650 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016651 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016652 if (act == NULL)
16653 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016654 if (*act == 'a' || *act == 'r')
16655 action = *act;
16656 }
16657
Bram Moolenaar81484f42012-12-05 15:16:47 +010016658 if (l != NULL && set_errorlist(wp, l, action,
16659 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016660 rettv->vval.v_number = 0;
16661 }
16662#endif
16663}
16664
16665/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016666 * "setloclist()" function
16667 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016668 static void
16669f_setloclist(argvars, rettv)
16670 typval_T *argvars;
16671 typval_T *rettv;
16672{
16673 win_T *win;
16674
16675 rettv->vval.v_number = -1;
16676
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016677 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016678 if (win != NULL)
16679 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16680}
16681
16682/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016683 * "setmatches()" function
16684 */
16685 static void
16686f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016687 typval_T *argvars UNUSED;
16688 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016689{
16690#ifdef FEAT_SEARCH_EXTRA
16691 list_T *l;
16692 listitem_T *li;
16693 dict_T *d;
16694
16695 rettv->vval.v_number = -1;
16696 if (argvars[0].v_type != VAR_LIST)
16697 {
16698 EMSG(_(e_listreq));
16699 return;
16700 }
16701 if ((l = argvars[0].vval.v_list) != NULL)
16702 {
16703
16704 /* To some extent make sure that we are dealing with a list from
16705 * "getmatches()". */
16706 li = l->lv_first;
16707 while (li != NULL)
16708 {
16709 if (li->li_tv.v_type != VAR_DICT
16710 || (d = li->li_tv.vval.v_dict) == NULL)
16711 {
16712 EMSG(_(e_invarg));
16713 return;
16714 }
16715 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16716 && dict_find(d, (char_u *)"pattern", -1) != NULL
16717 && dict_find(d, (char_u *)"priority", -1) != NULL
16718 && dict_find(d, (char_u *)"id", -1) != NULL))
16719 {
16720 EMSG(_(e_invarg));
16721 return;
16722 }
16723 li = li->li_next;
16724 }
16725
16726 clear_matches(curwin);
16727 li = l->lv_first;
16728 while (li != NULL)
16729 {
16730 d = li->li_tv.vval.v_dict;
16731 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16732 get_dict_string(d, (char_u *)"pattern", FALSE),
16733 (int)get_dict_number(d, (char_u *)"priority"),
16734 (int)get_dict_number(d, (char_u *)"id"));
16735 li = li->li_next;
16736 }
16737 rettv->vval.v_number = 0;
16738 }
16739#endif
16740}
16741
16742/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016743 * "setpos()" function
16744 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016745 static void
16746f_setpos(argvars, rettv)
16747 typval_T *argvars;
16748 typval_T *rettv;
16749{
16750 pos_T pos;
16751 int fnum;
16752 char_u *name;
16753
Bram Moolenaar08250432008-02-13 11:42:46 +000016754 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016755 name = get_tv_string_chk(argvars);
16756 if (name != NULL)
16757 {
16758 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16759 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016760 if (--pos.col < 0)
16761 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016762 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016763 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016764 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016765 if (fnum == curbuf->b_fnum)
16766 {
16767 curwin->w_cursor = pos;
16768 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016769 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016770 }
16771 else
16772 EMSG(_(e_invarg));
16773 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016774 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16775 {
16776 /* set mark */
16777 if (setmark_pos(name[1], &pos, fnum) == OK)
16778 rettv->vval.v_number = 0;
16779 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016780 else
16781 EMSG(_(e_invarg));
16782 }
16783 }
16784}
16785
16786/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016787 * "setqflist()" function
16788 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016789 static void
16790f_setqflist(argvars, rettv)
16791 typval_T *argvars;
16792 typval_T *rettv;
16793{
16794 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16795}
16796
16797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 * "setreg()" function
16799 */
16800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 typval_T *argvars;
16803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804{
16805 int regname;
16806 char_u *strregname;
16807 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016808 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 int append;
16810 char_u yank_type;
16811 long block_len;
16812
16813 block_len = -1;
16814 yank_type = MAUTO;
16815 append = FALSE;
16816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016817 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016818 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016820 if (strregname == NULL)
16821 return; /* type error; errmsg already given */
16822 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 if (regname == 0 || regname == '@')
16824 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016826 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016828 stropt = get_tv_string_chk(&argvars[2]);
16829 if (stropt == NULL)
16830 return; /* type error */
16831 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 switch (*stropt)
16833 {
16834 case 'a': case 'A': /* append */
16835 append = TRUE;
16836 break;
16837 case 'v': case 'c': /* character-wise selection */
16838 yank_type = MCHAR;
16839 break;
16840 case 'V': case 'l': /* line-wise selection */
16841 yank_type = MLINE;
16842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 case 'b': case Ctrl_V: /* block-wise selection */
16844 yank_type = MBLOCK;
16845 if (VIM_ISDIGIT(stropt[1]))
16846 {
16847 ++stropt;
16848 block_len = getdigits(&stropt) - 1;
16849 --stropt;
16850 }
16851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 }
16853 }
16854
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016855 if (argvars[1].v_type == VAR_LIST)
16856 {
16857 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016858 char_u **allocval;
16859 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016860 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016861 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016862 int len = argvars[1].vval.v_list->lv_len;
16863 listitem_T *li;
16864
Bram Moolenaar7d647822014-04-05 21:28:56 +020016865 /* First half: use for pointers to result lines; second half: use for
16866 * pointers to allocated copies. */
16867 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016868 if (lstval == NULL)
16869 return;
16870 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016871 allocval = lstval + len + 2;
16872 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016873
16874 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16875 li = li->li_next)
16876 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016877 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016878 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016879 goto free_lstval;
16880 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016881 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016882 /* Need to make a copy, next get_tv_string_buf_chk() will
16883 * overwrite the string. */
16884 strval = vim_strsave(buf);
16885 if (strval == NULL)
16886 goto free_lstval;
16887 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016888 }
16889 *curval++ = strval;
16890 }
16891 *curval++ = NULL;
16892
16893 write_reg_contents_lst(regname, lstval, -1,
16894 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016895free_lstval:
16896 while (curallocval > allocval)
16897 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016898 vim_free(lstval);
16899 }
16900 else
16901 {
16902 strval = get_tv_string_chk(&argvars[1]);
16903 if (strval == NULL)
16904 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016908 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909}
16910
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016911/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016912 * "settabvar()" function
16913 */
16914 static void
16915f_settabvar(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016919#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016920 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016921 tabpage_T *tp;
16922#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016923 char_u *varname, *tabvarname;
16924 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016925
16926 rettv->vval.v_number = 0;
16927
16928 if (check_restricted() || check_secure())
16929 return;
16930
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016931#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016932 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016933#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016934 varname = get_tv_string_chk(&argvars[1]);
16935 varp = &argvars[2];
16936
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016937 if (varname != NULL && varp != NULL
16938#ifdef FEAT_WINDOWS
16939 && tp != NULL
16940#endif
16941 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016942 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016943#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016944 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016945 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016946#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016947
16948 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16949 if (tabvarname != NULL)
16950 {
16951 STRCPY(tabvarname, "t:");
16952 STRCPY(tabvarname + 2, varname);
16953 set_var(tabvarname, varp, TRUE);
16954 vim_free(tabvarname);
16955 }
16956
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016957#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016958 /* Restore current tabpage */
16959 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016960 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016961#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016962 }
16963}
16964
16965/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016966 * "settabwinvar()" function
16967 */
16968 static void
16969f_settabwinvar(argvars, rettv)
16970 typval_T *argvars;
16971 typval_T *rettv;
16972{
16973 setwinvar(argvars, rettv, 1);
16974}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975
16976/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016977 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016980f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 typval_T *argvars;
16982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016984 setwinvar(argvars, rettv, 0);
16985}
16986
16987/*
16988 * "setwinvar()" and "settabwinvar()" functions
16989 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016990
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016991 static void
16992setwinvar(argvars, rettv, off)
16993 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016994 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016995 int off;
16996{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 win_T *win;
16998#ifdef FEAT_WINDOWS
16999 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017000 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001#endif
17002 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017003 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017005 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
17007 if (check_restricted() || check_secure())
17008 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017009
17010#ifdef FEAT_WINDOWS
17011 if (off == 1)
17012 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17013 else
17014 tp = curtab;
17015#endif
17016 win = find_win_by_nr(&argvars[off], tp);
17017 varname = get_tv_string_chk(&argvars[off + 1]);
17018 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019
17020 if (win != NULL && varname != NULL && varp != NULL)
17021 {
17022#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017023 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017024 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025#endif
17026
17027 if (*varname == '&')
17028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 long numval;
17030 char_u *strval;
17031 int error = FALSE;
17032
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017034 numval = get_tv_number_chk(varp, &error);
17035 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017036 if (!error && strval != NULL)
17037 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 }
17039 else
17040 {
17041 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17042 if (winvarname != NULL)
17043 {
17044 STRCPY(winvarname, "w:");
17045 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017046 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 vim_free(winvarname);
17048 }
17049 }
17050
17051#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017052 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053#endif
17054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055}
17056
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017057#ifdef FEAT_CRYPT
17058/*
17059 * "sha256({string})" function
17060 */
17061 static void
17062f_sha256(argvars, rettv)
17063 typval_T *argvars;
17064 typval_T *rettv;
17065{
17066 char_u *p;
17067
17068 p = get_tv_string(&argvars[0]);
17069 rettv->vval.v_string = vim_strsave(
17070 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17071 rettv->v_type = VAR_STRING;
17072}
17073#endif /* FEAT_CRYPT */
17074
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017076 * "shellescape({string})" function
17077 */
17078 static void
17079f_shellescape(argvars, rettv)
17080 typval_T *argvars;
17081 typval_T *rettv;
17082{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017083 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017084 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017085 rettv->v_type = VAR_STRING;
17086}
17087
17088/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017089 * shiftwidth() function
17090 */
17091 static void
17092f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017093 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017094 typval_T *rettv;
17095{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017096 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017097}
17098
17099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 */
17102 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017104 typval_T *argvars;
17105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 p = get_tv_string(&argvars[0]);
17110 rettv->vval.v_string = vim_strsave(p);
17111 simplify_filename(rettv->vval.v_string); /* simplify in place */
17112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113}
17114
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017115#ifdef FEAT_FLOAT
17116/*
17117 * "sin()" function
17118 */
17119 static void
17120f_sin(argvars, rettv)
17121 typval_T *argvars;
17122 typval_T *rettv;
17123{
17124 float_T f;
17125
17126 rettv->v_type = VAR_FLOAT;
17127 if (get_float_arg(argvars, &f) == OK)
17128 rettv->vval.v_float = sin(f);
17129 else
17130 rettv->vval.v_float = 0.0;
17131}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017132
17133/*
17134 * "sinh()" function
17135 */
17136 static void
17137f_sinh(argvars, rettv)
17138 typval_T *argvars;
17139 typval_T *rettv;
17140{
17141 float_T f;
17142
17143 rettv->v_type = VAR_FLOAT;
17144 if (get_float_arg(argvars, &f) == OK)
17145 rettv->vval.v_float = sinh(f);
17146 else
17147 rettv->vval.v_float = 0.0;
17148}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017149#endif
17150
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151static int
17152#ifdef __BORLANDC__
17153 _RTLENTRYF
17154#endif
17155 item_compare __ARGS((const void *s1, const void *s2));
17156static int
17157#ifdef __BORLANDC__
17158 _RTLENTRYF
17159#endif
17160 item_compare2 __ARGS((const void *s1, const void *s2));
17161
17162static int item_compare_ic;
17163static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017164static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017166static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167#define ITEM_COMPARE_FAIL 999
17168
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017170 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 static int
17173#ifdef __BORLANDC__
17174_RTLENTRYF
17175#endif
17176item_compare(s1, s2)
17177 const void *s1;
17178 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 char_u *p1, *p2;
17181 char_u *tofree1, *tofree2;
17182 int res;
17183 char_u numbuf1[NUMBUFLEN];
17184 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017186 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17187 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017188 if (p1 == NULL)
17189 p1 = (char_u *)"";
17190 if (p2 == NULL)
17191 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017192 if (item_compare_ic)
17193 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 res = STRCMP(p1, p2);
17196 vim_free(tofree1);
17197 vim_free(tofree2);
17198 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199}
17200
17201 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202#ifdef __BORLANDC__
17203_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205item_compare2(s1, s2)
17206 const void *s1;
17207 const void *s2;
17208{
17209 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017210 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017211 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 /* shortcut after failure in previous call; compare all items equal */
17215 if (item_compare_func_err)
17216 return 0;
17217
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017218 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17219 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17221 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222
17223 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017224 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017225 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17226 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 clear_tv(&argv[0]);
17228 clear_tv(&argv[1]);
17229
17230 if (res == FAIL)
17231 res = ITEM_COMPARE_FAIL;
17232 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17234 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017235 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017236 clear_tv(&rettv);
17237 return res;
17238}
17239
17240/*
17241 * "sort({list})" function
17242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017244do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017245 typval_T *argvars;
17246 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017247 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248{
Bram Moolenaar33570922005-01-25 22:26:29 +000017249 list_T *l;
17250 listitem_T *li;
17251 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017252 long len;
17253 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254
Bram Moolenaar0d660222005-01-07 21:51:51 +000017255 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017256 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 else
17258 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017260 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017261 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262 return;
17263 rettv->vval.v_list = l;
17264 rettv->v_type = VAR_LIST;
17265 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 len = list_len(l);
17268 if (len <= 1)
17269 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271 item_compare_ic = FALSE;
17272 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017273 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 if (argvars[1].v_type != VAR_UNKNOWN)
17275 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017276 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017277 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017278 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017279 else
17280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017281 int error = FALSE;
17282
17283 i = get_tv_number_chk(&argvars[1], &error);
17284 if (error)
17285 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286 if (i == 1)
17287 item_compare_ic = TRUE;
17288 else
17289 item_compare_func = get_tv_string(&argvars[1]);
17290 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017291
17292 if (argvars[2].v_type != VAR_UNKNOWN)
17293 {
17294 /* optional third argument: {dict} */
17295 if (argvars[2].v_type != VAR_DICT)
17296 {
17297 EMSG(_(e_dictreq));
17298 return;
17299 }
17300 item_compare_selfdict = argvars[2].vval.v_dict;
17301 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306 if (ptrs == NULL)
17307 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308
Bram Moolenaar327aa022014-03-25 18:24:23 +010017309 i = 0;
17310 if (sort)
17311 {
17312 /* sort(): ptrs will be the list to sort */
17313 for (li = l->lv_first; li != NULL; li = li->li_next)
17314 ptrs[i++] = li;
17315
17316 item_compare_func_err = FALSE;
17317 /* test the compare function */
17318 if (item_compare_func != NULL
17319 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017321 EMSG(_("E702: Sort compare function failed"));
17322 else
17323 {
17324 /* Sort the array with item pointers. */
17325 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17326 item_compare_func == NULL ? item_compare : item_compare2);
17327
17328 if (!item_compare_func_err)
17329 {
17330 /* Clear the List and append the items in sorted order. */
17331 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17332 l->lv_len = 0;
17333 for (i = 0; i < len; ++i)
17334 list_append(l, ptrs[i]);
17335 }
17336 }
17337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017340 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17341
17342 /* f_uniq(): ptrs will be a stack of items to remove */
17343 item_compare_func_err = FALSE;
17344 item_compare_func_ptr = item_compare_func
17345 ? item_compare2 : item_compare;
17346
17347 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17348 li = li->li_next)
17349 {
17350 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17351 == 0)
17352 ptrs[i++] = li;
17353 if (item_compare_func_err)
17354 {
17355 EMSG(_("E882: Uniq compare function failed"));
17356 break;
17357 }
17358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017360 if (!item_compare_func_err)
17361 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017362 while (--i >= 0)
17363 {
17364 li = ptrs[i]->li_next;
17365 ptrs[i]->li_next = li->li_next;
17366 if (li->li_next != NULL)
17367 li->li_next->li_prev = ptrs[i];
17368 else
17369 l->lv_last = ptrs[i];
17370 list_fix_watch(l, li);
17371 listitem_free(li);
17372 l->lv_len--;
17373 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017374 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375 }
17376
17377 vim_free(ptrs);
17378 }
17379}
17380
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017381/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017382 * "sort({list})" function
17383 */
17384 static void
17385f_sort(argvars, rettv)
17386 typval_T *argvars;
17387 typval_T *rettv;
17388{
17389 do_sort_uniq(argvars, rettv, TRUE);
17390}
17391
17392/*
17393 * "uniq({list})" function
17394 */
17395 static void
17396f_uniq(argvars, rettv)
17397 typval_T *argvars;
17398 typval_T *rettv;
17399{
17400 do_sort_uniq(argvars, rettv, FALSE);
17401}
17402
17403/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017404 * "soundfold({word})" function
17405 */
17406 static void
17407f_soundfold(argvars, rettv)
17408 typval_T *argvars;
17409 typval_T *rettv;
17410{
17411 char_u *s;
17412
17413 rettv->v_type = VAR_STRING;
17414 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017415#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017416 rettv->vval.v_string = eval_soundfold(s);
17417#else
17418 rettv->vval.v_string = vim_strsave(s);
17419#endif
17420}
17421
17422/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017423 * "spellbadword()" function
17424 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017425 static void
17426f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017427 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017428 typval_T *rettv;
17429{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017430 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017431 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017432 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017433
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017434 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017435 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017436
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017437#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017438 if (argvars[0].v_type == VAR_UNKNOWN)
17439 {
17440 /* Find the start and length of the badly spelled word. */
17441 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17442 if (len != 0)
17443 word = ml_get_cursor();
17444 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017445 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017446 {
17447 char_u *str = get_tv_string_chk(&argvars[0]);
17448 int capcol = -1;
17449
17450 if (str != NULL)
17451 {
17452 /* Check the argument for spelling. */
17453 while (*str != NUL)
17454 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017455 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017456 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017457 {
17458 word = str;
17459 break;
17460 }
17461 str += len;
17462 }
17463 }
17464 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017465#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017467 list_append_string(rettv->vval.v_list, word, len);
17468 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017469 attr == HLF_SPB ? "bad" :
17470 attr == HLF_SPR ? "rare" :
17471 attr == HLF_SPL ? "local" :
17472 attr == HLF_SPC ? "caps" :
17473 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474}
17475
17476/*
17477 * "spellsuggest()" function
17478 */
17479 static void
17480f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017481 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017482 typval_T *rettv;
17483{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017484#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017485 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017486 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017487 int maxcount;
17488 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017489 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017490 listitem_T *li;
17491 int need_capital = FALSE;
17492#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017494 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017495 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017496
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017497#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017498 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017499 {
17500 str = get_tv_string(&argvars[0]);
17501 if (argvars[1].v_type != VAR_UNKNOWN)
17502 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017503 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017504 if (maxcount <= 0)
17505 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017506 if (argvars[2].v_type != VAR_UNKNOWN)
17507 {
17508 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17509 if (typeerr)
17510 return;
17511 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017512 }
17513 else
17514 maxcount = 25;
17515
Bram Moolenaar4770d092006-01-12 23:22:24 +000017516 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017517
17518 for (i = 0; i < ga.ga_len; ++i)
17519 {
17520 str = ((char_u **)ga.ga_data)[i];
17521
17522 li = listitem_alloc();
17523 if (li == NULL)
17524 vim_free(str);
17525 else
17526 {
17527 li->li_tv.v_type = VAR_STRING;
17528 li->li_tv.v_lock = 0;
17529 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017530 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017531 }
17532 }
17533 ga_clear(&ga);
17534 }
17535#endif
17536}
17537
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017539f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 typval_T *argvars;
17541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542{
17543 char_u *str;
17544 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017545 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 regmatch_T regmatch;
17547 char_u patbuf[NUMBUFLEN];
17548 char_u *save_cpo;
17549 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017551 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017552 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553
17554 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17555 save_cpo = p_cpo;
17556 p_cpo = (char_u *)"";
17557
17558 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017559 if (argvars[1].v_type != VAR_UNKNOWN)
17560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17562 if (pat == NULL)
17563 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017564 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017566 }
17567 if (pat == NULL || *pat == NUL)
17568 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017570 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017572 if (typeerr)
17573 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17576 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017579 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017580 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017581 if (*str == NUL)
17582 match = FALSE; /* empty item at the end */
17583 else
17584 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 if (match)
17586 end = regmatch.startp[0];
17587 else
17588 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017589 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17590 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017591 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017592 if (list_append_string(rettv->vval.v_list, str,
17593 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 }
17596 if (!match)
17597 break;
17598 /* Advance to just after the match. */
17599 if (regmatch.endp[0] > str)
17600 col = 0;
17601 else
17602 {
17603 /* Don't get stuck at the same match. */
17604#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017605 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606#else
17607 col = 1;
17608#endif
17609 }
17610 str = regmatch.endp[0];
17611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
Bram Moolenaar473de612013-06-08 18:19:48 +020017613 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617}
17618
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017619#ifdef FEAT_FLOAT
17620/*
17621 * "sqrt()" function
17622 */
17623 static void
17624f_sqrt(argvars, rettv)
17625 typval_T *argvars;
17626 typval_T *rettv;
17627{
17628 float_T f;
17629
17630 rettv->v_type = VAR_FLOAT;
17631 if (get_float_arg(argvars, &f) == OK)
17632 rettv->vval.v_float = sqrt(f);
17633 else
17634 rettv->vval.v_float = 0.0;
17635}
17636
17637/*
17638 * "str2float()" function
17639 */
17640 static void
17641f_str2float(argvars, rettv)
17642 typval_T *argvars;
17643 typval_T *rettv;
17644{
17645 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17646
17647 if (*p == '+')
17648 p = skipwhite(p + 1);
17649 (void)string2float(p, &rettv->vval.v_float);
17650 rettv->v_type = VAR_FLOAT;
17651}
17652#endif
17653
Bram Moolenaar2c932302006-03-18 21:42:09 +000017654/*
17655 * "str2nr()" function
17656 */
17657 static void
17658f_str2nr(argvars, rettv)
17659 typval_T *argvars;
17660 typval_T *rettv;
17661{
17662 int base = 10;
17663 char_u *p;
17664 long n;
17665
17666 if (argvars[1].v_type != VAR_UNKNOWN)
17667 {
17668 base = get_tv_number(&argvars[1]);
17669 if (base != 8 && base != 10 && base != 16)
17670 {
17671 EMSG(_(e_invarg));
17672 return;
17673 }
17674 }
17675
17676 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017677 if (*p == '+')
17678 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017679 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17680 rettv->vval.v_number = n;
17681}
17682
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683#ifdef HAVE_STRFTIME
17684/*
17685 * "strftime({format}[, {time}])" function
17686 */
17687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 typval_T *argvars;
17690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691{
17692 char_u result_buf[256];
17693 struct tm *curtime;
17694 time_t seconds;
17695 char_u *p;
17696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017699 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017700 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 seconds = time(NULL);
17702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 curtime = localtime(&seconds);
17705 /* MSVC returns NULL for an invalid value of seconds. */
17706 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017707 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 else
17709 {
17710# ifdef FEAT_MBYTE
17711 vimconv_T conv;
17712 char_u *enc;
17713
17714 conv.vc_type = CONV_NONE;
17715 enc = enc_locale();
17716 convert_setup(&conv, p_enc, enc);
17717 if (conv.vc_type != CONV_NONE)
17718 p = string_convert(&conv, p, NULL);
17719# endif
17720 if (p != NULL)
17721 (void)strftime((char *)result_buf, sizeof(result_buf),
17722 (char *)p, curtime);
17723 else
17724 result_buf[0] = NUL;
17725
17726# ifdef FEAT_MBYTE
17727 if (conv.vc_type != CONV_NONE)
17728 vim_free(p);
17729 convert_setup(&conv, enc, p_enc);
17730 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017731 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 else
17733# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735
17736# ifdef FEAT_MBYTE
17737 /* Release conversion descriptors */
17738 convert_setup(&conv, NULL, NULL);
17739 vim_free(enc);
17740# endif
17741 }
17742}
17743#endif
17744
17745/*
17746 * "stridx()" function
17747 */
17748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 typval_T *argvars;
17751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752{
17753 char_u buf[NUMBUFLEN];
17754 char_u *needle;
17755 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017756 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 needle = get_tv_string_chk(&argvars[1]);
17761 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763 if (needle == NULL || haystack == NULL)
17764 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 if (argvars[2].v_type != VAR_UNKNOWN)
17767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 int error = FALSE;
17769
17770 start_idx = get_tv_number_chk(&argvars[2], &error);
17771 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017773 if (start_idx >= 0)
17774 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 }
17776
17777 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17778 if (pos != NULL)
17779 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780}
17781
17782/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017783 * "string()" function
17784 */
17785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017786f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017787 typval_T *argvars;
17788 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017789{
17790 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017791 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017793 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017794 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017795 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017796 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798}
17799
17800/*
17801 * "strlen()" function
17802 */
17803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017804f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 typval_T *argvars;
17806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017808 rettv->vval.v_number = (varnumber_T)(STRLEN(
17809 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810}
17811
17812/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017813 * "strchars()" function
17814 */
17815 static void
17816f_strchars(argvars, rettv)
17817 typval_T *argvars;
17818 typval_T *rettv;
17819{
17820 char_u *s = get_tv_string(&argvars[0]);
17821#ifdef FEAT_MBYTE
17822 varnumber_T len = 0;
17823
17824 while (*s != NUL)
17825 {
17826 mb_cptr2char_adv(&s);
17827 ++len;
17828 }
17829 rettv->vval.v_number = len;
17830#else
17831 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17832#endif
17833}
17834
17835/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017836 * "strdisplaywidth()" function
17837 */
17838 static void
17839f_strdisplaywidth(argvars, rettv)
17840 typval_T *argvars;
17841 typval_T *rettv;
17842{
17843 char_u *s = get_tv_string(&argvars[0]);
17844 int col = 0;
17845
17846 if (argvars[1].v_type != VAR_UNKNOWN)
17847 col = get_tv_number(&argvars[1]);
17848
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017849 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017850}
17851
17852/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017853 * "strwidth()" function
17854 */
17855 static void
17856f_strwidth(argvars, rettv)
17857 typval_T *argvars;
17858 typval_T *rettv;
17859{
17860 char_u *s = get_tv_string(&argvars[0]);
17861
17862 rettv->vval.v_number = (varnumber_T)(
17863#ifdef FEAT_MBYTE
17864 mb_string2cells(s, -1)
17865#else
17866 STRLEN(s)
17867#endif
17868 );
17869}
17870
17871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 * "strpart()" function
17873 */
17874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017875f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 typval_T *argvars;
17877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878{
17879 char_u *p;
17880 int n;
17881 int len;
17882 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017883 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017885 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 slen = (int)STRLEN(p);
17887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017888 n = get_tv_number_chk(&argvars[1], &error);
17889 if (error)
17890 len = 0;
17891 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017892 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 else
17894 len = slen - n; /* default len: all bytes that are available. */
17895
17896 /*
17897 * Only return the overlap between the specified part and the actual
17898 * string.
17899 */
17900 if (n < 0)
17901 {
17902 len += n;
17903 n = 0;
17904 }
17905 else if (n > slen)
17906 n = slen;
17907 if (len < 0)
17908 len = 0;
17909 else if (n + len > slen)
17910 len = slen - n;
17911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017912 rettv->v_type = VAR_STRING;
17913 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914}
17915
17916/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 * "strridx()" function
17918 */
17919 static void
17920f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017921 typval_T *argvars;
17922 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923{
17924 char_u buf[NUMBUFLEN];
17925 char_u *needle;
17926 char_u *haystack;
17927 char_u *rest;
17928 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017929 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017931 needle = get_tv_string_chk(&argvars[1]);
17932 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933
17934 rettv->vval.v_number = -1;
17935 if (needle == NULL || haystack == NULL)
17936 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017937
17938 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017939 if (argvars[2].v_type != VAR_UNKNOWN)
17940 {
17941 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017942 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017943 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017944 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017945 }
17946 else
17947 end_idx = haystack_len;
17948
Bram Moolenaar0d660222005-01-07 21:51:51 +000017949 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017950 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017952 lastmatch = haystack + end_idx;
17953 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956 for (rest = haystack; *rest != '\0'; ++rest)
17957 {
17958 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017959 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 break;
17961 lastmatch = rest;
17962 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964
17965 if (lastmatch == NULL)
17966 rettv->vval.v_number = -1;
17967 else
17968 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17969}
17970
17971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 * "strtrans()" function
17973 */
17974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017975f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017976 typval_T *argvars;
17977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017979 rettv->v_type = VAR_STRING;
17980 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981}
17982
17983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017984 * "submatch()" function
17985 */
17986 static void
17987f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017988 typval_T *argvars;
17989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990{
Bram Moolenaar41571762014-04-02 19:00:58 +020017991 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017992 int no;
17993 int retList = 0;
17994
17995 no = (int)get_tv_number_chk(&argvars[0], &error);
17996 if (error)
17997 return;
17998 error = FALSE;
17999 if (argvars[1].v_type != VAR_UNKNOWN)
18000 retList = get_tv_number_chk(&argvars[1], &error);
18001 if (error)
18002 return;
18003
18004 if (retList == 0)
18005 {
18006 rettv->v_type = VAR_STRING;
18007 rettv->vval.v_string = reg_submatch(no);
18008 }
18009 else
18010 {
18011 rettv->v_type = VAR_LIST;
18012 rettv->vval.v_list = reg_submatch_list(no);
18013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014}
18015
18016/*
18017 * "substitute()" function
18018 */
18019 static void
18020f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018021 typval_T *argvars;
18022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023{
18024 char_u patbuf[NUMBUFLEN];
18025 char_u subbuf[NUMBUFLEN];
18026 char_u flagsbuf[NUMBUFLEN];
18027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018028 char_u *str = get_tv_string_chk(&argvars[0]);
18029 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18030 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18031 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18032
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018034 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18035 rettv->vval.v_string = NULL;
18036 else
18037 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038}
18039
18040/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018041 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047{
18048 int id = 0;
18049#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018050 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 long col;
18052 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018053 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018055 lnum = get_tv_lnum(argvars); /* -1 on type error */
18056 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18057 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018060 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018061 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062#endif
18063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018064 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065}
18066
18067/*
18068 * "synIDattr(id, what [, mode])" function
18069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018071f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018072 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 char_u *p = NULL;
18076#ifdef FEAT_SYN_HL
18077 int id;
18078 char_u *what;
18079 char_u *mode;
18080 char_u modebuf[NUMBUFLEN];
18081 int modec;
18082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018083 id = get_tv_number(&argvars[0]);
18084 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018085 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018089 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 modec = 0; /* replace invalid with current */
18091 }
18092 else
18093 {
18094#ifdef FEAT_GUI
18095 if (gui.in_use)
18096 modec = 'g';
18097 else
18098#endif
18099 if (t_colors > 1)
18100 modec = 'c';
18101 else
18102 modec = 't';
18103 }
18104
18105
18106 switch (TOLOWER_ASC(what[0]))
18107 {
18108 case 'b':
18109 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18110 p = highlight_color(id, what, modec);
18111 else /* bold */
18112 p = highlight_has_attr(id, HL_BOLD, modec);
18113 break;
18114
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018115 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 p = highlight_color(id, what, modec);
18117 break;
18118
18119 case 'i':
18120 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18121 p = highlight_has_attr(id, HL_INVERSE, modec);
18122 else /* italic */
18123 p = highlight_has_attr(id, HL_ITALIC, modec);
18124 break;
18125
18126 case 'n': /* name */
18127 p = get_highlight_name(NULL, id - 1);
18128 break;
18129
18130 case 'r': /* reverse */
18131 p = highlight_has_attr(id, HL_INVERSE, modec);
18132 break;
18133
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018134 case 's':
18135 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18136 p = highlight_color(id, what, modec);
18137 else /* standout */
18138 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 break;
18140
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018141 case 'u':
18142 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18143 /* underline */
18144 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18145 else
18146 /* undercurl */
18147 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 break;
18149 }
18150
18151 if (p != NULL)
18152 p = vim_strsave(p);
18153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018154 rettv->v_type = VAR_STRING;
18155 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158/*
18159 * "synIDtrans(id)" function
18160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165{
18166 int id;
18167
18168#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170
18171 if (id > 0)
18172 id = syn_get_final_id(id);
18173 else
18174#endif
18175 id = 0;
18176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178}
18179
18180/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018181 * "synconcealed(lnum, col)" function
18182 */
18183 static void
18184f_synconcealed(argvars, rettv)
18185 typval_T *argvars UNUSED;
18186 typval_T *rettv;
18187{
18188#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18189 long lnum;
18190 long col;
18191 int syntax_flags = 0;
18192 int cchar;
18193 int matchid = 0;
18194 char_u str[NUMBUFLEN];
18195#endif
18196
18197 rettv->v_type = VAR_LIST;
18198 rettv->vval.v_list = NULL;
18199
18200#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18201 lnum = get_tv_lnum(argvars); /* -1 on type error */
18202 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18203
18204 vim_memset(str, NUL, sizeof(str));
18205
18206 if (rettv_list_alloc(rettv) != FAIL)
18207 {
18208 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18209 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18210 && curwin->w_p_cole > 0)
18211 {
18212 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18213 syntax_flags = get_syntax_info(&matchid);
18214
18215 /* get the conceal character */
18216 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18217 {
18218 cchar = syn_get_sub_char();
18219 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18220 cchar = lcs_conceal;
18221 if (cchar != NUL)
18222 {
18223# ifdef FEAT_MBYTE
18224 if (has_mbyte)
18225 (*mb_char2bytes)(cchar, str);
18226 else
18227# endif
18228 str[0] = cchar;
18229 }
18230 }
18231 }
18232
18233 list_append_number(rettv->vval.v_list,
18234 (syntax_flags & HL_CONCEAL) != 0);
18235 /* -1 to auto-determine strlen */
18236 list_append_string(rettv->vval.v_list, str, -1);
18237 list_append_number(rettv->vval.v_list, matchid);
18238 }
18239#endif
18240}
18241
18242/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018243 * "synstack(lnum, col)" function
18244 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018245 static void
18246f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018247 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018248 typval_T *rettv;
18249{
18250#ifdef FEAT_SYN_HL
18251 long lnum;
18252 long col;
18253 int i;
18254 int id;
18255#endif
18256
18257 rettv->v_type = VAR_LIST;
18258 rettv->vval.v_list = NULL;
18259
18260#ifdef FEAT_SYN_HL
18261 lnum = get_tv_lnum(argvars); /* -1 on type error */
18262 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18263
18264 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018265 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018266 && rettv_list_alloc(rettv) != FAIL)
18267 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018268 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018269 for (i = 0; ; ++i)
18270 {
18271 id = syn_get_stack_item(i);
18272 if (id < 0)
18273 break;
18274 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18275 break;
18276 }
18277 }
18278#endif
18279}
18280
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018282get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018283 typval_T *argvars;
18284 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018285 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018287 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018289 char_u *infile = NULL;
18290 char_u buf[NUMBUFLEN];
18291 int err = FALSE;
18292 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018293 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018295 rettv->v_type = VAR_STRING;
18296 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018297 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018298 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018301 {
18302 /*
18303 * Write the string to a temp file, to be used for input of the shell
18304 * command.
18305 */
18306 if ((infile = vim_tempname('i')) == NULL)
18307 {
18308 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018309 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018310 }
18311
18312 fd = mch_fopen((char *)infile, WRITEBIN);
18313 if (fd == NULL)
18314 {
18315 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018316 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018317 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018318 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018319 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018320 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18321 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018322 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018323 else
18324 {
18325 p = get_tv_string_buf_chk(&argvars[1], buf);
18326 if (p == NULL)
18327 {
18328 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018329 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018330 }
18331 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18332 err = TRUE;
18333 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018334 if (fclose(fd) != 0)
18335 err = TRUE;
18336 if (err)
18337 {
18338 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018339 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018340 }
18341 }
18342
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018343 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018345 int len;
18346 listitem_T *li;
18347 char_u *s = NULL;
18348 char_u *start;
18349 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018350 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018352 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18353 SHELL_SILENT | SHELL_COOKED, &len);
18354 if (res == NULL)
18355 goto errret;
18356
18357 list = list_alloc();
18358 if (list == NULL)
18359 goto errret;
18360
18361 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018363 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018364 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018365 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018366 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018367
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018368 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018369 if (s == NULL)
18370 goto errret;
18371
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018372 for (p = s; start < end; ++p, ++start)
18373 *p = *start == NUL ? NL : *start;
18374 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018375
18376 li = listitem_alloc();
18377 if (li == NULL)
18378 {
18379 vim_free(s);
18380 goto errret;
18381 }
18382 li->li_tv.v_type = VAR_STRING;
18383 li->li_tv.vval.v_string = s;
18384 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018386
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018387 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018388 rettv->v_type = VAR_LIST;
18389 rettv->vval.v_list = list;
18390 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018392 else
18393 {
18394 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18395 SHELL_SILENT | SHELL_COOKED, NULL);
18396#ifdef USE_CR
18397 /* translate <CR> into <NL> */
18398 if (res != NULL)
18399 {
18400 char_u *s;
18401
18402 for (s = res; *s; ++s)
18403 {
18404 if (*s == CAR)
18405 *s = NL;
18406 }
18407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408#else
18409# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018410 /* translate <CR><NL> into <NL> */
18411 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018413 char_u *s, *d;
18414
18415 d = res;
18416 for (s = res; *s; ++s)
18417 {
18418 if (s[0] == CAR && s[1] == NL)
18419 ++s;
18420 *d++ = *s;
18421 }
18422 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424# endif
18425#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018426 rettv->vval.v_string = res;
18427 res = NULL;
18428 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018429
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018430errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018431 if (infile != NULL)
18432 {
18433 mch_remove(infile);
18434 vim_free(infile);
18435 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018436 if (res != NULL)
18437 vim_free(res);
18438 if (list != NULL)
18439 list_free(list, TRUE);
18440}
18441
18442/*
18443 * "system()" function
18444 */
18445 static void
18446f_system(argvars, rettv)
18447 typval_T *argvars;
18448 typval_T *rettv;
18449{
18450 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18451}
18452
18453/*
18454 * "systemlist()" function
18455 */
18456 static void
18457f_systemlist(argvars, rettv)
18458 typval_T *argvars;
18459 typval_T *rettv;
18460{
18461 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462}
18463
18464/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018465 * "tabpagebuflist()" function
18466 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018467 static void
18468f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018469 typval_T *argvars UNUSED;
18470 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018471{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018472#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018473 tabpage_T *tp;
18474 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018475
18476 if (argvars[0].v_type == VAR_UNKNOWN)
18477 wp = firstwin;
18478 else
18479 {
18480 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18481 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018482 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018483 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018484 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018485 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018486 for (; wp != NULL; wp = wp->w_next)
18487 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018488 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018489 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018490 }
18491#endif
18492}
18493
18494
18495/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018496 * "tabpagenr()" function
18497 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018498 static void
18499f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018500 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018501 typval_T *rettv;
18502{
18503 int nr = 1;
18504#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018505 char_u *arg;
18506
18507 if (argvars[0].v_type != VAR_UNKNOWN)
18508 {
18509 arg = get_tv_string_chk(&argvars[0]);
18510 nr = 0;
18511 if (arg != NULL)
18512 {
18513 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018514 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018515 else
18516 EMSG2(_(e_invexpr2), arg);
18517 }
18518 }
18519 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018520 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018521#endif
18522 rettv->vval.v_number = nr;
18523}
18524
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018525
18526#ifdef FEAT_WINDOWS
18527static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18528
18529/*
18530 * Common code for tabpagewinnr() and winnr().
18531 */
18532 static int
18533get_winnr(tp, argvar)
18534 tabpage_T *tp;
18535 typval_T *argvar;
18536{
18537 win_T *twin;
18538 int nr = 1;
18539 win_T *wp;
18540 char_u *arg;
18541
18542 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18543 if (argvar->v_type != VAR_UNKNOWN)
18544 {
18545 arg = get_tv_string_chk(argvar);
18546 if (arg == NULL)
18547 nr = 0; /* type error; errmsg already given */
18548 else if (STRCMP(arg, "$") == 0)
18549 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18550 else if (STRCMP(arg, "#") == 0)
18551 {
18552 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18553 if (twin == NULL)
18554 nr = 0;
18555 }
18556 else
18557 {
18558 EMSG2(_(e_invexpr2), arg);
18559 nr = 0;
18560 }
18561 }
18562
18563 if (nr > 0)
18564 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18565 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018566 {
18567 if (wp == NULL)
18568 {
18569 /* didn't find it in this tabpage */
18570 nr = 0;
18571 break;
18572 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018573 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018574 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018575 return nr;
18576}
18577#endif
18578
18579/*
18580 * "tabpagewinnr()" function
18581 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018582 static void
18583f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018584 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018585 typval_T *rettv;
18586{
18587 int nr = 1;
18588#ifdef FEAT_WINDOWS
18589 tabpage_T *tp;
18590
18591 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18592 if (tp == NULL)
18593 nr = 0;
18594 else
18595 nr = get_winnr(tp, &argvars[1]);
18596#endif
18597 rettv->vval.v_number = nr;
18598}
18599
18600
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018601/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018602 * "tagfiles()" function
18603 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018604 static void
18605f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018606 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018607 typval_T *rettv;
18608{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018609 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018610 tagname_T tn;
18611 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018613 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018614 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018615 fname = alloc(MAXPATHL);
18616 if (fname == NULL)
18617 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018618
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018619 for (first = TRUE; ; first = FALSE)
18620 if (get_tagfname(&tn, first, fname) == FAIL
18621 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018622 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018623 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018624 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018625}
18626
18627/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018628 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018629 */
18630 static void
18631f_taglist(argvars, rettv)
18632 typval_T *argvars;
18633 typval_T *rettv;
18634{
18635 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018636
18637 tag_pattern = get_tv_string(&argvars[0]);
18638
18639 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640 if (*tag_pattern == NUL)
18641 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018642
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018643 if (rettv_list_alloc(rettv) == OK)
18644 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018645}
18646
18647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 * "tempname()" function
18649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654{
18655 static int x = 'A';
18656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 rettv->v_type = VAR_STRING;
18658 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659
18660 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18661 * names. Skip 'I' and 'O', they are used for shell redirection. */
18662 do
18663 {
18664 if (x == 'Z')
18665 x = '0';
18666 else if (x == '9')
18667 x = 'A';
18668 else
18669 {
18670#ifdef EBCDIC
18671 if (x == 'I')
18672 x = 'J';
18673 else if (x == 'R')
18674 x = 'S';
18675 else
18676#endif
18677 ++x;
18678 }
18679 } while (x == 'I' || x == 'O');
18680}
18681
18682/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018683 * "test(list)" function: Just checking the walls...
18684 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018685 static void
18686f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018687 typval_T *argvars UNUSED;
18688 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018689{
18690 /* Used for unit testing. Change the code below to your liking. */
18691#if 0
18692 listitem_T *li;
18693 list_T *l;
18694 char_u *bad, *good;
18695
18696 if (argvars[0].v_type != VAR_LIST)
18697 return;
18698 l = argvars[0].vval.v_list;
18699 if (l == NULL)
18700 return;
18701 li = l->lv_first;
18702 if (li == NULL)
18703 return;
18704 bad = get_tv_string(&li->li_tv);
18705 li = li->li_next;
18706 if (li == NULL)
18707 return;
18708 good = get_tv_string(&li->li_tv);
18709 rettv->vval.v_number = test_edit_score(bad, good);
18710#endif
18711}
18712
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018713#ifdef FEAT_FLOAT
18714/*
18715 * "tan()" function
18716 */
18717 static void
18718f_tan(argvars, rettv)
18719 typval_T *argvars;
18720 typval_T *rettv;
18721{
18722 float_T f;
18723
18724 rettv->v_type = VAR_FLOAT;
18725 if (get_float_arg(argvars, &f) == OK)
18726 rettv->vval.v_float = tan(f);
18727 else
18728 rettv->vval.v_float = 0.0;
18729}
18730
18731/*
18732 * "tanh()" function
18733 */
18734 static void
18735f_tanh(argvars, rettv)
18736 typval_T *argvars;
18737 typval_T *rettv;
18738{
18739 float_T f;
18740
18741 rettv->v_type = VAR_FLOAT;
18742 if (get_float_arg(argvars, &f) == OK)
18743 rettv->vval.v_float = tanh(f);
18744 else
18745 rettv->vval.v_float = 0.0;
18746}
18747#endif
18748
Bram Moolenaard52d9742005-08-21 22:20:28 +000018749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 * "tolower(string)" function
18751 */
18752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 typval_T *argvars;
18755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756{
18757 char_u *p;
18758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 p = vim_strsave(get_tv_string(&argvars[0]));
18760 rettv->v_type = VAR_STRING;
18761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762
18763 if (p != NULL)
18764 while (*p != NUL)
18765 {
18766#ifdef FEAT_MBYTE
18767 int l;
18768
18769 if (enc_utf8)
18770 {
18771 int c, lc;
18772
18773 c = utf_ptr2char(p);
18774 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018775 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 /* TODO: reallocate string when byte count changes. */
18777 if (utf_char2len(lc) == l)
18778 utf_char2bytes(lc, p);
18779 p += l;
18780 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018781 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 p += l; /* skip multi-byte character */
18783 else
18784#endif
18785 {
18786 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18787 ++p;
18788 }
18789 }
18790}
18791
18792/*
18793 * "toupper(string)" function
18794 */
18795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018796f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 typval_T *argvars;
18798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018801 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802}
18803
18804/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018805 * "tr(string, fromstr, tostr)" function
18806 */
18807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018809 typval_T *argvars;
18810 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018811{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018812 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018813 char_u *fromstr;
18814 char_u *tostr;
18815 char_u *p;
18816#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018817 int inlen;
18818 int fromlen;
18819 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018820 int idx;
18821 char_u *cpstr;
18822 int cplen;
18823 int first = TRUE;
18824#endif
18825 char_u buf[NUMBUFLEN];
18826 char_u buf2[NUMBUFLEN];
18827 garray_T ga;
18828
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018829 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018830 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18831 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018832
18833 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018834 rettv->v_type = VAR_STRING;
18835 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018836 if (fromstr == NULL || tostr == NULL)
18837 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018838 ga_init2(&ga, (int)sizeof(char), 80);
18839
18840#ifdef FEAT_MBYTE
18841 if (!has_mbyte)
18842#endif
18843 /* not multi-byte: fromstr and tostr must be the same length */
18844 if (STRLEN(fromstr) != STRLEN(tostr))
18845 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018846#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018847error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018848#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018849 EMSG2(_(e_invarg2), fromstr);
18850 ga_clear(&ga);
18851 return;
18852 }
18853
18854 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018855 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018856 {
18857#ifdef FEAT_MBYTE
18858 if (has_mbyte)
18859 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018860 inlen = (*mb_ptr2len)(in_str);
18861 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018862 cplen = inlen;
18863 idx = 0;
18864 for (p = fromstr; *p != NUL; p += fromlen)
18865 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018866 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018867 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018868 {
18869 for (p = tostr; *p != NUL; p += tolen)
18870 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018871 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018872 if (idx-- == 0)
18873 {
18874 cplen = tolen;
18875 cpstr = p;
18876 break;
18877 }
18878 }
18879 if (*p == NUL) /* tostr is shorter than fromstr */
18880 goto error;
18881 break;
18882 }
18883 ++idx;
18884 }
18885
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018886 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018887 {
18888 /* Check that fromstr and tostr have the same number of
18889 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018890 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018891 first = FALSE;
18892 for (p = tostr; *p != NUL; p += tolen)
18893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018894 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018895 --idx;
18896 }
18897 if (idx != 0)
18898 goto error;
18899 }
18900
18901 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018902 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018903 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018904
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018905 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018906 }
18907 else
18908#endif
18909 {
18910 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018911 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018912 if (p != NULL)
18913 ga_append(&ga, tostr[p - fromstr]);
18914 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018915 ga_append(&ga, *in_str);
18916 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018917 }
18918 }
18919
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018920 /* add a terminating NUL */
18921 ga_grow(&ga, 1);
18922 ga_append(&ga, NUL);
18923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018925}
18926
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018927#ifdef FEAT_FLOAT
18928/*
18929 * "trunc({float})" function
18930 */
18931 static void
18932f_trunc(argvars, rettv)
18933 typval_T *argvars;
18934 typval_T *rettv;
18935{
18936 float_T f;
18937
18938 rettv->v_type = VAR_FLOAT;
18939 if (get_float_arg(argvars, &f) == OK)
18940 /* trunc() is not in C90, use floor() or ceil() instead. */
18941 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18942 else
18943 rettv->vval.v_float = 0.0;
18944}
18945#endif
18946
Bram Moolenaar8299df92004-07-10 09:47:34 +000018947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 * "type(expr)" function
18949 */
18950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018951f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 typval_T *argvars;
18953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018955 int n;
18956
18957 switch (argvars[0].v_type)
18958 {
18959 case VAR_NUMBER: n = 0; break;
18960 case VAR_STRING: n = 1; break;
18961 case VAR_FUNC: n = 2; break;
18962 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018963 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018964#ifdef FEAT_FLOAT
18965 case VAR_FLOAT: n = 5; break;
18966#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018967 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18968 }
18969 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970}
18971
18972/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018973 * "undofile(name)" function
18974 */
18975 static void
18976f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018977 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018978 typval_T *rettv;
18979{
18980 rettv->v_type = VAR_STRING;
18981#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018982 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018983 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018984
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018985 if (*fname == NUL)
18986 {
18987 /* If there is no file name there will be no undo file. */
18988 rettv->vval.v_string = NULL;
18989 }
18990 else
18991 {
18992 char_u *ffname = FullName_save(fname, FALSE);
18993
18994 if (ffname != NULL)
18995 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18996 vim_free(ffname);
18997 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018998 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018999#else
19000 rettv->vval.v_string = NULL;
19001#endif
19002}
19003
19004/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019005 * "undotree()" function
19006 */
19007 static void
19008f_undotree(argvars, rettv)
19009 typval_T *argvars UNUSED;
19010 typval_T *rettv;
19011{
19012 if (rettv_dict_alloc(rettv) == OK)
19013 {
19014 dict_T *dict = rettv->vval.v_dict;
19015 list_T *list;
19016
Bram Moolenaar730cde92010-06-27 05:18:54 +020019017 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019018 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019019 dict_add_nr_str(dict, "save_last",
19020 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019021 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19022 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019023 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019024
19025 list = list_alloc();
19026 if (list != NULL)
19027 {
19028 u_eval_tree(curbuf->b_u_oldhead, list);
19029 dict_add_list(dict, "entries", list);
19030 }
19031 }
19032}
19033
19034/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019035 * "values(dict)" function
19036 */
19037 static void
19038f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 typval_T *argvars;
19040 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019041{
19042 dict_list(argvars, rettv, 1);
19043}
19044
19045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 * "virtcol(string)" function
19047 */
19048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 typval_T *argvars;
19051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052{
19053 colnr_T vcol = 0;
19054 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019055 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019057 fp = var2fpos(&argvars[0], FALSE, &fnum);
19058 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19059 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 {
19061 getvvcol(curwin, fp, NULL, NULL, &vcol);
19062 ++vcol;
19063 }
19064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066}
19067
19068/*
19069 * "visualmode()" function
19070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019073 typval_T *argvars UNUSED;
19074 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 char_u str[2];
19077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 str[0] = curbuf->b_visual_mode_eval;
19080 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
19083 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019084 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086}
19087
19088/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019089 * "wildmenumode()" function
19090 */
19091 static void
19092f_wildmenumode(argvars, rettv)
19093 typval_T *argvars UNUSED;
19094 typval_T *rettv UNUSED;
19095{
19096#ifdef FEAT_WILDMENU
19097 if (wild_menu_showing)
19098 rettv->vval.v_number = 1;
19099#endif
19100}
19101
19102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 * "winbufnr(nr)" function
19104 */
19105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019106f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 typval_T *argvars;
19108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109{
19110 win_T *wp;
19111
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019112 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117}
19118
19119/*
19120 * "wincol()" function
19121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126{
19127 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019128 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129}
19130
19131/*
19132 * "winheight(nr)" function
19133 */
19134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *argvars;
19137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138{
19139 win_T *wp;
19140
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019141 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146}
19147
19148/*
19149 * "winline()" function
19150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155{
19156 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019157 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158}
19159
19160/*
19161 * "winnr()" function
19162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019165 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167{
19168 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019169
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019171 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174}
19175
19176/*
19177 * "winrestcmd()" function
19178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019181 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183{
19184#ifdef FEAT_WINDOWS
19185 win_T *wp;
19186 int winnr = 1;
19187 garray_T ga;
19188 char_u buf[50];
19189
19190 ga_init2(&ga, (int)sizeof(char), 70);
19191 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19192 {
19193 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19194 ga_concat(&ga, buf);
19195# ifdef FEAT_VERTSPLIT
19196 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19197 ga_concat(&ga, buf);
19198# endif
19199 ++winnr;
19200 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019201 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019207 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208}
19209
19210/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019211 * "winrestview()" function
19212 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019213 static void
19214f_winrestview(argvars, rettv)
19215 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019216 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019217{
19218 dict_T *dict;
19219
19220 if (argvars[0].v_type != VAR_DICT
19221 || (dict = argvars[0].vval.v_dict) == NULL)
19222 EMSG(_(e_invarg));
19223 else
19224 {
19225 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19226 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19227#ifdef FEAT_VIRTUALEDIT
19228 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19229#endif
19230 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019231 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019232
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019233 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019234#ifdef FEAT_DIFF
19235 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19236#endif
19237 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19238 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19239
19240 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019241 win_new_height(curwin, curwin->w_height);
19242# ifdef FEAT_VERTSPLIT
19243 win_new_width(curwin, W_WIDTH(curwin));
19244# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019245 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019246
19247 if (curwin->w_topline == 0)
19248 curwin->w_topline = 1;
19249 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19250 curwin->w_topline = curbuf->b_ml.ml_line_count;
19251#ifdef FEAT_DIFF
19252 check_topfill(curwin, TRUE);
19253#endif
19254 }
19255}
19256
19257/*
19258 * "winsaveview()" function
19259 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019260 static void
19261f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019262 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019263 typval_T *rettv;
19264{
19265 dict_T *dict;
19266
Bram Moolenaara800b422010-06-27 01:15:55 +020019267 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019268 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019269 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019270
19271 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19272 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19273#ifdef FEAT_VIRTUALEDIT
19274 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19275#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019276 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019277 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19278
19279 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19280#ifdef FEAT_DIFF
19281 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19282#endif
19283 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19284 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19285}
19286
19287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 * "winwidth(nr)" function
19289 */
19290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019292 typval_T *argvars;
19293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294{
19295 win_T *wp;
19296
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019297 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019299 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 else
19301#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019302 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019304 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305#endif
19306}
19307
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019309 * Write list of strings to file
19310 */
19311 static int
19312write_list(fd, list, binary)
19313 FILE *fd;
19314 list_T *list;
19315 int binary;
19316{
19317 listitem_T *li;
19318 int c;
19319 int ret = OK;
19320 char_u *s;
19321
19322 for (li = list->lv_first; li != NULL; li = li->li_next)
19323 {
19324 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19325 {
19326 if (*s == '\n')
19327 c = putc(NUL, fd);
19328 else
19329 c = putc(*s, fd);
19330 if (c == EOF)
19331 {
19332 ret = FAIL;
19333 break;
19334 }
19335 }
19336 if (!binary || li->li_next != NULL)
19337 if (putc('\n', fd) == EOF)
19338 {
19339 ret = FAIL;
19340 break;
19341 }
19342 if (ret == FAIL)
19343 {
19344 EMSG(_(e_write));
19345 break;
19346 }
19347 }
19348 return ret;
19349}
19350
19351/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019352 * "writefile()" function
19353 */
19354 static void
19355f_writefile(argvars, rettv)
19356 typval_T *argvars;
19357 typval_T *rettv;
19358{
19359 int binary = FALSE;
19360 char_u *fname;
19361 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019362 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019363
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019364 if (check_restricted() || check_secure())
19365 return;
19366
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019367 if (argvars[0].v_type != VAR_LIST)
19368 {
19369 EMSG2(_(e_listarg), "writefile()");
19370 return;
19371 }
19372 if (argvars[0].vval.v_list == NULL)
19373 return;
19374
19375 if (argvars[2].v_type != VAR_UNKNOWN
19376 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19377 binary = TRUE;
19378
19379 /* Always open the file in binary mode, library functions have a mind of
19380 * their own about CR-LF conversion. */
19381 fname = get_tv_string(&argvars[1]);
19382 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19383 {
19384 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19385 ret = -1;
19386 }
19387 else
19388 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019389 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19390 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019391 fclose(fd);
19392 }
19393
19394 rettv->vval.v_number = ret;
19395}
19396
19397/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019398 * "xor(expr, expr)" function
19399 */
19400 static void
19401f_xor(argvars, rettv)
19402 typval_T *argvars;
19403 typval_T *rettv;
19404{
19405 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19406 ^ get_tv_number_chk(&argvars[1], NULL);
19407}
19408
19409
19410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019412 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 */
19414 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019415var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019417 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019418 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019420 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019422 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaara5525202006-03-02 22:52:09 +000019424 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019425 if (varp->v_type == VAR_LIST)
19426 {
19427 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019428 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019429 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019430 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019431
19432 l = varp->vval.v_list;
19433 if (l == NULL)
19434 return NULL;
19435
19436 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019437 pos.lnum = list_find_nr(l, 0L, &error);
19438 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019439 return NULL; /* invalid line number */
19440
19441 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019442 pos.col = list_find_nr(l, 1L, &error);
19443 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019444 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019445 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019446
19447 /* We accept "$" for the column number: last column. */
19448 li = list_find(l, 1L);
19449 if (li != NULL && li->li_tv.v_type == VAR_STRING
19450 && li->li_tv.vval.v_string != NULL
19451 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19452 pos.col = len + 1;
19453
Bram Moolenaara5525202006-03-02 22:52:09 +000019454 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019455 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019456 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019457 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019458
Bram Moolenaara5525202006-03-02 22:52:09 +000019459#ifdef FEAT_VIRTUALEDIT
19460 /* Get the virtual offset. Defaults to zero. */
19461 pos.coladd = list_find_nr(l, 2L, &error);
19462 if (error)
19463 pos.coladd = 0;
19464#endif
19465
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019466 return &pos;
19467 }
19468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019469 name = get_tv_string_chk(varp);
19470 if (name == NULL)
19471 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019472 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019474 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19475 {
19476 if (VIsual_active)
19477 return &VIsual;
19478 return &curwin->w_cursor;
19479 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019480 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019482 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19484 return NULL;
19485 return pp;
19486 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019487
19488#ifdef FEAT_VIRTUALEDIT
19489 pos.coladd = 0;
19490#endif
19491
Bram Moolenaar477933c2007-07-17 14:32:23 +000019492 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019493 {
19494 pos.col = 0;
19495 if (name[1] == '0') /* "w0": first visible line */
19496 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019497 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019498 pos.lnum = curwin->w_topline;
19499 return &pos;
19500 }
19501 else if (name[1] == '$') /* "w$": last visible line */
19502 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019503 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019504 pos.lnum = curwin->w_botline - 1;
19505 return &pos;
19506 }
19507 }
19508 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019510 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
19512 pos.lnum = curbuf->b_ml.ml_line_count;
19513 pos.col = 0;
19514 }
19515 else
19516 {
19517 pos.lnum = curwin->w_cursor.lnum;
19518 pos.col = (colnr_T)STRLEN(ml_get_curline());
19519 }
19520 return &pos;
19521 }
19522 return NULL;
19523}
19524
19525/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019526 * Convert list in "arg" into a position and optional file number.
19527 * When "fnump" is NULL there is no file number, only 3 items.
19528 * Note that the column is passed on as-is, the caller may want to decrement
19529 * it to use 1 for the first column.
19530 * Return FAIL when conversion is not possible, doesn't check the position for
19531 * validity.
19532 */
19533 static int
19534list2fpos(arg, posp, fnump)
19535 typval_T *arg;
19536 pos_T *posp;
19537 int *fnump;
19538{
19539 list_T *l = arg->vval.v_list;
19540 long i = 0;
19541 long n;
19542
Bram Moolenaarbde35262006-07-23 20:12:24 +000019543 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19544 * when "fnump" isn't NULL and "coladd" is optional. */
19545 if (arg->v_type != VAR_LIST
19546 || l == NULL
19547 || l->lv_len < (fnump == NULL ? 2 : 3)
19548 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019549 return FAIL;
19550
19551 if (fnump != NULL)
19552 {
19553 n = list_find_nr(l, i++, NULL); /* fnum */
19554 if (n < 0)
19555 return FAIL;
19556 if (n == 0)
19557 n = curbuf->b_fnum; /* current buffer */
19558 *fnump = n;
19559 }
19560
19561 n = list_find_nr(l, i++, NULL); /* lnum */
19562 if (n < 0)
19563 return FAIL;
19564 posp->lnum = n;
19565
19566 n = list_find_nr(l, i++, NULL); /* col */
19567 if (n < 0)
19568 return FAIL;
19569 posp->col = n;
19570
19571#ifdef FEAT_VIRTUALEDIT
19572 n = list_find_nr(l, i, NULL);
19573 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019574 posp->coladd = 0;
19575 else
19576 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019577#endif
19578
19579 return OK;
19580}
19581
19582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 * Get the length of an environment variable name.
19584 * Advance "arg" to the first character after the name.
19585 * Return 0 for error.
19586 */
19587 static int
19588get_env_len(arg)
19589 char_u **arg;
19590{
19591 char_u *p;
19592 int len;
19593
19594 for (p = *arg; vim_isIDc(*p); ++p)
19595 ;
19596 if (p == *arg) /* no name found */
19597 return 0;
19598
19599 len = (int)(p - *arg);
19600 *arg = p;
19601 return len;
19602}
19603
19604/*
19605 * Get the length of the name of a function or internal variable.
19606 * "arg" is advanced to the first non-white character after the name.
19607 * Return 0 if something is wrong.
19608 */
19609 static int
19610get_id_len(arg)
19611 char_u **arg;
19612{
19613 char_u *p;
19614 int len;
19615
19616 /* Find the end of the name. */
19617 for (p = *arg; eval_isnamec(*p); ++p)
19618 ;
19619 if (p == *arg) /* no name found */
19620 return 0;
19621
19622 len = (int)(p - *arg);
19623 *arg = skipwhite(p);
19624
19625 return len;
19626}
19627
19628/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019629 * Get the length of the name of a variable or function.
19630 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019632 * Return -1 if curly braces expansion failed.
19633 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 * If the name contains 'magic' {}'s, expand them and return the
19635 * expanded name in an allocated string via 'alias' - caller must free.
19636 */
19637 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019638get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 char_u **arg;
19640 char_u **alias;
19641 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019642 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643{
19644 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 char_u *p;
19646 char_u *expr_start;
19647 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648
19649 *alias = NULL; /* default to no alias */
19650
19651 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19652 && (*arg)[2] == (int)KE_SNR)
19653 {
19654 /* hard coded <SNR>, already translated */
19655 *arg += 3;
19656 return get_id_len(arg) + 3;
19657 }
19658 len = eval_fname_script(*arg);
19659 if (len > 0)
19660 {
19661 /* literal "<SID>", "s:" or "<SNR>" */
19662 *arg += len;
19663 }
19664
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019668 p = find_name_end(*arg, &expr_start, &expr_end,
19669 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 if (expr_start != NULL)
19671 {
19672 char_u *temp_string;
19673
19674 if (!evaluate)
19675 {
19676 len += (int)(p - *arg);
19677 *arg = skipwhite(p);
19678 return len;
19679 }
19680
19681 /*
19682 * Include any <SID> etc in the expanded string:
19683 * Thus the -len here.
19684 */
19685 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19686 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019687 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 *alias = temp_string;
19689 *arg = skipwhite(p);
19690 return (int)STRLEN(temp_string);
19691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692
19693 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019694 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 EMSG2(_(e_invexpr2), *arg);
19696
19697 return len;
19698}
19699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700/*
19701 * Find the end of a variable or function name, taking care of magic braces.
19702 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19703 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019704 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 * Return a pointer to just after the name. Equal to "arg" if there is no
19706 * valid name.
19707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019709find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 char_u *arg;
19711 char_u **expr_start;
19712 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019713 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 int mb_nest = 0;
19716 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 char_u *p;
19718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 *expr_start = NULL;
19722 *expr_end = NULL;
19723 }
19724
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019725 /* Quick check for valid starting character. */
19726 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19727 return arg;
19728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729 for (p = arg; *p != NUL
19730 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019731 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019732 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019734 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019736 if (*p == '\'')
19737 {
19738 /* skip over 'string' to avoid counting [ and ] inside it. */
19739 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19740 ;
19741 if (*p == NUL)
19742 break;
19743 }
19744 else if (*p == '"')
19745 {
19746 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19747 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19748 if (*p == '\\' && p[1] != NUL)
19749 ++p;
19750 if (*p == NUL)
19751 break;
19752 }
19753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 if (*p == '[')
19757 ++br_nest;
19758 else if (*p == ']')
19759 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019762 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 if (*p == '{')
19765 {
19766 mb_nest++;
19767 if (expr_start != NULL && *expr_start == NULL)
19768 *expr_start = p;
19769 }
19770 else if (*p == '}')
19771 {
19772 mb_nest--;
19773 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19774 *expr_end = p;
19775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 }
19778
19779 return p;
19780}
19781
19782/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019783 * Expands out the 'magic' {}'s in a variable/function name.
19784 * Note that this can call itself recursively, to deal with
19785 * constructs like foo{bar}{baz}{bam}
19786 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19787 * "in_start" ^
19788 * "expr_start" ^
19789 * "expr_end" ^
19790 * "in_end" ^
19791 *
19792 * Returns a new allocated string, which the caller must free.
19793 * Returns NULL for failure.
19794 */
19795 static char_u *
19796make_expanded_name(in_start, expr_start, expr_end, in_end)
19797 char_u *in_start;
19798 char_u *expr_start;
19799 char_u *expr_end;
19800 char_u *in_end;
19801{
19802 char_u c1;
19803 char_u *retval = NULL;
19804 char_u *temp_result;
19805 char_u *nextcmd = NULL;
19806
19807 if (expr_end == NULL || in_end == NULL)
19808 return NULL;
19809 *expr_start = NUL;
19810 *expr_end = NUL;
19811 c1 = *in_end;
19812 *in_end = NUL;
19813
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019814 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019815 if (temp_result != NULL && nextcmd == NULL)
19816 {
19817 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19818 + (in_end - expr_end) + 1));
19819 if (retval != NULL)
19820 {
19821 STRCPY(retval, in_start);
19822 STRCAT(retval, temp_result);
19823 STRCAT(retval, expr_end + 1);
19824 }
19825 }
19826 vim_free(temp_result);
19827
19828 *in_end = c1; /* put char back for error messages */
19829 *expr_start = '{';
19830 *expr_end = '}';
19831
19832 if (retval != NULL)
19833 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019834 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019835 if (expr_start != NULL)
19836 {
19837 /* Further expansion! */
19838 temp_result = make_expanded_name(retval, expr_start,
19839 expr_end, temp_result);
19840 vim_free(retval);
19841 retval = temp_result;
19842 }
19843 }
19844
19845 return retval;
19846}
19847
19848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019850 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 */
19852 static int
19853eval_isnamec(c)
19854 int c;
19855{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019856 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19857}
19858
19859/*
19860 * Return TRUE if character "c" can be used as the first character in a
19861 * variable or function name (excluding '{' and '}').
19862 */
19863 static int
19864eval_isnamec1(c)
19865 int c;
19866{
19867 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868}
19869
19870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 * Set number v: variable to "val".
19872 */
19873 void
19874set_vim_var_nr(idx, val)
19875 int idx;
19876 long val;
19877{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019878 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879}
19880
19881/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019882 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 */
19884 long
19885get_vim_var_nr(idx)
19886 int idx;
19887{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019888 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889}
19890
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019891/*
19892 * Get string v: variable value. Uses a static buffer, can only be used once.
19893 */
19894 char_u *
19895get_vim_var_str(idx)
19896 int idx;
19897{
19898 return get_tv_string(&vimvars[idx].vv_tv);
19899}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019900
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019902 * Get List v: variable value. Caller must take care of reference count when
19903 * needed.
19904 */
19905 list_T *
19906get_vim_var_list(idx)
19907 int idx;
19908{
19909 return vimvars[idx].vv_list;
19910}
19911
19912/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019913 * Set v:char to character "c".
19914 */
19915 void
19916set_vim_var_char(c)
19917 int c;
19918{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019919 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019920
19921#ifdef FEAT_MBYTE
19922 if (has_mbyte)
19923 buf[(*mb_char2bytes)(c, buf)] = NUL;
19924 else
19925#endif
19926 {
19927 buf[0] = c;
19928 buf[1] = NUL;
19929 }
19930 set_vim_var_string(VV_CHAR, buf, -1);
19931}
19932
19933/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019934 * Set v:count to "count" and v:count1 to "count1".
19935 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 */
19937 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019938set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 long count;
19940 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019941 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019943 if (set_prevcount)
19944 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019945 vimvars[VV_COUNT].vv_nr = count;
19946 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947}
19948
19949/*
19950 * Set string v: variable to a copy of "val".
19951 */
19952 void
19953set_vim_var_string(idx, val, len)
19954 int idx;
19955 char_u *val;
19956 int len; /* length of "val" to use or -1 (whole string) */
19957{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019958 /* Need to do this (at least) once, since we can't initialize a union.
19959 * Will always be invoked when "v:progname" is set. */
19960 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19961
Bram Moolenaare9a41262005-01-15 22:18:47 +000019962 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019964 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019966 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019968 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969}
19970
19971/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019972 * Set List v: variable to "val".
19973 */
19974 void
19975set_vim_var_list(idx, val)
19976 int idx;
19977 list_T *val;
19978{
19979 list_unref(vimvars[idx].vv_list);
19980 vimvars[idx].vv_list = val;
19981 if (val != NULL)
19982 ++val->lv_refcount;
19983}
19984
19985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 * Set v:register if needed.
19987 */
19988 void
19989set_reg_var(c)
19990 int c;
19991{
19992 char_u regname;
19993
19994 if (c == 0 || c == ' ')
19995 regname = '"';
19996 else
19997 regname = c;
19998 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019999 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 set_vim_var_string(VV_REG, &regname, 1);
20001}
20002
20003/*
20004 * Get or set v:exception. If "oldval" == NULL, return the current value.
20005 * Otherwise, restore the value to "oldval" and return NULL.
20006 * Must always be called in pairs to save and restore v:exception! Does not
20007 * take care of memory allocations.
20008 */
20009 char_u *
20010v_exception(oldval)
20011 char_u *oldval;
20012{
20013 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020014 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015
Bram Moolenaare9a41262005-01-15 22:18:47 +000020016 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 return NULL;
20018}
20019
20020/*
20021 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20022 * Otherwise, restore the value to "oldval" and return NULL.
20023 * Must always be called in pairs to save and restore v:throwpoint! Does not
20024 * take care of memory allocations.
20025 */
20026 char_u *
20027v_throwpoint(oldval)
20028 char_u *oldval;
20029{
20030 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032
Bram Moolenaare9a41262005-01-15 22:18:47 +000020033 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 return NULL;
20035}
20036
20037#if defined(FEAT_AUTOCMD) || defined(PROTO)
20038/*
20039 * Set v:cmdarg.
20040 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20041 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20042 * Must always be called in pairs!
20043 */
20044 char_u *
20045set_cmdarg(eap, oldarg)
20046 exarg_T *eap;
20047 char_u *oldarg;
20048{
20049 char_u *oldval;
20050 char_u *newval;
20051 unsigned len;
20052
Bram Moolenaare9a41262005-01-15 22:18:47 +000020053 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020054 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020056 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020057 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020058 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 }
20060
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020061 if (eap->force_bin == FORCE_BIN)
20062 len = 6;
20063 else if (eap->force_bin == FORCE_NOBIN)
20064 len = 8;
20065 else
20066 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020067
20068 if (eap->read_edit)
20069 len += 7;
20070
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020071 if (eap->force_ff != 0)
20072 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20073# ifdef FEAT_MBYTE
20074 if (eap->force_enc != 0)
20075 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020076 if (eap->bad_char != 0)
20077 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020078# endif
20079
20080 newval = alloc(len + 1);
20081 if (newval == NULL)
20082 return NULL;
20083
20084 if (eap->force_bin == FORCE_BIN)
20085 sprintf((char *)newval, " ++bin");
20086 else if (eap->force_bin == FORCE_NOBIN)
20087 sprintf((char *)newval, " ++nobin");
20088 else
20089 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020090
20091 if (eap->read_edit)
20092 STRCAT(newval, " ++edit");
20093
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020094 if (eap->force_ff != 0)
20095 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20096 eap->cmd + eap->force_ff);
20097# ifdef FEAT_MBYTE
20098 if (eap->force_enc != 0)
20099 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20100 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020101 if (eap->bad_char == BAD_KEEP)
20102 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20103 else if (eap->bad_char == BAD_DROP)
20104 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20105 else if (eap->bad_char != 0)
20106 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020107# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020108 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020109 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110}
20111#endif
20112
20113/*
20114 * Get the value of internal variable "name".
20115 * Return OK or FAIL.
20116 */
20117 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020118get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 char_u *name;
20120 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020121 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020122 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020123 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124{
20125 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 typval_T *tv = NULL;
20127 typval_T atv;
20128 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130
20131 /* truncate the name, so that we can use strcmp() */
20132 cc = name[len];
20133 name[len] = NUL;
20134
20135 /*
20136 * Check for "b:changedtick".
20137 */
20138 if (STRCMP(name, "b:changedtick") == 0)
20139 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020140 atv.v_type = VAR_NUMBER;
20141 atv.vval.v_number = curbuf->b_changedtick;
20142 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 }
20144
20145 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 * Check for user-defined variables.
20147 */
20148 else
20149 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020150 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 }
20154
Bram Moolenaare9a41262005-01-15 22:18:47 +000020155 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020157 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 ret = FAIL;
20160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020162 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163
20164 name[len] = cc;
20165
20166 return ret;
20167}
20168
20169/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020170 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20171 * Also handle function call with Funcref variable: func(expr)
20172 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20173 */
20174 static int
20175handle_subscript(arg, rettv, evaluate, verbose)
20176 char_u **arg;
20177 typval_T *rettv;
20178 int evaluate; /* do more than finding the end */
20179 int verbose; /* give error messages */
20180{
20181 int ret = OK;
20182 dict_T *selfdict = NULL;
20183 char_u *s;
20184 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020185 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020186
20187 while (ret == OK
20188 && (**arg == '['
20189 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020190 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020191 && !vim_iswhite(*(*arg - 1)))
20192 {
20193 if (**arg == '(')
20194 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020195 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020196 if (evaluate)
20197 {
20198 functv = *rettv;
20199 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020200
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020201 /* Invoke the function. Recursive! */
20202 s = functv.vval.v_string;
20203 }
20204 else
20205 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020206 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020207 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20208 &len, evaluate, selfdict);
20209
20210 /* Clear the funcref afterwards, so that deleting it while
20211 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020212 if (evaluate)
20213 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020214
20215 /* Stop the expression evaluation when immediately aborting on
20216 * error, or when an interrupt occurred or an exception was thrown
20217 * but not caught. */
20218 if (aborting())
20219 {
20220 if (ret == OK)
20221 clear_tv(rettv);
20222 ret = FAIL;
20223 }
20224 dict_unref(selfdict);
20225 selfdict = NULL;
20226 }
20227 else /* **arg == '[' || **arg == '.' */
20228 {
20229 dict_unref(selfdict);
20230 if (rettv->v_type == VAR_DICT)
20231 {
20232 selfdict = rettv->vval.v_dict;
20233 if (selfdict != NULL)
20234 ++selfdict->dv_refcount;
20235 }
20236 else
20237 selfdict = NULL;
20238 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20239 {
20240 clear_tv(rettv);
20241 ret = FAIL;
20242 }
20243 }
20244 }
20245 dict_unref(selfdict);
20246 return ret;
20247}
20248
20249/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020250 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020251 * value).
20252 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020253 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020254alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020255{
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020257}
20258
20259/*
20260 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 * The string "s" must have been allocated, it is consumed.
20262 * Return NULL for out of memory, the variable otherwise.
20263 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 char_u *s;
20267{
Bram Moolenaar33570922005-01-25 22:26:29 +000020268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270 rettv = alloc_tv();
20271 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020273 rettv->v_type = VAR_STRING;
20274 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 }
20276 else
20277 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279}
20280
20281/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020282 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020284 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020286 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287{
20288 if (varp != NULL)
20289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020290 switch (varp->v_type)
20291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020293 func_unref(varp->vval.v_string);
20294 /*FALLTHROUGH*/
20295 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020296 vim_free(varp->vval.v_string);
20297 break;
20298 case VAR_LIST:
20299 list_unref(varp->vval.v_list);
20300 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020301 case VAR_DICT:
20302 dict_unref(varp->vval.v_dict);
20303 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020304 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020305#ifdef FEAT_FLOAT
20306 case VAR_FLOAT:
20307#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020308 case VAR_UNKNOWN:
20309 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020310 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020311 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020312 break;
20313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 vim_free(varp);
20315 }
20316}
20317
20318/*
20319 * Free the memory for a variable value and set the value to NULL or 0.
20320 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020321 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020322clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324{
20325 if (varp != NULL)
20326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020327 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020329 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020330 func_unref(varp->vval.v_string);
20331 /*FALLTHROUGH*/
20332 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020333 vim_free(varp->vval.v_string);
20334 varp->vval.v_string = NULL;
20335 break;
20336 case VAR_LIST:
20337 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020338 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020339 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020340 case VAR_DICT:
20341 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020342 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020343 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020344 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020345 varp->vval.v_number = 0;
20346 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020347#ifdef FEAT_FLOAT
20348 case VAR_FLOAT:
20349 varp->vval.v_float = 0.0;
20350 break;
20351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 case VAR_UNKNOWN:
20353 break;
20354 default:
20355 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020357 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 }
20359}
20360
20361/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 * Set the value of a variable to NULL without freeing items.
20363 */
20364 static void
20365init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367{
20368 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370}
20371
20372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 * Get the number value of a variable.
20374 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020375 * For incompatible types, return 0.
20376 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20377 * caller of incompatible types: it sets *denote to TRUE if "denote"
20378 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 */
20380 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020384 int error = FALSE;
20385
20386 return get_tv_number_chk(varp, &error); /* return 0L on error */
20387}
20388
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020389 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020390get_tv_number_chk(varp, denote)
20391 typval_T *varp;
20392 int *denote;
20393{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020394 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020396 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020398 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020399 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020400#ifdef FEAT_FLOAT
20401 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020402 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020403 break;
20404#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020405 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020406 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020407 break;
20408 case VAR_STRING:
20409 if (varp->vval.v_string != NULL)
20410 vim_str2nr(varp->vval.v_string, NULL, NULL,
20411 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020412 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020413 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020414 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020415 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020416 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020417 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020418 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020419 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020420 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020423 if (denote == NULL) /* useful for values that must be unsigned */
20424 n = -1;
20425 else
20426 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020427 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428}
20429
20430/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020431 * Get the lnum from the first argument.
20432 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020433 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 */
20435 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020437 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438{
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 linenr_T lnum;
20441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020442 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 if (lnum == 0) /* no valid number, try using line() */
20444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 rettv.v_type = VAR_NUMBER;
20446 f_line(argvars, &rettv);
20447 lnum = rettv.vval.v_number;
20448 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 }
20450 return lnum;
20451}
20452
20453/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020454 * Get the lnum from the first argument.
20455 * Also accepts "$", then "buf" is used.
20456 * Returns 0 on error.
20457 */
20458 static linenr_T
20459get_tv_lnum_buf(argvars, buf)
20460 typval_T *argvars;
20461 buf_T *buf;
20462{
20463 if (argvars[0].v_type == VAR_STRING
20464 && argvars[0].vval.v_string != NULL
20465 && argvars[0].vval.v_string[0] == '$'
20466 && buf != NULL)
20467 return buf->b_ml.ml_line_count;
20468 return get_tv_number_chk(&argvars[0], NULL);
20469}
20470
20471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 * Get the string value of a variable.
20473 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20475 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 * If the String variable has never been set, return an empty string.
20477 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020478 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20479 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 */
20481 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484{
20485 static char_u mybuf[NUMBUFLEN];
20486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488}
20489
20490 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020492 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020493 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020495 char_u *res = get_tv_string_buf_chk(varp, buf);
20496
20497 return res != NULL ? res : (char_u *)"";
20498}
20499
Bram Moolenaar7d647822014-04-05 21:28:56 +020020500/*
20501 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20502 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020503 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020504get_tv_string_chk(varp)
20505 typval_T *varp;
20506{
20507 static char_u mybuf[NUMBUFLEN];
20508
20509 return get_tv_string_buf_chk(varp, mybuf);
20510}
20511
20512 static char_u *
20513get_tv_string_buf_chk(varp, buf)
20514 typval_T *varp;
20515 char_u *buf;
20516{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020519 case VAR_NUMBER:
20520 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20521 return buf;
20522 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020523 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020524 break;
20525 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020526 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020527 break;
20528 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020529 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020530 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020531#ifdef FEAT_FLOAT
20532 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020533 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020534 break;
20535#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020536 case VAR_STRING:
20537 if (varp->vval.v_string != NULL)
20538 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020539 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020540 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020541 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020544 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545}
20546
20547/*
20548 * Find variable "name" in the list of variables.
20549 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020550 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020551 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020554 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020555find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020557 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020558 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562
Bram Moolenaara7043832005-01-21 11:56:39 +000020563 ht = find_var_ht(name, &varname);
20564 if (htp != NULL)
20565 *htp = ht;
20566 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020568 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020572 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020573 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020575 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020576find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020577 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020578 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020579 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020580 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020581{
Bram Moolenaar33570922005-01-25 22:26:29 +000020582 hashitem_T *hi;
20583
20584 if (*varname == NUL)
20585 {
20586 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020587 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020589 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 case 'g': return &globvars_var;
20591 case 'v': return &vimvars_var;
20592 case 'b': return &curbuf->b_bufvar;
20593 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020594#ifdef FEAT_WINDOWS
20595 case 't': return &curtab->tp_winvar;
20596#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020597 case 'l': return current_funccal == NULL
20598 ? NULL : &current_funccal->l_vars_var;
20599 case 'a': return current_funccal == NULL
20600 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020601 }
20602 return NULL;
20603 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020604
20605 hi = hash_find(ht, varname);
20606 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020607 {
20608 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020609 * worked find the variable again. Don't auto-load a script if it was
20610 * loaded already, otherwise it would be loaded every time when
20611 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020612 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020613 {
20614 /* Note: script_autoload() may make "hi" invalid. It must either
20615 * be obtained again or not used. */
20616 if (!script_autoload(varname, FALSE) || aborting())
20617 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020618 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020619 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020620 if (HASHITEM_EMPTY(hi))
20621 return NULL;
20622 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020623 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020624}
20625
20626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020627 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020628 * Set "varname" to the start of name without ':'.
20629 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020630 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020631find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 char_u *name;
20633 char_u **varname;
20634{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020635 hashitem_T *hi;
20636
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 if (name[1] != ':')
20638 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020639 /* The name must not start with a colon or #. */
20640 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 return NULL;
20642 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020643
20644 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020645 hi = hash_find(&compat_hashtab, name);
20646 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020647 return &compat_hashtab;
20648
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020650 return &globvarht; /* global variable */
20651 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 }
20653 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020654 if (*name == 'g') /* global variable */
20655 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020656 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20657 */
20658 if (vim_strchr(name + 2, ':') != NULL
20659 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020660 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020662 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020664 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020665#ifdef FEAT_WINDOWS
20666 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020667 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 if (*name == 'v') /* v: variable */
20670 return &vimvarht;
20671 if (*name == 'a' && current_funccal != NULL) /* function argument */
20672 return &current_funccal->l_avars.dv_hashtab;
20673 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20674 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 if (*name == 's' /* script variable */
20676 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20677 return &SCRIPT_VARS(current_SID);
20678 return NULL;
20679}
20680
20681/*
20682 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020683 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 * Returns NULL when it doesn't exist.
20685 */
20686 char_u *
20687get_var_value(name)
20688 char_u *name;
20689{
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020692 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 if (v == NULL)
20694 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020695 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696}
20697
20698/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020699 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 * sourcing this script and when executing functions defined in the script.
20701 */
20702 void
20703new_script_vars(id)
20704 scid_T id;
20705{
Bram Moolenaara7043832005-01-21 11:56:39 +000020706 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020707 hashtab_T *ht;
20708 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020709
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20711 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020712 /* Re-allocating ga_data means that an ht_array pointing to
20713 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020714 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020715 for (i = 1; i <= ga_scripts.ga_len; ++i)
20716 {
20717 ht = &SCRIPT_VARS(i);
20718 if (ht->ht_mask == HT_INIT_SIZE - 1)
20719 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020720 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020721 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020722 }
20723
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 while (ga_scripts.ga_len < id)
20725 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020726 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020727 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020728 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 }
20731 }
20732}
20733
20734/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020735 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20736 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 */
20738 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020739init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020740 dict_T *dict;
20741 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020742 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743{
Bram Moolenaar33570922005-01-25 22:26:29 +000020744 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020745 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020746 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020747 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020748 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020749 dict_var->di_tv.vval.v_dict = dict;
20750 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020751 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020752 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20753 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754}
20755
20756/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020757 * Unreference a dictionary initialized by init_var_dict().
20758 */
20759 void
20760unref_var_dict(dict)
20761 dict_T *dict;
20762{
20763 /* Now the dict needs to be freed if no one else is using it, go back to
20764 * normal reference counting. */
20765 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20766 dict_unref(dict);
20767}
20768
20769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020771 * Frees all allocated variables and the value they contain.
20772 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 */
20774 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020775vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020776 hashtab_T *ht;
20777{
20778 vars_clear_ext(ht, TRUE);
20779}
20780
20781/*
20782 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20783 */
20784 static void
20785vars_clear_ext(ht, free_val)
20786 hashtab_T *ht;
20787 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788{
Bram Moolenaara7043832005-01-21 11:56:39 +000020789 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020790 hashitem_T *hi;
20791 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792
Bram Moolenaar33570922005-01-25 22:26:29 +000020793 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020794 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020795 for (hi = ht->ht_array; todo > 0; ++hi)
20796 {
20797 if (!HASHITEM_EMPTY(hi))
20798 {
20799 --todo;
20800
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020802 * ht_array might change then. hash_clear() takes care of it
20803 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020804 v = HI2DI(hi);
20805 if (free_val)
20806 clear_tv(&v->di_tv);
20807 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20808 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020809 }
20810 }
20811 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020812 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813}
20814
Bram Moolenaara7043832005-01-21 11:56:39 +000020815/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020816 * Delete a variable from hashtab "ht" at item "hi".
20817 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020820delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020821 hashtab_T *ht;
20822 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
Bram Moolenaar33570922005-01-25 22:26:29 +000020824 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020825
20826 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020827 clear_tv(&di->di_tv);
20828 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829}
20830
20831/*
20832 * List the value of one internal variable.
20833 */
20834 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020835list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020838 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020840 char_u *tofree;
20841 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020842 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020843
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020844 current_copyID += COPYID_INC;
20845 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020847 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020848 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849}
20850
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020852list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 char_u *prefix;
20854 char_u *name;
20855 int type;
20856 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020857 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
Bram Moolenaar31859182007-08-14 20:41:13 +000020859 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20860 msg_start();
20861 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 if (name != NULL) /* "a:" vars don't have a name stored */
20863 msg_puts(name);
20864 msg_putchar(' ');
20865 msg_advance(22);
20866 if (type == VAR_NUMBER)
20867 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020868 else if (type == VAR_FUNC)
20869 msg_putchar('*');
20870 else if (type == VAR_LIST)
20871 {
20872 msg_putchar('[');
20873 if (*string == '[')
20874 ++string;
20875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020876 else if (type == VAR_DICT)
20877 {
20878 msg_putchar('{');
20879 if (*string == '{')
20880 ++string;
20881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 else
20883 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020884
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020886
20887 if (type == VAR_FUNC)
20888 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020889 if (*first)
20890 {
20891 msg_clr_eos();
20892 *first = FALSE;
20893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894}
20895
20896/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 * If the variable already exists, the value is updated.
20899 * Otherwise the variable is created.
20900 */
20901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020904 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020905 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906{
Bram Moolenaar33570922005-01-25 22:26:29 +000020907 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020909 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020911 ht = find_var_ht(name, &varname);
20912 if (ht == NULL || *varname == NUL)
20913 {
20914 EMSG2(_(e_illvar), name);
20915 return;
20916 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020917 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020918
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020919 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20920 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020921
Bram Moolenaar33570922005-01-25 22:26:29 +000020922 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020924 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020925 if (var_check_ro(v->di_flags, name)
20926 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020927 return;
20928 if (v->di_tv.v_type != tv->v_type
20929 && !((v->di_tv.v_type == VAR_STRING
20930 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020931 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020932 || tv->v_type == VAR_NUMBER))
20933#ifdef FEAT_FLOAT
20934 && !((v->di_tv.v_type == VAR_NUMBER
20935 || v->di_tv.v_type == VAR_FLOAT)
20936 && (tv->v_type == VAR_NUMBER
20937 || tv->v_type == VAR_FLOAT))
20938#endif
20939 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020940 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020941 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020942 return;
20943 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020944
20945 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020946 * Handle setting internal v: variables separately: we don't change
20947 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020948 */
20949 if (ht == &vimvarht)
20950 {
20951 if (v->di_tv.v_type == VAR_STRING)
20952 {
20953 vim_free(v->di_tv.vval.v_string);
20954 if (copy || tv->v_type != VAR_STRING)
20955 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20956 else
20957 {
20958 /* Take over the string to avoid an extra alloc/free. */
20959 v->di_tv.vval.v_string = tv->vval.v_string;
20960 tv->vval.v_string = NULL;
20961 }
20962 }
20963 else if (v->di_tv.v_type != VAR_NUMBER)
20964 EMSG2(_(e_intern2), "set_var()");
20965 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020966 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020967 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020968 if (STRCMP(varname, "searchforward") == 0)
20969 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020970#ifdef FEAT_SEARCH_EXTRA
20971 else if (STRCMP(varname, "hlsearch") == 0)
20972 {
20973 no_hlsearch = !v->di_tv.vval.v_number;
20974 redraw_all_later(SOME_VALID);
20975 }
20976#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020977 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 return;
20979 }
20980
20981 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 }
20983 else /* add a new variable */
20984 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020985 /* Can't add "v:" variable. */
20986 if (ht == &vimvarht)
20987 {
20988 EMSG2(_(e_illvar), name);
20989 return;
20990 }
20991
Bram Moolenaar92124a32005-06-17 22:03:40 +000020992 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020993 if (!valid_varname(varname))
20994 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020995
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020996 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20997 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020998 if (v == NULL)
20999 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021000 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021001 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021003 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 return;
21005 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021006 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021008
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021009 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021011 else
21012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021014 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017}
21018
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021019/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021020 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021021 * Also give an error message.
21022 */
21023 static int
21024var_check_ro(flags, name)
21025 int flags;
21026 char_u *name;
21027{
21028 if (flags & DI_FLAGS_RO)
21029 {
21030 EMSG2(_(e_readonlyvar), name);
21031 return TRUE;
21032 }
21033 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21034 {
21035 EMSG2(_(e_readonlysbx), name);
21036 return TRUE;
21037 }
21038 return FALSE;
21039}
21040
21041/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021042 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21043 * Also give an error message.
21044 */
21045 static int
21046var_check_fixed(flags, name)
21047 int flags;
21048 char_u *name;
21049{
21050 if (flags & DI_FLAGS_FIX)
21051 {
21052 EMSG2(_("E795: Cannot delete variable %s"), name);
21053 return TRUE;
21054 }
21055 return FALSE;
21056}
21057
21058/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021059 * Check if a funcref is assigned to a valid variable name.
21060 * Return TRUE and give an error if not.
21061 */
21062 static int
21063var_check_func_name(name, new_var)
21064 char_u *name; /* points to start of variable name */
21065 int new_var; /* TRUE when creating the variable */
21066{
21067 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
21068 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21069 ? name[2] : name[0]))
21070 {
21071 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21072 name);
21073 return TRUE;
21074 }
21075 /* Don't allow hiding a function. When "v" is not NULL we might be
21076 * assigning another function to the same var, the type is checked
21077 * below. */
21078 if (new_var && function_exists(name))
21079 {
21080 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21081 name);
21082 return TRUE;
21083 }
21084 return FALSE;
21085}
21086
21087/*
21088 * Check if a variable name is valid.
21089 * Return FALSE and give an error if not.
21090 */
21091 static int
21092valid_varname(varname)
21093 char_u *varname;
21094{
21095 char_u *p;
21096
21097 for (p = varname; *p != NUL; ++p)
21098 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21099 && *p != AUTOLOAD_CHAR)
21100 {
21101 EMSG2(_(e_illvar), varname);
21102 return FALSE;
21103 }
21104 return TRUE;
21105}
21106
21107/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021108 * Return TRUE if typeval "tv" is set to be locked (immutable).
21109 * Also give an error message, using "name".
21110 */
21111 static int
21112tv_check_lock(lock, name)
21113 int lock;
21114 char_u *name;
21115{
21116 if (lock & VAR_LOCKED)
21117 {
21118 EMSG2(_("E741: Value is locked: %s"),
21119 name == NULL ? (char_u *)_("Unknown") : name);
21120 return TRUE;
21121 }
21122 if (lock & VAR_FIXED)
21123 {
21124 EMSG2(_("E742: Cannot change value of %s"),
21125 name == NULL ? (char_u *)_("Unknown") : name);
21126 return TRUE;
21127 }
21128 return FALSE;
21129}
21130
21131/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021132 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021133 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021134 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021135 * It is OK for "from" and "to" to point to the same item. This is used to
21136 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021137 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021138 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021139copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021140 typval_T *from;
21141 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021143 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021144 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021145 switch (from->v_type)
21146 {
21147 case VAR_NUMBER:
21148 to->vval.v_number = from->vval.v_number;
21149 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021150#ifdef FEAT_FLOAT
21151 case VAR_FLOAT:
21152 to->vval.v_float = from->vval.v_float;
21153 break;
21154#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021155 case VAR_STRING:
21156 case VAR_FUNC:
21157 if (from->vval.v_string == NULL)
21158 to->vval.v_string = NULL;
21159 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021160 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021161 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 if (from->v_type == VAR_FUNC)
21163 func_ref(to->vval.v_string);
21164 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021165 break;
21166 case VAR_LIST:
21167 if (from->vval.v_list == NULL)
21168 to->vval.v_list = NULL;
21169 else
21170 {
21171 to->vval.v_list = from->vval.v_list;
21172 ++to->vval.v_list->lv_refcount;
21173 }
21174 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021175 case VAR_DICT:
21176 if (from->vval.v_dict == NULL)
21177 to->vval.v_dict = NULL;
21178 else
21179 {
21180 to->vval.v_dict = from->vval.v_dict;
21181 ++to->vval.v_dict->dv_refcount;
21182 }
21183 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021184 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021186 break;
21187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188}
21189
21190/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021191 * Make a copy of an item.
21192 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021193 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21194 * reference to an already copied list/dict can be used.
21195 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021196 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021197 static int
21198item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021199 typval_T *from;
21200 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021201 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021202 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203{
21204 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021205 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021206
Bram Moolenaar33570922005-01-25 22:26:29 +000021207 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021208 {
21209 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021210 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021211 }
21212 ++recurse;
21213
21214 switch (from->v_type)
21215 {
21216 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021217#ifdef FEAT_FLOAT
21218 case VAR_FLOAT:
21219#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021220 case VAR_STRING:
21221 case VAR_FUNC:
21222 copy_tv(from, to);
21223 break;
21224 case VAR_LIST:
21225 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021226 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021227 if (from->vval.v_list == NULL)
21228 to->vval.v_list = NULL;
21229 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21230 {
21231 /* use the copy made earlier */
21232 to->vval.v_list = from->vval.v_list->lv_copylist;
21233 ++to->vval.v_list->lv_refcount;
21234 }
21235 else
21236 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21237 if (to->vval.v_list == NULL)
21238 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021239 break;
21240 case VAR_DICT:
21241 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021242 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021243 if (from->vval.v_dict == NULL)
21244 to->vval.v_dict = NULL;
21245 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21246 {
21247 /* use the copy made earlier */
21248 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21249 ++to->vval.v_dict->dv_refcount;
21250 }
21251 else
21252 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21253 if (to->vval.v_dict == NULL)
21254 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021255 break;
21256 default:
21257 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021258 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021259 }
21260 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021261 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021262}
21263
21264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 * ":echo expr1 ..." print each argument separated with a space, add a
21266 * newline at the end.
21267 * ":echon expr1 ..." print each argument plain.
21268 */
21269 void
21270ex_echo(eap)
21271 exarg_T *eap;
21272{
21273 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021274 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021275 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 char_u *p;
21277 int needclr = TRUE;
21278 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021279 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280
21281 if (eap->skip)
21282 ++emsg_skip;
21283 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21284 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021285 /* If eval1() causes an error message the text from the command may
21286 * still need to be cleared. E.g., "echo 22,44". */
21287 need_clr_eos = needclr;
21288
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021290 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
21292 /*
21293 * Report the invalid expression unless the expression evaluation
21294 * has been cancelled due to an aborting error, an interrupt, or an
21295 * exception.
21296 */
21297 if (!aborting())
21298 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021299 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 break;
21301 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021302 need_clr_eos = FALSE;
21303
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 if (!eap->skip)
21305 {
21306 if (atstart)
21307 {
21308 atstart = FALSE;
21309 /* Call msg_start() after eval1(), evaluating the expression
21310 * may cause a message to appear. */
21311 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021312 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021313 /* Mark the saved text as finishing the line, so that what
21314 * follows is displayed on a new line when scrolling back
21315 * at the more prompt. */
21316 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 }
21320 else if (eap->cmdidx == CMD_echo)
21321 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021322 current_copyID += COPYID_INC;
21323 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021324 if (p != NULL)
21325 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021327 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021329 if (*p != TAB && needclr)
21330 {
21331 /* remove any text still there from the command */
21332 msg_clr_eos();
21333 needclr = FALSE;
21334 }
21335 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 }
21337 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021338 {
21339#ifdef FEAT_MBYTE
21340 if (has_mbyte)
21341 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021342 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021343
21344 (void)msg_outtrans_len_attr(p, i, echo_attr);
21345 p += i - 1;
21346 }
21347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021349 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021352 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 arg = skipwhite(arg);
21356 }
21357 eap->nextcmd = check_nextcmd(arg);
21358
21359 if (eap->skip)
21360 --emsg_skip;
21361 else
21362 {
21363 /* remove text that may still be there from the command */
21364 if (needclr)
21365 msg_clr_eos();
21366 if (eap->cmdidx == CMD_echo)
21367 msg_end();
21368 }
21369}
21370
21371/*
21372 * ":echohl {name}".
21373 */
21374 void
21375ex_echohl(eap)
21376 exarg_T *eap;
21377{
21378 int id;
21379
21380 id = syn_name2id(eap->arg);
21381 if (id == 0)
21382 echo_attr = 0;
21383 else
21384 echo_attr = syn_id2attr(id);
21385}
21386
21387/*
21388 * ":execute expr1 ..." execute the result of an expression.
21389 * ":echomsg expr1 ..." Print a message
21390 * ":echoerr expr1 ..." Print an error
21391 * Each gets spaces around each argument and a newline at the end for
21392 * echo commands
21393 */
21394 void
21395ex_execute(eap)
21396 exarg_T *eap;
21397{
21398 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 int ret = OK;
21401 char_u *p;
21402 garray_T ga;
21403 int len;
21404 int save_did_emsg;
21405
21406 ga_init2(&ga, 1, 80);
21407
21408 if (eap->skip)
21409 ++emsg_skip;
21410 while (*arg != NUL && *arg != '|' && *arg != '\n')
21411 {
21412 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021413 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 {
21415 /*
21416 * Report the invalid expression unless the expression evaluation
21417 * has been cancelled due to an aborting error, an interrupt, or an
21418 * exception.
21419 */
21420 if (!aborting())
21421 EMSG2(_(e_invexpr2), p);
21422 ret = FAIL;
21423 break;
21424 }
21425
21426 if (!eap->skip)
21427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021428 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 len = (int)STRLEN(p);
21430 if (ga_grow(&ga, len + 2) == FAIL)
21431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 ret = FAIL;
21434 break;
21435 }
21436 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 ga.ga_len += len;
21440 }
21441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021442 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 arg = skipwhite(arg);
21444 }
21445
21446 if (ret != FAIL && ga.ga_data != NULL)
21447 {
21448 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021451 out_flush();
21452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 else if (eap->cmdidx == CMD_echoerr)
21454 {
21455 /* We don't want to abort following commands, restore did_emsg. */
21456 save_did_emsg = did_emsg;
21457 EMSG((char_u *)ga.ga_data);
21458 if (!force_abort)
21459 did_emsg = save_did_emsg;
21460 }
21461 else if (eap->cmdidx == CMD_execute)
21462 do_cmdline((char_u *)ga.ga_data,
21463 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21464 }
21465
21466 ga_clear(&ga);
21467
21468 if (eap->skip)
21469 --emsg_skip;
21470
21471 eap->nextcmd = check_nextcmd(arg);
21472}
21473
21474/*
21475 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21476 * "arg" points to the "&" or '+' when called, to "option" when returning.
21477 * Returns NULL when no option name found. Otherwise pointer to the char
21478 * after the option name.
21479 */
21480 static char_u *
21481find_option_end(arg, opt_flags)
21482 char_u **arg;
21483 int *opt_flags;
21484{
21485 char_u *p = *arg;
21486
21487 ++p;
21488 if (*p == 'g' && p[1] == ':')
21489 {
21490 *opt_flags = OPT_GLOBAL;
21491 p += 2;
21492 }
21493 else if (*p == 'l' && p[1] == ':')
21494 {
21495 *opt_flags = OPT_LOCAL;
21496 p += 2;
21497 }
21498 else
21499 *opt_flags = 0;
21500
21501 if (!ASCII_ISALPHA(*p))
21502 return NULL;
21503 *arg = p;
21504
21505 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21506 p += 4; /* termcap option */
21507 else
21508 while (ASCII_ISALPHA(*p))
21509 ++p;
21510 return p;
21511}
21512
21513/*
21514 * ":function"
21515 */
21516 void
21517ex_function(eap)
21518 exarg_T *eap;
21519{
21520 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021521 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 int j;
21523 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021525 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 char_u *name = NULL;
21527 char_u *p;
21528 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021529 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 garray_T newargs;
21531 garray_T newlines;
21532 int varargs = FALSE;
21533 int mustend = FALSE;
21534 int flags = 0;
21535 ufunc_T *fp;
21536 int indent;
21537 int nesting;
21538 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 dictitem_T *v;
21540 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021541 static int func_nr = 0; /* number for nameless function */
21542 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021543 hashtab_T *ht;
21544 int todo;
21545 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021546 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547
21548 /*
21549 * ":function" without argument: list functions.
21550 */
21551 if (ends_excmd(*eap->arg))
21552 {
21553 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021554 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021555 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021556 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021557 {
21558 if (!HASHITEM_EMPTY(hi))
21559 {
21560 --todo;
21561 fp = HI2UF(hi);
21562 if (!isdigit(*fp->uf_name))
21563 list_func_head(fp, FALSE);
21564 }
21565 }
21566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 eap->nextcmd = check_nextcmd(eap->arg);
21568 return;
21569 }
21570
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021571 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021572 * ":function /pat": list functions matching pattern.
21573 */
21574 if (*eap->arg == '/')
21575 {
21576 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21577 if (!eap->skip)
21578 {
21579 regmatch_T regmatch;
21580
21581 c = *p;
21582 *p = NUL;
21583 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21584 *p = c;
21585 if (regmatch.regprog != NULL)
21586 {
21587 regmatch.rm_ic = p_ic;
21588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021589 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021590 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21591 {
21592 if (!HASHITEM_EMPTY(hi))
21593 {
21594 --todo;
21595 fp = HI2UF(hi);
21596 if (!isdigit(*fp->uf_name)
21597 && vim_regexec(&regmatch, fp->uf_name, 0))
21598 list_func_head(fp, FALSE);
21599 }
21600 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021601 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021602 }
21603 }
21604 if (*p == '/')
21605 ++p;
21606 eap->nextcmd = check_nextcmd(p);
21607 return;
21608 }
21609
21610 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 * Get the function name. There are these situations:
21612 * func normal function name
21613 * "name" == func, "fudi.fd_dict" == NULL
21614 * dict.func new dictionary entry
21615 * "name" == NULL, "fudi.fd_dict" set,
21616 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21617 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021618 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21620 * dict.func existing dict entry that's not a Funcref
21621 * "name" == NULL, "fudi.fd_dict" set,
21622 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021623 * s:func script-local function name
21624 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 name = trans_function_name(&p, eap->skip, 0, &fudi);
21628 paren = (vim_strchr(p, '(') != NULL);
21629 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 {
21631 /*
21632 * Return on an invalid expression in braces, unless the expression
21633 * evaluation has been cancelled due to an aborting error, an
21634 * interrupt, or an exception.
21635 */
21636 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021638 if (!eap->skip && fudi.fd_newkey != NULL)
21639 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021640 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 else
21644 eap->skip = TRUE;
21645 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021646
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 /* An error in a function call during evaluation of an expression in magic
21648 * braces should not cause the function not to be defined. */
21649 saved_did_emsg = did_emsg;
21650 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651
21652 /*
21653 * ":function func" with only function name: list function.
21654 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 {
21657 if (!ends_excmd(*skipwhite(p)))
21658 {
21659 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 }
21662 eap->nextcmd = check_nextcmd(p);
21663 if (eap->nextcmd != NULL)
21664 *p = NUL;
21665 if (!eap->skip && !got_int)
21666 {
21667 fp = find_func(name);
21668 if (fp != NULL)
21669 {
21670 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021671 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021673 if (FUNCLINE(fp, j) == NULL)
21674 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 msg_putchar('\n');
21676 msg_outnum((long)(j + 1));
21677 if (j < 9)
21678 msg_putchar(' ');
21679 if (j < 99)
21680 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021681 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 out_flush(); /* show a line at a time */
21683 ui_breakcheck();
21684 }
21685 if (!got_int)
21686 {
21687 msg_putchar('\n');
21688 msg_puts((char_u *)" endfunction");
21689 }
21690 }
21691 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021692 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 }
21696
21697 /*
21698 * ":function name(arg1, arg2)" Define function.
21699 */
21700 p = skipwhite(p);
21701 if (*p != '(')
21702 {
21703 if (!eap->skip)
21704 {
21705 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
21708 /* attempt to continue by skipping some text */
21709 if (vim_strchr(p, '(') != NULL)
21710 p = vim_strchr(p, '(');
21711 }
21712 p = skipwhite(p + 1);
21713
21714 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21715 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21716
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021717 if (!eap->skip)
21718 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021719 /* Check the name of the function. Unless it's a dictionary function
21720 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021721 if (name != NULL)
21722 arg = name;
21723 else
21724 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021725 if (arg != NULL && (fudi.fd_di == NULL
21726 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021727 {
21728 if (*arg == K_SPECIAL)
21729 j = 3;
21730 else
21731 j = 0;
21732 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21733 : eval_isnamec(arg[j])))
21734 ++j;
21735 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021736 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021737 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021738 /* Disallow using the g: dict. */
21739 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21740 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021741 }
21742
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 /*
21744 * Isolate the arguments: "arg1, arg2, ...)"
21745 */
21746 while (*p != ')')
21747 {
21748 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21749 {
21750 varargs = TRUE;
21751 p += 3;
21752 mustend = TRUE;
21753 }
21754 else
21755 {
21756 arg = p;
21757 while (ASCII_ISALNUM(*p) || *p == '_')
21758 ++p;
21759 if (arg == p || isdigit(*arg)
21760 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21761 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21762 {
21763 if (!eap->skip)
21764 EMSG2(_("E125: Illegal argument: %s"), arg);
21765 break;
21766 }
21767 if (ga_grow(&newargs, 1) == FAIL)
21768 goto erret;
21769 c = *p;
21770 *p = NUL;
21771 arg = vim_strsave(arg);
21772 if (arg == NULL)
21773 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021774
21775 /* Check for duplicate argument name. */
21776 for (i = 0; i < newargs.ga_len; ++i)
21777 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21778 {
21779 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021780 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021781 goto erret;
21782 }
21783
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21785 *p = c;
21786 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 if (*p == ',')
21788 ++p;
21789 else
21790 mustend = TRUE;
21791 }
21792 p = skipwhite(p);
21793 if (mustend && *p != ')')
21794 {
21795 if (!eap->skip)
21796 EMSG2(_(e_invarg2), eap->arg);
21797 break;
21798 }
21799 }
21800 ++p; /* skip the ')' */
21801
Bram Moolenaare9a41262005-01-15 22:18:47 +000021802 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 for (;;)
21804 {
21805 p = skipwhite(p);
21806 if (STRNCMP(p, "range", 5) == 0)
21807 {
21808 flags |= FC_RANGE;
21809 p += 5;
21810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021811 else if (STRNCMP(p, "dict", 4) == 0)
21812 {
21813 flags |= FC_DICT;
21814 p += 4;
21815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 else if (STRNCMP(p, "abort", 5) == 0)
21817 {
21818 flags |= FC_ABORT;
21819 p += 5;
21820 }
21821 else
21822 break;
21823 }
21824
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021825 /* When there is a line break use what follows for the function body.
21826 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21827 if (*p == '\n')
21828 line_arg = p + 1;
21829 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 EMSG(_(e_trailing));
21831
21832 /*
21833 * Read the body of the function, until ":endfunction" is found.
21834 */
21835 if (KeyTyped)
21836 {
21837 /* Check if the function already exists, don't let the user type the
21838 * whole function before telling him it doesn't work! For a script we
21839 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021840 if (!eap->skip && !eap->forceit)
21841 {
21842 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21843 EMSG(_(e_funcdict));
21844 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021845 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021848 if (!eap->skip && did_emsg)
21849 goto erret;
21850
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 msg_putchar('\n'); /* don't overwrite the function name */
21852 cmdline_row = msg_row;
21853 }
21854
21855 indent = 2;
21856 nesting = 0;
21857 for (;;)
21858 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021859 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021860 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021861 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021862 saved_wait_return = FALSE;
21863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021865 sourcing_lnum_off = sourcing_lnum;
21866
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021867 if (line_arg != NULL)
21868 {
21869 /* Use eap->arg, split up in parts by line breaks. */
21870 theline = line_arg;
21871 p = vim_strchr(theline, '\n');
21872 if (p == NULL)
21873 line_arg += STRLEN(line_arg);
21874 else
21875 {
21876 *p = NUL;
21877 line_arg = p + 1;
21878 }
21879 }
21880 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 theline = getcmdline(':', 0L, indent);
21882 else
21883 theline = eap->getline(':', eap->cookie, indent);
21884 if (KeyTyped)
21885 lines_left = Rows - 1;
21886 if (theline == NULL)
21887 {
21888 EMSG(_("E126: Missing :endfunction"));
21889 goto erret;
21890 }
21891
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021892 /* Detect line continuation: sourcing_lnum increased more than one. */
21893 if (sourcing_lnum > sourcing_lnum_off + 1)
21894 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21895 else
21896 sourcing_lnum_off = 0;
21897
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 if (skip_until != NULL)
21899 {
21900 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21901 * don't check for ":endfunc". */
21902 if (STRCMP(theline, skip_until) == 0)
21903 {
21904 vim_free(skip_until);
21905 skip_until = NULL;
21906 }
21907 }
21908 else
21909 {
21910 /* skip ':' and blanks*/
21911 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21912 ;
21913
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021914 /* Check for "endfunction". */
21915 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021917 if (line_arg == NULL)
21918 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 break;
21920 }
21921
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021922 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 * at "end". */
21924 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21925 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021926 else if (STRNCMP(p, "if", 2) == 0
21927 || STRNCMP(p, "wh", 2) == 0
21928 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 || STRNCMP(p, "try", 3) == 0)
21930 indent += 2;
21931
21932 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021933 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021935 if (*p == '!')
21936 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 p += eval_fname_script(p);
21938 if (ASCII_ISALPHA(*p))
21939 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021940 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 if (*skipwhite(p) == '(')
21942 {
21943 ++nesting;
21944 indent += 2;
21945 }
21946 }
21947 }
21948
21949 /* Check for ":append" or ":insert". */
21950 p = skip_range(p, NULL);
21951 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21952 || (p[0] == 'i'
21953 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21954 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21955 skip_until = vim_strsave((char_u *)".");
21956
21957 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21958 arg = skipwhite(skiptowhite(p));
21959 if (arg[0] == '<' && arg[1] =='<'
21960 && ((p[0] == 'p' && p[1] == 'y'
21961 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21962 || (p[0] == 'p' && p[1] == 'e'
21963 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21964 || (p[0] == 't' && p[1] == 'c'
21965 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021966 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21967 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21969 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021970 || (p[0] == 'm' && p[1] == 'z'
21971 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 ))
21973 {
21974 /* ":python <<" continues until a dot, like ":append" */
21975 p = skipwhite(arg + 2);
21976 if (*p == NUL)
21977 skip_until = vim_strsave((char_u *)".");
21978 else
21979 skip_until = vim_strsave(p);
21980 }
21981 }
21982
21983 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021984 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021985 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021986 if (line_arg == NULL)
21987 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021989 }
21990
21991 /* Copy the line to newly allocated memory. get_one_sourceline()
21992 * allocates 250 bytes per line, this saves 80% on average. The cost
21993 * is an extra alloc/free. */
21994 p = vim_strsave(theline);
21995 if (p != NULL)
21996 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021997 if (line_arg == NULL)
21998 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021999 theline = p;
22000 }
22001
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022002 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22003
22004 /* Add NULL lines for continuation lines, so that the line count is
22005 * equal to the index in the growarray. */
22006 while (sourcing_lnum_off-- > 0)
22007 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022008
22009 /* Check for end of eap->arg. */
22010 if (line_arg != NULL && *line_arg == NUL)
22011 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 }
22013
22014 /* Don't define the function when skipping commands or when an error was
22015 * detected. */
22016 if (eap->skip || did_emsg)
22017 goto erret;
22018
22019 /*
22020 * If there are no errors, add the function
22021 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022022 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022024 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022026 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022027 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022028 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022029 goto erret;
22030 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022032 fp = find_func(name);
22033 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022035 if (!eap->forceit)
22036 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022037 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022038 goto erret;
22039 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022040 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022041 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022042 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 name);
22044 goto erret;
22045 }
22046 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022047 ga_clear_strings(&(fp->uf_args));
22048 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022049 vim_free(name);
22050 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 }
22053 else
22054 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022055 char numbuf[20];
22056
22057 fp = NULL;
22058 if (fudi.fd_newkey == NULL && !eap->forceit)
22059 {
22060 EMSG(_(e_funcdict));
22061 goto erret;
22062 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022063 if (fudi.fd_di == NULL)
22064 {
22065 /* Can't add a function to a locked dictionary */
22066 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22067 goto erret;
22068 }
22069 /* Can't change an existing function if it is locked */
22070 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22071 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022072
22073 /* Give the function a sequential number. Can only be used with a
22074 * Funcref! */
22075 vim_free(name);
22076 sprintf(numbuf, "%d", ++func_nr);
22077 name = vim_strsave((char_u *)numbuf);
22078 if (name == NULL)
22079 goto erret;
22080 }
22081
22082 if (fp == NULL)
22083 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022084 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022085 {
22086 int slen, plen;
22087 char_u *scriptname;
22088
22089 /* Check that the autoload name matches the script name. */
22090 j = FAIL;
22091 if (sourcing_name != NULL)
22092 {
22093 scriptname = autoload_name(name);
22094 if (scriptname != NULL)
22095 {
22096 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022097 plen = (int)STRLEN(p);
22098 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022099 if (slen > plen && fnamecmp(p,
22100 sourcing_name + slen - plen) == 0)
22101 j = OK;
22102 vim_free(scriptname);
22103 }
22104 }
22105 if (j == FAIL)
22106 {
22107 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22108 goto erret;
22109 }
22110 }
22111
22112 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 if (fp == NULL)
22114 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022115
22116 if (fudi.fd_dict != NULL)
22117 {
22118 if (fudi.fd_di == NULL)
22119 {
22120 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022121 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022122 if (fudi.fd_di == NULL)
22123 {
22124 vim_free(fp);
22125 goto erret;
22126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022127 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22128 {
22129 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022130 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022131 goto erret;
22132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022133 }
22134 else
22135 /* overwrite existing dict entry */
22136 clear_tv(&fudi.fd_di->di_tv);
22137 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022138 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022139 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022140 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022141
22142 /* behave like "dict" was used */
22143 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022144 }
22145
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022147 STRCPY(fp->uf_name, name);
22148 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022150 fp->uf_args = newargs;
22151 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022152#ifdef FEAT_PROFILE
22153 fp->uf_tml_count = NULL;
22154 fp->uf_tml_total = NULL;
22155 fp->uf_tml_self = NULL;
22156 fp->uf_profiling = FALSE;
22157 if (prof_def_func())
22158 func_do_profile(fp);
22159#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022160 fp->uf_varargs = varargs;
22161 fp->uf_flags = flags;
22162 fp->uf_calls = 0;
22163 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022164 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165
22166erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 ga_clear_strings(&newargs);
22168 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022169ret_free:
22170 vim_free(skip_until);
22171 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022174 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175}
22176
22177/*
22178 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022179 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022181 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022182 * TFN_INT: internal function name OK
22183 * TFN_QUIET: be quiet
22184 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 * Advances "pp" to just after the function name (if no error).
22186 */
22187 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022188trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 char_u **pp;
22190 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022191 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022194 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 char_u *start;
22196 char_u *end;
22197 int lead;
22198 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022201
22202 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022205
22206 /* Check for hard coded <SNR>: already translated function ID (from a user
22207 * command). */
22208 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22209 && (*pp)[2] == (int)KE_SNR)
22210 {
22211 *pp += 3;
22212 len = get_id_len(pp) + 3;
22213 return vim_strnsave(start, len);
22214 }
22215
22216 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22217 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022219 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022221
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022222 /* Note that TFN_ flags use the same values as GLV_ flags. */
22223 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022224 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022225 if (end == start)
22226 {
22227 if (!skip)
22228 EMSG(_("E129: Function name required"));
22229 goto theend;
22230 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022231 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022232 {
22233 /*
22234 * Report an invalid expression in braces, unless the expression
22235 * evaluation has been cancelled due to an aborting error, an
22236 * interrupt, or an exception.
22237 */
22238 if (!aborting())
22239 {
22240 if (end != NULL)
22241 EMSG2(_(e_invarg2), start);
22242 }
22243 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022244 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022245 goto theend;
22246 }
22247
22248 if (lv.ll_tv != NULL)
22249 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022250 if (fdp != NULL)
22251 {
22252 fdp->fd_dict = lv.ll_dict;
22253 fdp->fd_newkey = lv.ll_newkey;
22254 lv.ll_newkey = NULL;
22255 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022257 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22258 {
22259 name = vim_strsave(lv.ll_tv->vval.v_string);
22260 *pp = end;
22261 }
22262 else
22263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022264 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22265 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022266 EMSG(_(e_funcref));
22267 else
22268 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022269 name = NULL;
22270 }
22271 goto theend;
22272 }
22273
22274 if (lv.ll_name == NULL)
22275 {
22276 /* Error found, but continue after the function name. */
22277 *pp = end;
22278 goto theend;
22279 }
22280
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022281 /* Check if the name is a Funcref. If so, use the value. */
22282 if (lv.ll_exp_name != NULL)
22283 {
22284 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022285 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022286 if (name == lv.ll_exp_name)
22287 name = NULL;
22288 }
22289 else
22290 {
22291 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022292 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022293 if (name == *pp)
22294 name = NULL;
22295 }
22296 if (name != NULL)
22297 {
22298 name = vim_strsave(name);
22299 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022300 if (STRNCMP(name, "<SNR>", 5) == 0)
22301 {
22302 /* Change "<SNR>" to the byte sequence. */
22303 name[0] = K_SPECIAL;
22304 name[1] = KS_EXTRA;
22305 name[2] = (int)KE_SNR;
22306 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22307 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022308 goto theend;
22309 }
22310
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022311 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022312 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022313 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022314 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22315 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22316 {
22317 /* When there was "s:" already or the name expanded to get a
22318 * leading "s:" then remove it. */
22319 lv.ll_name += 2;
22320 len -= 2;
22321 lead = 2;
22322 }
22323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022324 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022325 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022326 /* skip over "s:" and "g:" */
22327 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022328 lv.ll_name += 2;
22329 len = (int)(end - lv.ll_name);
22330 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022331
22332 /*
22333 * Copy the function name to allocated memory.
22334 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22335 * Accept <SNR>123_name() outside a script.
22336 */
22337 if (skip)
22338 lead = 0; /* do nothing */
22339 else if (lead > 0)
22340 {
22341 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022342 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22343 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022344 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022345 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022346 if (current_SID <= 0)
22347 {
22348 EMSG(_(e_usingsid));
22349 goto theend;
22350 }
22351 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22352 lead += (int)STRLEN(sid_buf);
22353 }
22354 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022355 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022356 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022357 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022358 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022359 goto theend;
22360 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022361 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022362 {
22363 char_u *cp = vim_strchr(lv.ll_name, ':');
22364
22365 if (cp != NULL && cp < end)
22366 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022367 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022368 goto theend;
22369 }
22370 }
22371
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022372 name = alloc((unsigned)(len + lead + 1));
22373 if (name != NULL)
22374 {
22375 if (lead > 0)
22376 {
22377 name[0] = K_SPECIAL;
22378 name[1] = KS_EXTRA;
22379 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022380 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022381 STRCPY(name + 3, sid_buf);
22382 }
22383 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022384 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022385 }
22386 *pp = end;
22387
22388theend:
22389 clear_lval(&lv);
22390 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391}
22392
22393/*
22394 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22395 * Return 2 if "p" starts with "s:".
22396 * Return 0 otherwise.
22397 */
22398 static int
22399eval_fname_script(p)
22400 char_u *p;
22401{
22402 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22403 || STRNICMP(p + 1, "SNR>", 4) == 0))
22404 return 5;
22405 if (p[0] == 's' && p[1] == ':')
22406 return 2;
22407 return 0;
22408}
22409
22410/*
22411 * Return TRUE if "p" starts with "<SID>" or "s:".
22412 * Only works if eval_fname_script() returned non-zero for "p"!
22413 */
22414 static int
22415eval_fname_sid(p)
22416 char_u *p;
22417{
22418 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22419}
22420
22421/*
22422 * List the head of the function: "name(arg1, arg2)".
22423 */
22424 static void
22425list_func_head(fp, indent)
22426 ufunc_T *fp;
22427 int indent;
22428{
22429 int j;
22430
22431 msg_start();
22432 if (indent)
22433 MSG_PUTS(" ");
22434 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022435 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 {
22437 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022438 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022441 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 {
22445 if (j)
22446 MSG_PUTS(", ");
22447 msg_puts(FUNCARG(fp, j));
22448 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022449 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 {
22451 if (j)
22452 MSG_PUTS(", ");
22453 MSG_PUTS("...");
22454 }
22455 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022456 if (fp->uf_flags & FC_ABORT)
22457 MSG_PUTS(" abort");
22458 if (fp->uf_flags & FC_RANGE)
22459 MSG_PUTS(" range");
22460 if (fp->uf_flags & FC_DICT)
22461 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022462 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022463 if (p_verbose > 0)
22464 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465}
22466
22467/*
22468 * Find a function by name, return pointer to it in ufuncs.
22469 * Return NULL for unknown function.
22470 */
22471 static ufunc_T *
22472find_func(name)
22473 char_u *name;
22474{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022475 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022477 hi = hash_find(&func_hashtab, name);
22478 if (!HASHITEM_EMPTY(hi))
22479 return HI2UF(hi);
22480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481}
22482
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022483#if defined(EXITFREE) || defined(PROTO)
22484 void
22485free_all_functions()
22486{
22487 hashitem_T *hi;
22488
22489 /* Need to start all over every time, because func_free() may change the
22490 * hash table. */
22491 while (func_hashtab.ht_used > 0)
22492 for (hi = func_hashtab.ht_array; ; ++hi)
22493 if (!HASHITEM_EMPTY(hi))
22494 {
22495 func_free(HI2UF(hi));
22496 break;
22497 }
22498}
22499#endif
22500
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022501 int
22502translated_function_exists(name)
22503 char_u *name;
22504{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022505 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022506 return find_internal_func(name) >= 0;
22507 return find_func(name) != NULL;
22508}
22509
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510/*
22511 * Return TRUE if a function "name" exists.
22512 */
22513 static int
22514function_exists(name)
22515 char_u *name;
22516{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022517 char_u *nm = name;
22518 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022519 int n = FALSE;
22520
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022521 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22522 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022523 nm = skipwhite(nm);
22524
22525 /* Only accept "funcname", "funcname ", "funcname (..." and
22526 * "funcname(...", not "funcname!...". */
22527 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022528 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022529 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022530 return n;
22531}
22532
Bram Moolenaara1544c02013-05-30 12:35:52 +020022533 char_u *
22534get_expanded_name(name, check)
22535 char_u *name;
22536 int check;
22537{
22538 char_u *nm = name;
22539 char_u *p;
22540
22541 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22542
22543 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022544 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022545 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022546
Bram Moolenaara1544c02013-05-30 12:35:52 +020022547 vim_free(p);
22548 return NULL;
22549}
22550
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022551/*
22552 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022553 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22554 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022555 */
22556 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022557builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022558 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022559 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022560{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022561 char_u *p;
22562
22563 if (!ASCII_ISLOWER(name[0]))
22564 return FALSE;
22565 p = vim_strchr(name, AUTOLOAD_CHAR);
22566 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022567}
22568
Bram Moolenaar05159a02005-02-26 23:04:13 +000022569#if defined(FEAT_PROFILE) || defined(PROTO)
22570/*
22571 * Start profiling function "fp".
22572 */
22573 static void
22574func_do_profile(fp)
22575 ufunc_T *fp;
22576{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022577 int len = fp->uf_lines.ga_len;
22578
22579 if (len == 0)
22580 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022581 fp->uf_tm_count = 0;
22582 profile_zero(&fp->uf_tm_self);
22583 profile_zero(&fp->uf_tm_total);
22584 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022585 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022586 if (fp->uf_tml_total == NULL)
22587 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022588 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022589 if (fp->uf_tml_self == NULL)
22590 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022591 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592 fp->uf_tml_idx = -1;
22593 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22594 || fp->uf_tml_self == NULL)
22595 return; /* out of memory */
22596
22597 fp->uf_profiling = TRUE;
22598}
22599
22600/*
22601 * Dump the profiling results for all functions in file "fd".
22602 */
22603 void
22604func_dump_profile(fd)
22605 FILE *fd;
22606{
22607 hashitem_T *hi;
22608 int todo;
22609 ufunc_T *fp;
22610 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022611 ufunc_T **sorttab;
22612 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022614 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022615 if (todo == 0)
22616 return; /* nothing to dump */
22617
Bram Moolenaar73830342005-02-28 22:48:19 +000022618 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22619
Bram Moolenaar05159a02005-02-26 23:04:13 +000022620 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22621 {
22622 if (!HASHITEM_EMPTY(hi))
22623 {
22624 --todo;
22625 fp = HI2UF(hi);
22626 if (fp->uf_profiling)
22627 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022628 if (sorttab != NULL)
22629 sorttab[st_len++] = fp;
22630
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631 if (fp->uf_name[0] == K_SPECIAL)
22632 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22633 else
22634 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22635 if (fp->uf_tm_count == 1)
22636 fprintf(fd, "Called 1 time\n");
22637 else
22638 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22639 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22640 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22641 fprintf(fd, "\n");
22642 fprintf(fd, "count total (s) self (s)\n");
22643
22644 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22645 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022646 if (FUNCLINE(fp, i) == NULL)
22647 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022648 prof_func_line(fd, fp->uf_tml_count[i],
22649 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022650 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22651 }
22652 fprintf(fd, "\n");
22653 }
22654 }
22655 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022656
22657 if (sorttab != NULL && st_len > 0)
22658 {
22659 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22660 prof_total_cmp);
22661 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22662 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22663 prof_self_cmp);
22664 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22665 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022666
22667 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022668}
Bram Moolenaar73830342005-02-28 22:48:19 +000022669
22670 static void
22671prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22672 FILE *fd;
22673 ufunc_T **sorttab;
22674 int st_len;
22675 char *title;
22676 int prefer_self; /* when equal print only self time */
22677{
22678 int i;
22679 ufunc_T *fp;
22680
22681 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22682 fprintf(fd, "count total (s) self (s) function\n");
22683 for (i = 0; i < 20 && i < st_len; ++i)
22684 {
22685 fp = sorttab[i];
22686 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22687 prefer_self);
22688 if (fp->uf_name[0] == K_SPECIAL)
22689 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22690 else
22691 fprintf(fd, " %s()\n", fp->uf_name);
22692 }
22693 fprintf(fd, "\n");
22694}
22695
22696/*
22697 * Print the count and times for one function or function line.
22698 */
22699 static void
22700prof_func_line(fd, count, total, self, prefer_self)
22701 FILE *fd;
22702 int count;
22703 proftime_T *total;
22704 proftime_T *self;
22705 int prefer_self; /* when equal print only self time */
22706{
22707 if (count > 0)
22708 {
22709 fprintf(fd, "%5d ", count);
22710 if (prefer_self && profile_equal(total, self))
22711 fprintf(fd, " ");
22712 else
22713 fprintf(fd, "%s ", profile_msg(total));
22714 if (!prefer_self && profile_equal(total, self))
22715 fprintf(fd, " ");
22716 else
22717 fprintf(fd, "%s ", profile_msg(self));
22718 }
22719 else
22720 fprintf(fd, " ");
22721}
22722
22723/*
22724 * Compare function for total time sorting.
22725 */
22726 static int
22727#ifdef __BORLANDC__
22728_RTLENTRYF
22729#endif
22730prof_total_cmp(s1, s2)
22731 const void *s1;
22732 const void *s2;
22733{
22734 ufunc_T *p1, *p2;
22735
22736 p1 = *(ufunc_T **)s1;
22737 p2 = *(ufunc_T **)s2;
22738 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22739}
22740
22741/*
22742 * Compare function for self time sorting.
22743 */
22744 static int
22745#ifdef __BORLANDC__
22746_RTLENTRYF
22747#endif
22748prof_self_cmp(s1, s2)
22749 const void *s1;
22750 const void *s2;
22751{
22752 ufunc_T *p1, *p2;
22753
22754 p1 = *(ufunc_T **)s1;
22755 p2 = *(ufunc_T **)s2;
22756 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22757}
22758
Bram Moolenaar05159a02005-02-26 23:04:13 +000022759#endif
22760
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022761/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022762 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022763 * Return TRUE if a package was loaded.
22764 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022765 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022766script_autoload(name, reload)
22767 char_u *name;
22768 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022769{
22770 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022771 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022772 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022773 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022774
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022775 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022776 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022777 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022778 return FALSE;
22779
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022780 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022781
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022782 /* Find the name in the list of previously loaded package names. Skip
22783 * "autoload/", it's always the same. */
22784 for (i = 0; i < ga_loaded.ga_len; ++i)
22785 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22786 break;
22787 if (!reload && i < ga_loaded.ga_len)
22788 ret = FALSE; /* was loaded already */
22789 else
22790 {
22791 /* Remember the name if it wasn't loaded already. */
22792 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22793 {
22794 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22795 tofree = NULL;
22796 }
22797
22798 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022799 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022800 ret = TRUE;
22801 }
22802
22803 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022804 return ret;
22805}
22806
22807/*
22808 * Return the autoload script name for a function or variable name.
22809 * Returns NULL when out of memory.
22810 */
22811 static char_u *
22812autoload_name(name)
22813 char_u *name;
22814{
22815 char_u *p;
22816 char_u *scriptname;
22817
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022818 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022819 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22820 if (scriptname == NULL)
22821 return FALSE;
22822 STRCPY(scriptname, "autoload/");
22823 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022824 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022825 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022826 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022827 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022828 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022829}
22830
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22832
22833/*
22834 * Function given to ExpandGeneric() to obtain the list of user defined
22835 * function names.
22836 */
22837 char_u *
22838get_user_func_name(xp, idx)
22839 expand_T *xp;
22840 int idx;
22841{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022842 static long_u done;
22843 static hashitem_T *hi;
22844 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845
22846 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 done = 0;
22849 hi = func_hashtab.ht_array;
22850 }
22851 if (done < func_hashtab.ht_used)
22852 {
22853 if (done++ > 0)
22854 ++hi;
22855 while (HASHITEM_EMPTY(hi))
22856 ++hi;
22857 fp = HI2UF(hi);
22858
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022859 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022860 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022861
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022862 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22863 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864
22865 cat_func_name(IObuff, fp);
22866 if (xp->xp_context != EXPAND_USER_FUNC)
22867 {
22868 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022869 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 STRCAT(IObuff, ")");
22871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 return IObuff;
22873 }
22874 return NULL;
22875}
22876
22877#endif /* FEAT_CMDL_COMPL */
22878
22879/*
22880 * Copy the function name of "fp" to buffer "buf".
22881 * "buf" must be able to hold the function name plus three bytes.
22882 * Takes care of script-local function names.
22883 */
22884 static void
22885cat_func_name(buf, fp)
22886 char_u *buf;
22887 ufunc_T *fp;
22888{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022889 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 {
22891 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022892 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 }
22894 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022895 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896}
22897
22898/*
22899 * ":delfunction {name}"
22900 */
22901 void
22902ex_delfunction(eap)
22903 exarg_T *eap;
22904{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 char_u *p;
22907 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
22910 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022911 name = trans_function_name(&p, eap->skip, 0, &fudi);
22912 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022914 {
22915 if (fudi.fd_dict != NULL && !eap->skip)
22916 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 if (!ends_excmd(*skipwhite(p)))
22920 {
22921 vim_free(name);
22922 EMSG(_(e_trailing));
22923 return;
22924 }
22925 eap->nextcmd = check_nextcmd(p);
22926 if (eap->nextcmd != NULL)
22927 *p = NUL;
22928
22929 if (!eap->skip)
22930 fp = find_func(name);
22931 vim_free(name);
22932
22933 if (!eap->skip)
22934 {
22935 if (fp == NULL)
22936 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022937 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 return;
22939 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022940 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 {
22942 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22943 return;
22944 }
22945
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022948 /* Delete the dict item that refers to the function, it will
22949 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022950 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022952 else
22953 func_free(fp);
22954 }
22955}
22956
22957/*
22958 * Free a function and remove it from the list of functions.
22959 */
22960 static void
22961func_free(fp)
22962 ufunc_T *fp;
22963{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022964 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022965
22966 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022967 ga_clear_strings(&(fp->uf_args));
22968 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022969#ifdef FEAT_PROFILE
22970 vim_free(fp->uf_tml_count);
22971 vim_free(fp->uf_tml_total);
22972 vim_free(fp->uf_tml_self);
22973#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022975 /* remove the function from the function hashtable */
22976 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22977 if (HASHITEM_EMPTY(hi))
22978 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022980 hash_remove(&func_hashtab, hi);
22981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 vim_free(fp);
22983}
22984
22985/*
22986 * Unreference a Function: decrement the reference count and free it when it
22987 * becomes zero. Only for numbered functions.
22988 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022989 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022990func_unref(name)
22991 char_u *name;
22992{
22993 ufunc_T *fp;
22994
22995 if (name != NULL && isdigit(*name))
22996 {
22997 fp = find_func(name);
22998 if (fp == NULL)
22999 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023000 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023001 {
23002 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023003 * when "uf_calls" becomes zero. */
23004 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023005 func_free(fp);
23006 }
23007 }
23008}
23009
23010/*
23011 * Count a reference to a Function.
23012 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023013 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023014func_ref(name)
23015 char_u *name;
23016{
23017 ufunc_T *fp;
23018
23019 if (name != NULL && isdigit(*name))
23020 {
23021 fp = find_func(name);
23022 if (fp == NULL)
23023 EMSG2(_(e_intern2), "func_ref()");
23024 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023025 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 }
23027}
23028
23029/*
23030 * Call a user function.
23031 */
23032 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023033call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 ufunc_T *fp; /* pointer to function */
23035 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 typval_T *argvars; /* arguments */
23037 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 linenr_T firstline; /* first line of range */
23039 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041{
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 char_u *save_sourcing_name;
23043 linenr_T save_sourcing_lnum;
23044 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023045 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023046 int save_did_emsg;
23047 static int depth = 0;
23048 dictitem_T *v;
23049 int fixvar_idx = 0; /* index in fixvar[] */
23050 int i;
23051 int ai;
23052 char_u numbuf[NUMBUFLEN];
23053 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023054#ifdef FEAT_PROFILE
23055 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023056 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058
23059 /* If depth of calling is getting too high, don't execute the function */
23060 if (depth >= p_mfd)
23061 {
23062 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023063 rettv->v_type = VAR_NUMBER;
23064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 return;
23066 }
23067 ++depth;
23068
23069 line_breakcheck(); /* check for CTRL-C hit */
23070
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023071 fc = (funccall_T *)alloc(sizeof(funccall_T));
23072 fc->caller = current_funccal;
23073 current_funccal = fc;
23074 fc->func = fp;
23075 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023076 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023077 fc->linenr = 0;
23078 fc->returned = FALSE;
23079 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023081 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23082 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023085 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23087 * each argument variable and saves a lot of time.
23088 */
23089 /*
23090 * Init l: variables.
23091 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023092 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023093 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023094 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023095 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23096 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023097 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023098 name = v->di_key;
23099 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023101 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023103 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023104 v->di_tv.vval.v_dict = selfdict;
23105 ++selfdict->dv_refcount;
23106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023107
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 /*
23109 * Init a: variables.
23110 * Set a:0 to "argcount".
23111 * Set a:000 to a list with room for the "..." arguments.
23112 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023113 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023114 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023115 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023116 /* Use "name" to avoid a warning from some compiler that checks the
23117 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023118 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023119 name = v->di_key;
23120 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023121 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023122 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023124 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023125 v->di_tv.vval.v_list = &fc->l_varlist;
23126 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23127 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23128 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023129
23130 /*
23131 * Set a:firstline to "firstline" and a:lastline to "lastline".
23132 * Set a:name to named arguments.
23133 * Set a:N to the "..." arguments.
23134 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023135 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023136 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 (varnumber_T)lastline);
23139 for (i = 0; i < argcount; ++i)
23140 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023141 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 if (ai < 0)
23143 /* named argument a:name */
23144 name = FUNCARG(fp, i);
23145 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023146 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 /* "..." argument a:1, a:2, etc. */
23148 sprintf((char *)numbuf, "%d", ai + 1);
23149 name = numbuf;
23150 }
23151 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23152 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023153 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23155 }
23156 else
23157 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023158 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23159 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 if (v == NULL)
23161 break;
23162 v->di_flags = DI_FLAGS_RO;
23163 }
23164 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023165 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023166
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023167 /* Note: the values are copied directly to avoid alloc/free.
23168 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023170 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023171
23172 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23173 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023174 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23175 fc->l_listitems[ai].li_tv = argvars[i];
23176 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023177 }
23178 }
23179
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 /* Don't redraw while executing the function. */
23181 ++RedrawingDisabled;
23182 save_sourcing_name = sourcing_name;
23183 save_sourcing_lnum = sourcing_lnum;
23184 sourcing_lnum = 1;
23185 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023186 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 if (sourcing_name != NULL)
23188 {
23189 if (save_sourcing_name != NULL
23190 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23191 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23192 else
23193 STRCPY(sourcing_name, "function ");
23194 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23195
23196 if (p_verbose >= 12)
23197 {
23198 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023199 verbose_enter_scroll();
23200
Bram Moolenaar555b2802005-05-19 21:08:39 +000023201 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 if (p_verbose >= 14)
23203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023205 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023206 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023207 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208
23209 msg_puts((char_u *)"(");
23210 for (i = 0; i < argcount; ++i)
23211 {
23212 if (i > 0)
23213 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023214 if (argvars[i].v_type == VAR_NUMBER)
23215 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 else
23217 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023218 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23219 if (s != NULL)
23220 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023221 if (vim_strsize(s) > MSG_BUF_CLEN)
23222 {
23223 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23224 s = buf;
23225 }
23226 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023227 vim_free(tofree);
23228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 }
23230 }
23231 msg_puts((char_u *)")");
23232 }
23233 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023234
23235 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 --no_wait_return;
23237 }
23238 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023239#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023240 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023241 {
23242 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23243 func_do_profile(fp);
23244 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023245 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023246 {
23247 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023248 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023249 profile_zero(&fp->uf_tm_children);
23250 }
23251 script_prof_save(&wait_start);
23252 }
23253#endif
23254
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023256 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 save_did_emsg = did_emsg;
23258 did_emsg = FALSE;
23259
23260 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023261 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23263
23264 --RedrawingDisabled;
23265
23266 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023267 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023269 clear_tv(rettv);
23270 rettv->v_type = VAR_NUMBER;
23271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 }
23273
Bram Moolenaar05159a02005-02-26 23:04:13 +000023274#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023275 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023276 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023277 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023278 profile_end(&call_start);
23279 profile_sub_wait(&wait_start, &call_start);
23280 profile_add(&fp->uf_tm_total, &call_start);
23281 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023282 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023283 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023284 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23285 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023286 }
23287 }
23288#endif
23289
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 /* when being verbose, mention the return value */
23291 if (p_verbose >= 12)
23292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023294 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023297 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023298 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023299 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023300 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023303 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023304 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023305 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023306 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023307
Bram Moolenaar555b2802005-05-19 21:08:39 +000023308 /* The value may be very long. Skip the middle part, so that we
23309 * have some idea how it starts and ends. smsg() would always
23310 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023311 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023312 if (s != NULL)
23313 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023314 if (vim_strsize(s) > MSG_BUF_CLEN)
23315 {
23316 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23317 s = buf;
23318 }
23319 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023320 vim_free(tofree);
23321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 }
23323 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023324
23325 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 --no_wait_return;
23327 }
23328
23329 vim_free(sourcing_name);
23330 sourcing_name = save_sourcing_name;
23331 sourcing_lnum = save_sourcing_lnum;
23332 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023333#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023334 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023335 script_prof_restore(&wait_start);
23336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337
23338 if (p_verbose >= 12 && sourcing_name != NULL)
23339 {
23340 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023341 verbose_enter_scroll();
23342
Bram Moolenaar555b2802005-05-19 21:08:39 +000023343 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023345
23346 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 --no_wait_return;
23348 }
23349
23350 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023351 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023353
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023354 /* If the a:000 list and the l: and a: dicts are not referenced we can
23355 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023356 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23357 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23358 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23359 {
23360 free_funccal(fc, FALSE);
23361 }
23362 else
23363 {
23364 hashitem_T *hi;
23365 listitem_T *li;
23366 int todo;
23367
23368 /* "fc" is still in use. This can happen when returning "a:000" or
23369 * assigning "l:" to a global variable.
23370 * Link "fc" in the list for garbage collection later. */
23371 fc->caller = previous_funccal;
23372 previous_funccal = fc;
23373
23374 /* Make a copy of the a: variables, since we didn't do that above. */
23375 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23376 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23377 {
23378 if (!HASHITEM_EMPTY(hi))
23379 {
23380 --todo;
23381 v = HI2DI(hi);
23382 copy_tv(&v->di_tv, &v->di_tv);
23383 }
23384 }
23385
23386 /* Make a copy of the a:000 items, since we didn't do that above. */
23387 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23388 copy_tv(&li->li_tv, &li->li_tv);
23389 }
23390}
23391
23392/*
23393 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023394 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023395 */
23396 static int
23397can_free_funccal(fc, copyID)
23398 funccall_T *fc;
23399 int copyID;
23400{
23401 return (fc->l_varlist.lv_copyID != copyID
23402 && fc->l_vars.dv_copyID != copyID
23403 && fc->l_avars.dv_copyID != copyID);
23404}
23405
23406/*
23407 * Free "fc" and what it contains.
23408 */
23409 static void
23410free_funccal(fc, free_val)
23411 funccall_T *fc;
23412 int free_val; /* a: vars were allocated */
23413{
23414 listitem_T *li;
23415
23416 /* The a: variables typevals may not have been allocated, only free the
23417 * allocated variables. */
23418 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23419
23420 /* free all l: variables */
23421 vars_clear(&fc->l_vars.dv_hashtab);
23422
23423 /* Free the a:000 variables if they were allocated. */
23424 if (free_val)
23425 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23426 clear_tv(&li->li_tv);
23427
23428 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429}
23430
23431/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023432 * Add a number variable "name" to dict "dp" with value "nr".
23433 */
23434 static void
23435add_nr_var(dp, v, name, nr)
23436 dict_T *dp;
23437 dictitem_T *v;
23438 char *name;
23439 varnumber_T nr;
23440{
23441 STRCPY(v->di_key, name);
23442 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23443 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23444 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023445 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023446 v->di_tv.vval.v_number = nr;
23447}
23448
23449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 * ":return [expr]"
23451 */
23452 void
23453ex_return(eap)
23454 exarg_T *eap;
23455{
23456 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 int returning = FALSE;
23459
23460 if (current_funccal == NULL)
23461 {
23462 EMSG(_("E133: :return not inside a function"));
23463 return;
23464 }
23465
23466 if (eap->skip)
23467 ++emsg_skip;
23468
23469 eap->nextcmd = NULL;
23470 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023471 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 {
23473 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023474 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 }
23478 /* It's safer to return also on error. */
23479 else if (!eap->skip)
23480 {
23481 /*
23482 * Return unless the expression evaluation has been cancelled due to an
23483 * aborting error, an interrupt, or an exception.
23484 */
23485 if (!aborting())
23486 returning = do_return(eap, FALSE, TRUE, NULL);
23487 }
23488
23489 /* When skipping or the return gets pending, advance to the next command
23490 * in this line (!returning). Otherwise, ignore the rest of the line.
23491 * Following lines will be ignored by get_func_line(). */
23492 if (returning)
23493 eap->nextcmd = NULL;
23494 else if (eap->nextcmd == NULL) /* no argument */
23495 eap->nextcmd = check_nextcmd(arg);
23496
23497 if (eap->skip)
23498 --emsg_skip;
23499}
23500
23501/*
23502 * Return from a function. Possibly makes the return pending. Also called
23503 * for a pending return at the ":endtry" or after returning from an extra
23504 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023505 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023506 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 * FALSE when the return gets pending.
23508 */
23509 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023510do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 exarg_T *eap;
23512 int reanimate;
23513 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023514 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515{
23516 int idx;
23517 struct condstack *cstack = eap->cstack;
23518
23519 if (reanimate)
23520 /* Undo the return. */
23521 current_funccal->returned = FALSE;
23522
23523 /*
23524 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23525 * not in its finally clause (which then is to be executed next) is found.
23526 * In this case, make the ":return" pending for execution at the ":endtry".
23527 * Otherwise, return normally.
23528 */
23529 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23530 if (idx >= 0)
23531 {
23532 cstack->cs_pending[idx] = CSTP_RETURN;
23533
23534 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023535 /* A pending return again gets pending. "rettv" points to an
23536 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023538 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 else
23540 {
23541 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023542 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023544 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023546 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 {
23548 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023549 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023550 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 else
23552 EMSG(_(e_outofmem));
23553 }
23554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023555 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556
23557 if (reanimate)
23558 {
23559 /* The pending return value could be overwritten by a ":return"
23560 * without argument in a finally clause; reset the default
23561 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023562 current_funccal->rettv->v_type = VAR_NUMBER;
23563 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 }
23565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023566 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 }
23568 else
23569 {
23570 current_funccal->returned = TRUE;
23571
23572 /* If the return is carried out now, store the return value. For
23573 * a return immediately after reanimation, the value is already
23574 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023575 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023577 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023580 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 }
23582 }
23583
23584 return idx < 0;
23585}
23586
23587/*
23588 * Free the variable with a pending return value.
23589 */
23590 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023591discard_pending_return(rettv)
23592 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593{
Bram Moolenaar33570922005-01-25 22:26:29 +000023594 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595}
23596
23597/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023598 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 * is an allocated string. Used by report_pending() for verbose messages.
23600 */
23601 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023602get_return_cmd(rettv)
23603 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023605 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023606 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023607 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023609 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023610 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023611 if (s == NULL)
23612 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023613
23614 STRCPY(IObuff, ":return ");
23615 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23616 if (STRLEN(s) + 8 >= IOSIZE)
23617 STRCPY(IObuff + IOSIZE - 4, "...");
23618 vim_free(tofree);
23619 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620}
23621
23622/*
23623 * Get next function line.
23624 * Called by do_cmdline() to get the next line.
23625 * Returns allocated string, or NULL for end of function.
23626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 char_u *
23628get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023629 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023631 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632{
Bram Moolenaar33570922005-01-25 22:26:29 +000023633 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023634 ufunc_T *fp = fcp->func;
23635 char_u *retval;
23636 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637
23638 /* If breakpoints have been added/deleted need to check for it. */
23639 if (fcp->dbg_tick != debug_tick)
23640 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023641 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 sourcing_lnum);
23643 fcp->dbg_tick = debug_tick;
23644 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023645#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023646 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023647 func_line_end(cookie);
23648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649
Bram Moolenaar05159a02005-02-26 23:04:13 +000023650 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023651 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23652 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 retval = NULL;
23654 else
23655 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023656 /* Skip NULL lines (continuation lines). */
23657 while (fcp->linenr < gap->ga_len
23658 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23659 ++fcp->linenr;
23660 if (fcp->linenr >= gap->ga_len)
23661 retval = NULL;
23662 else
23663 {
23664 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23665 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023666#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023667 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023668 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023669#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 }
23672
23673 /* Did we encounter a breakpoint? */
23674 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23675 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023676 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023678 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 sourcing_lnum);
23680 fcp->dbg_tick = debug_tick;
23681 }
23682
23683 return retval;
23684}
23685
Bram Moolenaar05159a02005-02-26 23:04:13 +000023686#if defined(FEAT_PROFILE) || defined(PROTO)
23687/*
23688 * Called when starting to read a function line.
23689 * "sourcing_lnum" must be correct!
23690 * When skipping lines it may not actually be executed, but we won't find out
23691 * until later and we need to store the time now.
23692 */
23693 void
23694func_line_start(cookie)
23695 void *cookie;
23696{
23697 funccall_T *fcp = (funccall_T *)cookie;
23698 ufunc_T *fp = fcp->func;
23699
23700 if (fp->uf_profiling && sourcing_lnum >= 1
23701 && sourcing_lnum <= fp->uf_lines.ga_len)
23702 {
23703 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023704 /* Skip continuation lines. */
23705 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23706 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023707 fp->uf_tml_execed = FALSE;
23708 profile_start(&fp->uf_tml_start);
23709 profile_zero(&fp->uf_tml_children);
23710 profile_get_wait(&fp->uf_tml_wait);
23711 }
23712}
23713
23714/*
23715 * Called when actually executing a function line.
23716 */
23717 void
23718func_line_exec(cookie)
23719 void *cookie;
23720{
23721 funccall_T *fcp = (funccall_T *)cookie;
23722 ufunc_T *fp = fcp->func;
23723
23724 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23725 fp->uf_tml_execed = TRUE;
23726}
23727
23728/*
23729 * Called when done with a function line.
23730 */
23731 void
23732func_line_end(cookie)
23733 void *cookie;
23734{
23735 funccall_T *fcp = (funccall_T *)cookie;
23736 ufunc_T *fp = fcp->func;
23737
23738 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23739 {
23740 if (fp->uf_tml_execed)
23741 {
23742 ++fp->uf_tml_count[fp->uf_tml_idx];
23743 profile_end(&fp->uf_tml_start);
23744 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023745 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023746 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23747 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023748 }
23749 fp->uf_tml_idx = -1;
23750 }
23751}
23752#endif
23753
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754/*
23755 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023756 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 */
23758 int
23759func_has_ended(cookie)
23760 void *cookie;
23761{
Bram Moolenaar33570922005-01-25 22:26:29 +000023762 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763
23764 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23765 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023766 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 || fcp->returned);
23768}
23769
23770/*
23771 * return TRUE if cookie indicates a function which "abort"s on errors.
23772 */
23773 int
23774func_has_abort(cookie)
23775 void *cookie;
23776{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023777 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778}
23779
23780#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23781typedef enum
23782{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023783 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23784 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23785 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786} var_flavour_T;
23787
23788static var_flavour_T var_flavour __ARGS((char_u *varname));
23789
23790 static var_flavour_T
23791var_flavour(varname)
23792 char_u *varname;
23793{
23794 char_u *p = varname;
23795
23796 if (ASCII_ISUPPER(*p))
23797 {
23798 while (*(++p))
23799 if (ASCII_ISLOWER(*p))
23800 return VAR_FLAVOUR_SESSION;
23801 return VAR_FLAVOUR_VIMINFO;
23802 }
23803 else
23804 return VAR_FLAVOUR_DEFAULT;
23805}
23806#endif
23807
23808#if defined(FEAT_VIMINFO) || defined(PROTO)
23809/*
23810 * Restore global vars that start with a capital from the viminfo file
23811 */
23812 int
23813read_viminfo_varlist(virp, writing)
23814 vir_T *virp;
23815 int writing;
23816{
23817 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023818 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820
23821 if (!writing && (find_viminfo_parameter('!') != NULL))
23822 {
23823 tab = vim_strchr(virp->vir_line + 1, '\t');
23824 if (tab != NULL)
23825 {
23826 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023827 switch (*tab)
23828 {
23829 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023830#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023831 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023832#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023833 case 'D': type = VAR_DICT; break;
23834 case 'L': type = VAR_LIST; break;
23835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836
23837 tab = vim_strchr(tab, '\t');
23838 if (tab != NULL)
23839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023840 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023841 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023842 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023844#ifdef FEAT_FLOAT
23845 else if (type == VAR_FLOAT)
23846 (void)string2float(tab + 1, &tv.vval.v_float);
23847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023849 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023850 if (type == VAR_DICT || type == VAR_LIST)
23851 {
23852 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23853
23854 if (etv == NULL)
23855 /* Failed to parse back the dict or list, use it as a
23856 * string. */
23857 tv.v_type = VAR_STRING;
23858 else
23859 {
23860 vim_free(tv.vval.v_string);
23861 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023862 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023863 }
23864 }
23865
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023866 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023867
23868 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023869 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023870 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 }
23873 }
23874 }
23875
23876 return viminfo_readline(virp);
23877}
23878
23879/*
23880 * Write global vars that start with a capital to the viminfo file
23881 */
23882 void
23883write_viminfo_varlist(fp)
23884 FILE *fp;
23885{
Bram Moolenaar33570922005-01-25 22:26:29 +000023886 hashitem_T *hi;
23887 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023888 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023889 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023890 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023891 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023892 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893
23894 if (find_viminfo_parameter('!') == NULL)
23895 return;
23896
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023897 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023899 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023902 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023904 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023905 this_var = HI2DI(hi);
23906 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023907 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023908 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023909 {
23910 case VAR_STRING: s = "STR"; break;
23911 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023912#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023913 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023914#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023915 case VAR_DICT: s = "DIC"; break;
23916 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023917 default: continue;
23918 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023920 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023921 if (p != NULL)
23922 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023923 vim_free(tofree);
23924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 }
23926 }
23927}
23928#endif
23929
23930#if defined(FEAT_SESSION) || defined(PROTO)
23931 int
23932store_session_globals(fd)
23933 FILE *fd;
23934{
Bram Moolenaar33570922005-01-25 22:26:29 +000023935 hashitem_T *hi;
23936 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023937 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 char_u *p, *t;
23939
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023940 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023941 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023943 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023945 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023946 this_var = HI2DI(hi);
23947 if ((this_var->di_tv.v_type == VAR_NUMBER
23948 || this_var->di_tv.v_type == VAR_STRING)
23949 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023950 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023951 /* Escape special characters with a backslash. Turn a LF and
23952 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023953 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023954 (char_u *)"\\\"\n\r");
23955 if (p == NULL) /* out of memory */
23956 break;
23957 for (t = p; *t != NUL; ++t)
23958 if (*t == '\n')
23959 *t = 'n';
23960 else if (*t == '\r')
23961 *t = 'r';
23962 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023963 this_var->di_key,
23964 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23965 : ' ',
23966 p,
23967 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23968 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023969 || put_eol(fd) == FAIL)
23970 {
23971 vim_free(p);
23972 return FAIL;
23973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 vim_free(p);
23975 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023976#ifdef FEAT_FLOAT
23977 else if (this_var->di_tv.v_type == VAR_FLOAT
23978 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23979 {
23980 float_T f = this_var->di_tv.vval.v_float;
23981 int sign = ' ';
23982
23983 if (f < 0)
23984 {
23985 f = -f;
23986 sign = '-';
23987 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023988 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023989 this_var->di_key, sign, f) < 0)
23990 || put_eol(fd) == FAIL)
23991 return FAIL;
23992 }
23993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 }
23995 }
23996 return OK;
23997}
23998#endif
23999
Bram Moolenaar661b1822005-07-28 22:36:45 +000024000/*
24001 * Display script name where an item was last set.
24002 * Should only be invoked when 'verbose' is non-zero.
24003 */
24004 void
24005last_set_msg(scriptID)
24006 scid_T scriptID;
24007{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024008 char_u *p;
24009
Bram Moolenaar661b1822005-07-28 22:36:45 +000024010 if (scriptID != 0)
24011 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024012 p = home_replace_save(NULL, get_scriptname(scriptID));
24013 if (p != NULL)
24014 {
24015 verbose_enter();
24016 MSG_PUTS(_("\n\tLast set from "));
24017 MSG_PUTS(p);
24018 vim_free(p);
24019 verbose_leave();
24020 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024021 }
24022}
24023
Bram Moolenaard812df62008-11-09 12:46:09 +000024024/*
24025 * List v:oldfiles in a nice way.
24026 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024027 void
24028ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024029 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024030{
24031 list_T *l = vimvars[VV_OLDFILES].vv_list;
24032 listitem_T *li;
24033 int nr = 0;
24034
24035 if (l == NULL)
24036 msg((char_u *)_("No old files"));
24037 else
24038 {
24039 msg_start();
24040 msg_scroll = TRUE;
24041 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24042 {
24043 msg_outnum((long)++nr);
24044 MSG_PUTS(": ");
24045 msg_outtrans(get_tv_string(&li->li_tv));
24046 msg_putchar('\n');
24047 out_flush(); /* output one line at a time */
24048 ui_breakcheck();
24049 }
24050 /* Assume "got_int" was set to truncate the listing. */
24051 got_int = FALSE;
24052
24053#ifdef FEAT_BROWSE_CMD
24054 if (cmdmod.browse)
24055 {
24056 quit_more = FALSE;
24057 nr = prompt_for_number(FALSE);
24058 msg_starthere();
24059 if (nr > 0)
24060 {
24061 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24062 (long)nr);
24063
24064 if (p != NULL)
24065 {
24066 p = expand_env_save(p);
24067 eap->arg = p;
24068 eap->cmdidx = CMD_edit;
24069 cmdmod.browse = FALSE;
24070 do_exedit(eap, NULL);
24071 vim_free(p);
24072 }
24073 }
24074 }
24075#endif
24076 }
24077}
24078
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079#endif /* FEAT_EVAL */
24080
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024082#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083
24084#ifdef WIN3264
24085/*
24086 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24087 */
24088static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24089static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24090static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24091
24092/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024093 * Get the short path (8.3) for the filename in "fnamep".
24094 * Only works for a valid file name.
24095 * When the path gets longer "fnamep" is changed and the allocated buffer
24096 * is put in "bufp".
24097 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24098 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 */
24100 static int
24101get_short_pathname(fnamep, bufp, fnamelen)
24102 char_u **fnamep;
24103 char_u **bufp;
24104 int *fnamelen;
24105{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024106 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 char_u *newbuf;
24108
24109 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 l = GetShortPathName(*fnamep, *fnamep, len);
24111 if (l > len - 1)
24112 {
24113 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024114 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 newbuf = vim_strnsave(*fnamep, l);
24116 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118
24119 vim_free(*bufp);
24120 *fnamep = *bufp = newbuf;
24121
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024122 /* Really should always succeed, as the buffer is big enough. */
24123 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
24125
24126 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024127 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128}
24129
24130/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024131 * Get the short path (8.3) for the filename in "fname". The converted
24132 * path is returned in "bufp".
24133 *
24134 * Some of the directories specified in "fname" may not exist. This function
24135 * will shorten the existing directories at the beginning of the path and then
24136 * append the remaining non-existing path.
24137 *
24138 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024139 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024140 * bufp - Pointer to an allocated buffer for the filename.
24141 * fnamelen - Length of the filename pointed to by fname
24142 *
24143 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 */
24145 static int
24146shortpath_for_invalid_fname(fname, bufp, fnamelen)
24147 char_u **fname;
24148 char_u **bufp;
24149 int *fnamelen;
24150{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024151 char_u *short_fname, *save_fname, *pbuf_unused;
24152 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024154 int old_len, len;
24155 int new_len, sfx_len;
24156 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157
24158 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024159 old_len = *fnamelen;
24160 save_fname = vim_strnsave(*fname, old_len);
24161 pbuf_unused = NULL;
24162 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024164 endp = save_fname + old_len - 1; /* Find the end of the copy */
24165 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024167 /*
24168 * Try shortening the supplied path till it succeeds by removing one
24169 * directory at a time from the tail of the path.
24170 */
24171 len = 0;
24172 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024174 /* go back one path-separator */
24175 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24176 --endp;
24177 if (endp <= save_fname)
24178 break; /* processed the complete path */
24179
24180 /*
24181 * Replace the path separator with a NUL and try to shorten the
24182 * resulting path.
24183 */
24184 ch = *endp;
24185 *endp = 0;
24186 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024187 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024188 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24189 {
24190 retval = FAIL;
24191 goto theend;
24192 }
24193 *endp = ch; /* preserve the string */
24194
24195 if (len > 0)
24196 break; /* successfully shortened the path */
24197
24198 /* failed to shorten the path. Skip the path separator */
24199 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 }
24201
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024202 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024204 /*
24205 * Succeeded in shortening the path. Now concatenate the shortened
24206 * path with the remaining path at the tail.
24207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024209 /* Compute the length of the new path. */
24210 sfx_len = (int)(save_endp - endp) + 1;
24211 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024213 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024215 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024217 /* There is not enough space in the currently allocated string,
24218 * copy it to a buffer big enough. */
24219 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024221 {
24222 retval = FAIL;
24223 goto theend;
24224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 }
24226 else
24227 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024228 /* Transfer short_fname to the main buffer (it's big enough),
24229 * unless get_short_pathname() did its work in-place. */
24230 *fname = *bufp = save_fname;
24231 if (short_fname != save_fname)
24232 vim_strncpy(save_fname, short_fname, len);
24233 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024235
24236 /* concat the not-shortened part of the path */
24237 vim_strncpy(*fname + len, endp, sfx_len);
24238 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024240
24241theend:
24242 vim_free(pbuf_unused);
24243 vim_free(save_fname);
24244
24245 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246}
24247
24248/*
24249 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024250 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 */
24252 static int
24253shortpath_for_partial(fnamep, bufp, fnamelen)
24254 char_u **fnamep;
24255 char_u **bufp;
24256 int *fnamelen;
24257{
24258 int sepcount, len, tflen;
24259 char_u *p;
24260 char_u *pbuf, *tfname;
24261 int hasTilde;
24262
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024263 /* Count up the path separators from the RHS.. so we know which part
24264 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024266 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 if (vim_ispathsep(*p))
24268 ++sepcount;
24269
24270 /* Need full path first (use expand_env() to remove a "~/") */
24271 hasTilde = (**fnamep == '~');
24272 if (hasTilde)
24273 pbuf = tfname = expand_env_save(*fnamep);
24274 else
24275 pbuf = tfname = FullName_save(*fnamep, FALSE);
24276
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024277 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024279 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281
24282 if (len == 0)
24283 {
24284 /* Don't have a valid filename, so shorten the rest of the
24285 * path if we can. This CAN give us invalid 8.3 filenames, but
24286 * there's not a lot of point in guessing what it might be.
24287 */
24288 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024289 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 }
24292
24293 /* Count the paths backward to find the beginning of the desired string. */
24294 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024295 {
24296#ifdef FEAT_MBYTE
24297 if (has_mbyte)
24298 p -= mb_head_off(tfname, p);
24299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 if (vim_ispathsep(*p))
24301 {
24302 if (sepcount == 0 || (hasTilde && sepcount == 1))
24303 break;
24304 else
24305 sepcount --;
24306 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 if (hasTilde)
24309 {
24310 --p;
24311 if (p >= tfname)
24312 *p = '~';
24313 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 }
24316 else
24317 ++p;
24318
24319 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24320 vim_free(*bufp);
24321 *fnamelen = (int)STRLEN(p);
24322 *bufp = pbuf;
24323 *fnamep = p;
24324
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024325 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326}
24327#endif /* WIN3264 */
24328
24329/*
24330 * Adjust a filename, according to a string of modifiers.
24331 * *fnamep must be NUL terminated when called. When returning, the length is
24332 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024333 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 * When there is an error, *fnamep is set to NULL.
24335 */
24336 int
24337modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24338 char_u *src; /* string with modifiers */
24339 int *usedlen; /* characters after src that are used */
24340 char_u **fnamep; /* file name so far */
24341 char_u **bufp; /* buffer for allocated file name or NULL */
24342 int *fnamelen; /* length of fnamep */
24343{
24344 int valid = 0;
24345 char_u *tail;
24346 char_u *s, *p, *pbuf;
24347 char_u dirname[MAXPATHL];
24348 int c;
24349 int has_fullname = 0;
24350#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024351 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 int has_shortname = 0;
24353#endif
24354
24355repeat:
24356 /* ":p" - full path/file_name */
24357 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24358 {
24359 has_fullname = 1;
24360
24361 valid |= VALID_PATH;
24362 *usedlen += 2;
24363
24364 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24365 if ((*fnamep)[0] == '~'
24366#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24367 && ((*fnamep)[1] == '/'
24368# ifdef BACKSLASH_IN_FILENAME
24369 || (*fnamep)[1] == '\\'
24370# endif
24371 || (*fnamep)[1] == NUL)
24372
24373#endif
24374 )
24375 {
24376 *fnamep = expand_env_save(*fnamep);
24377 vim_free(*bufp); /* free any allocated file name */
24378 *bufp = *fnamep;
24379 if (*fnamep == NULL)
24380 return -1;
24381 }
24382
24383 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024384 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 {
24386 if (vim_ispathsep(*p)
24387 && p[1] == '.'
24388 && (p[2] == NUL
24389 || vim_ispathsep(p[2])
24390 || (p[2] == '.'
24391 && (p[3] == NUL || vim_ispathsep(p[3])))))
24392 break;
24393 }
24394
24395 /* FullName_save() is slow, don't use it when not needed. */
24396 if (*p != NUL || !vim_isAbsName(*fnamep))
24397 {
24398 *fnamep = FullName_save(*fnamep, *p != NUL);
24399 vim_free(*bufp); /* free any allocated file name */
24400 *bufp = *fnamep;
24401 if (*fnamep == NULL)
24402 return -1;
24403 }
24404
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024405#ifdef WIN3264
24406# if _WIN32_WINNT >= 0x0500
24407 if (vim_strchr(*fnamep, '~') != NULL)
24408 {
24409 /* Expand 8.3 filename to full path. Needed to make sure the same
24410 * file does not have two different names.
24411 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24412 p = alloc(_MAX_PATH + 1);
24413 if (p != NULL)
24414 {
24415 if (GetLongPathName(*fnamep, p, MAXPATHL))
24416 {
24417 vim_free(*bufp);
24418 *bufp = *fnamep = p;
24419 }
24420 else
24421 vim_free(p);
24422 }
24423 }
24424# endif
24425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 /* Append a path separator to a directory. */
24427 if (mch_isdir(*fnamep))
24428 {
24429 /* Make room for one or two extra characters. */
24430 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24431 vim_free(*bufp); /* free any allocated file name */
24432 *bufp = *fnamep;
24433 if (*fnamep == NULL)
24434 return -1;
24435 add_pathsep(*fnamep);
24436 }
24437 }
24438
24439 /* ":." - path relative to the current directory */
24440 /* ":~" - path relative to the home directory */
24441 /* ":8" - shortname path - postponed till after */
24442 while (src[*usedlen] == ':'
24443 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24444 {
24445 *usedlen += 2;
24446 if (c == '8')
24447 {
24448#ifdef WIN3264
24449 has_shortname = 1; /* Postpone this. */
24450#endif
24451 continue;
24452 }
24453 pbuf = NULL;
24454 /* Need full path first (use expand_env() to remove a "~/") */
24455 if (!has_fullname)
24456 {
24457 if (c == '.' && **fnamep == '~')
24458 p = pbuf = expand_env_save(*fnamep);
24459 else
24460 p = pbuf = FullName_save(*fnamep, FALSE);
24461 }
24462 else
24463 p = *fnamep;
24464
24465 has_fullname = 0;
24466
24467 if (p != NULL)
24468 {
24469 if (c == '.')
24470 {
24471 mch_dirname(dirname, MAXPATHL);
24472 s = shorten_fname(p, dirname);
24473 if (s != NULL)
24474 {
24475 *fnamep = s;
24476 if (pbuf != NULL)
24477 {
24478 vim_free(*bufp); /* free any allocated file name */
24479 *bufp = pbuf;
24480 pbuf = NULL;
24481 }
24482 }
24483 }
24484 else
24485 {
24486 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24487 /* Only replace it when it starts with '~' */
24488 if (*dirname == '~')
24489 {
24490 s = vim_strsave(dirname);
24491 if (s != NULL)
24492 {
24493 *fnamep = s;
24494 vim_free(*bufp);
24495 *bufp = s;
24496 }
24497 }
24498 }
24499 vim_free(pbuf);
24500 }
24501 }
24502
24503 tail = gettail(*fnamep);
24504 *fnamelen = (int)STRLEN(*fnamep);
24505
24506 /* ":h" - head, remove "/file_name", can be repeated */
24507 /* Don't remove the first "/" or "c:\" */
24508 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24509 {
24510 valid |= VALID_HEAD;
24511 *usedlen += 2;
24512 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024513 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024514 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 *fnamelen = (int)(tail - *fnamep);
24516#ifdef VMS
24517 if (*fnamelen > 0)
24518 *fnamelen += 1; /* the path separator is part of the path */
24519#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024520 if (*fnamelen == 0)
24521 {
24522 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24523 p = vim_strsave((char_u *)".");
24524 if (p == NULL)
24525 return -1;
24526 vim_free(*bufp);
24527 *bufp = *fnamep = tail = p;
24528 *fnamelen = 1;
24529 }
24530 else
24531 {
24532 while (tail > s && !after_pathsep(s, tail))
24533 mb_ptr_back(*fnamep, tail);
24534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 }
24536
24537 /* ":8" - shortname */
24538 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24539 {
24540 *usedlen += 2;
24541#ifdef WIN3264
24542 has_shortname = 1;
24543#endif
24544 }
24545
24546#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024547 /*
24548 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 */
24550 if (has_shortname)
24551 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024552 /* Copy the string if it is shortened by :h and when it wasn't copied
24553 * yet, because we are going to change it in place. Avoids changing
24554 * the buffer name for "%:8". */
24555 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556 {
24557 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024558 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 return -1;
24560 vim_free(*bufp);
24561 *bufp = *fnamep = p;
24562 }
24563
24564 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024565 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 if (!has_fullname && !vim_isAbsName(*fnamep))
24567 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024568 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 return -1;
24570 }
24571 else
24572 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024573 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
Bram Moolenaardc935552011-08-17 15:23:23 +020024575 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024577 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 return -1;
24579
24580 if (l == 0)
24581 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024582 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024584 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 return -1;
24586 }
24587 *fnamelen = l;
24588 }
24589 }
24590#endif /* WIN3264 */
24591
24592 /* ":t" - tail, just the basename */
24593 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24594 {
24595 *usedlen += 2;
24596 *fnamelen -= (int)(tail - *fnamep);
24597 *fnamep = tail;
24598 }
24599
24600 /* ":e" - extension, can be repeated */
24601 /* ":r" - root, without extension, can be repeated */
24602 while (src[*usedlen] == ':'
24603 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24604 {
24605 /* find a '.' in the tail:
24606 * - for second :e: before the current fname
24607 * - otherwise: The last '.'
24608 */
24609 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24610 s = *fnamep - 2;
24611 else
24612 s = *fnamep + *fnamelen - 1;
24613 for ( ; s > tail; --s)
24614 if (s[0] == '.')
24615 break;
24616 if (src[*usedlen + 1] == 'e') /* :e */
24617 {
24618 if (s > tail)
24619 {
24620 *fnamelen += (int)(*fnamep - (s + 1));
24621 *fnamep = s + 1;
24622#ifdef VMS
24623 /* cut version from the extension */
24624 s = *fnamep + *fnamelen - 1;
24625 for ( ; s > *fnamep; --s)
24626 if (s[0] == ';')
24627 break;
24628 if (s > *fnamep)
24629 *fnamelen = s - *fnamep;
24630#endif
24631 }
24632 else if (*fnamep <= tail)
24633 *fnamelen = 0;
24634 }
24635 else /* :r */
24636 {
24637 if (s > tail) /* remove one extension */
24638 *fnamelen = (int)(s - *fnamep);
24639 }
24640 *usedlen += 2;
24641 }
24642
24643 /* ":s?pat?foo?" - substitute */
24644 /* ":gs?pat?foo?" - global substitute */
24645 if (src[*usedlen] == ':'
24646 && (src[*usedlen + 1] == 's'
24647 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24648 {
24649 char_u *str;
24650 char_u *pat;
24651 char_u *sub;
24652 int sep;
24653 char_u *flags;
24654 int didit = FALSE;
24655
24656 flags = (char_u *)"";
24657 s = src + *usedlen + 2;
24658 if (src[*usedlen + 1] == 'g')
24659 {
24660 flags = (char_u *)"g";
24661 ++s;
24662 }
24663
24664 sep = *s++;
24665 if (sep)
24666 {
24667 /* find end of pattern */
24668 p = vim_strchr(s, sep);
24669 if (p != NULL)
24670 {
24671 pat = vim_strnsave(s, (int)(p - s));
24672 if (pat != NULL)
24673 {
24674 s = p + 1;
24675 /* find end of substitution */
24676 p = vim_strchr(s, sep);
24677 if (p != NULL)
24678 {
24679 sub = vim_strnsave(s, (int)(p - s));
24680 str = vim_strnsave(*fnamep, *fnamelen);
24681 if (sub != NULL && str != NULL)
24682 {
24683 *usedlen = (int)(p + 1 - src);
24684 s = do_string_sub(str, pat, sub, flags);
24685 if (s != NULL)
24686 {
24687 *fnamep = s;
24688 *fnamelen = (int)STRLEN(s);
24689 vim_free(*bufp);
24690 *bufp = s;
24691 didit = TRUE;
24692 }
24693 }
24694 vim_free(sub);
24695 vim_free(str);
24696 }
24697 vim_free(pat);
24698 }
24699 }
24700 /* after using ":s", repeat all the modifiers */
24701 if (didit)
24702 goto repeat;
24703 }
24704 }
24705
Bram Moolenaar26df0922014-02-23 23:39:13 +010024706 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24707 {
24708 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24709 if (p == NULL)
24710 return -1;
24711 vim_free(*bufp);
24712 *bufp = *fnamep = p;
24713 *fnamelen = (int)STRLEN(p);
24714 *usedlen += 2;
24715 }
24716
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717 return valid;
24718}
24719
24720/*
24721 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24722 * "flags" can be "g" to do a global substitute.
24723 * Returns an allocated string, NULL for error.
24724 */
24725 char_u *
24726do_string_sub(str, pat, sub, flags)
24727 char_u *str;
24728 char_u *pat;
24729 char_u *sub;
24730 char_u *flags;
24731{
24732 int sublen;
24733 regmatch_T regmatch;
24734 int i;
24735 int do_all;
24736 char_u *tail;
24737 garray_T ga;
24738 char_u *ret;
24739 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024740 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741
24742 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24743 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024744 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745
24746 ga_init2(&ga, 1, 200);
24747
24748 do_all = (flags[0] == 'g');
24749
24750 regmatch.rm_ic = p_ic;
24751 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24752 if (regmatch.regprog != NULL)
24753 {
24754 tail = str;
24755 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24756 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024757 /* Skip empty match except for first match. */
24758 if (regmatch.startp[0] == regmatch.endp[0])
24759 {
24760 if (zero_width == regmatch.startp[0])
24761 {
24762 /* avoid getting stuck on a match with an empty string */
24763 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24764 ++ga.ga_len;
24765 continue;
24766 }
24767 zero_width = regmatch.startp[0];
24768 }
24769
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770 /*
24771 * Get some space for a temporary buffer to do the substitution
24772 * into. It will contain:
24773 * - The text up to where the match is.
24774 * - The substituted text.
24775 * - The text after the match.
24776 */
24777 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24778 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24779 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24780 {
24781 ga_clear(&ga);
24782 break;
24783 }
24784
24785 /* copy the text up to where the match is */
24786 i = (int)(regmatch.startp[0] - tail);
24787 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24788 /* add the substituted text */
24789 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24790 + ga.ga_len + i, TRUE, TRUE, FALSE);
24791 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024792 tail = regmatch.endp[0];
24793 if (*tail == NUL)
24794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 if (!do_all)
24796 break;
24797 }
24798
24799 if (ga.ga_data != NULL)
24800 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24801
Bram Moolenaar473de612013-06-08 18:19:48 +020024802 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 }
24804
24805 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24806 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024807 if (p_cpo == empty_option)
24808 p_cpo = save_cpo;
24809 else
24810 /* Darn, evaluating {sub} expression changed the value. */
24811 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812
24813 return ret;
24814}
24815
24816#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */