blob: 85c40315458c260ae996f22b1f036bee4262990b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000364};
365
366/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_type vv_di.di_tv.v_type
368#define vv_nr vv_di.di_tv.vval.v_number
369#define vv_float vv_di.di_tv.vval.v_float
370#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000371#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
378static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
380static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
381static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
383static void list_glob_vars __ARGS((int *first));
384static void list_buf_vars __ARGS((int *first));
385static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_vim_vars __ARGS((int *first));
390static void list_script_vars __ARGS((int *first));
391static void list_func_vars __ARGS((int *first));
392static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
394static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100395static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void clear_lval __ARGS((lval_T *lp));
397static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
398static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
400static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
401static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
402static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
403static void item_lock __ARGS((typval_T *tv, int deep, int lock));
404static int tv_islocked __ARGS((typval_T *tv));
405
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
407static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000412static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
413static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000415static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000420static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100422static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
423static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
424static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000431static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100432static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000434static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200435static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#ifdef FEAT_FLOAT
446static int string2float __ARGS((char_u *text, float_T *value));
447#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100450static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200452static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000453static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000454static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200458static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100461static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
485static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
486#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000487static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000490static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000493static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200501static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
506static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517#ifdef FEAT_FLOAT
518static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200564static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100592static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000594static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200607static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#ifdef FEAT_LUA
611static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000618static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000621static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625#ifdef vim_mkdir
626static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100629#ifdef FEAT_MZSCHEME
630static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100634static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000636#ifdef FEAT_FLOAT
637static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000640static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200642#ifdef FEAT_PYTHON3
643static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645#ifdef FEAT_PYTHON
646static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200665static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100667static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100687#ifdef FEAT_CRYPT
688static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
689#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000690static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200691static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100750static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200852 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
853 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200854 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857
858 for (i = 0; i < VV_LEN; ++i)
859 {
860 p = &vimvars[i];
861 STRCPY(p->vv_di.di_key, p->vv_name);
862 if (p->vv_flags & VV_RO)
863 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 else if (p->vv_flags & VV_RO_SBX)
865 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
866 else
867 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000868
869 /* add to v: scope dict, unless the value is not always available */
870 if (p->vv_type != VAR_UNKNOWN)
871 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 /* add to compat scope dict */
874 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000876 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100877 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917
918 /* global variables */
919 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000921 /* autoloaded script names */
922 ga_clear_strings(&ga_loaded);
923
Bram Moolenaarcca74132013-09-25 21:00:28 +0200924 /* Script-local variables. First clear all the variables and in a second
925 * loop free the scriptvar_T, because a variable in one script might hold
926 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001638/*
1639 * Call vimL function "func" and return the result as a number.
1640 * Returns -1 when calling the function fails.
1641 * Uses argv[argc] for the function arguments.
1642 */
1643 long
1644call_func_retnr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
1651 long retval;
1652
1653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
1661
1662#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1663 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1664
Bram Moolenaar4f688582007-07-24 12:34:30 +00001665# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 * Call vimL function "func" and return the result as a string.
1668 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Uses argv[argc] for the function arguments.
1670 */
1671 void *
1672call_func_retstr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 return NULL;
1684
1685 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return retval;
1688}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001689# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 */
1696 void *
1697call_func_retlist(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 return NULL;
1708
1709 if (rettv.v_type != VAR_LIST)
1710 {
1711 clear_tv(&rettv);
1712 return NULL;
1713 }
1714
1715 return rettv.vval.v_list;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718
1719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar429fa852013-04-15 12:27:36 +02002128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002244 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002524 * flags:
2525 * GLV_QUIET: do not give error messages
2526 * GLV_NO_AUTOLOAD: do not use script autoloading
2527 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002529 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 */
2532 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 typval_T *rettv;
2536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int unlet;
2538 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *expr_start, *expr_end;
2544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 dictitem_T *v;
2546 typval_T var1;
2547 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002754 else
2755 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2757 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 || !valid_varname(key);
2760 if (len != -1)
2761 key[len] = prevval;
2762 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 return NULL;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* Can't add "v:" variable. */
2769 if (lp->ll_dict == &vimvardict)
2770 {
2771 EMSG2(_(e_illvar), name);
2772 return NULL;
2773 }
2774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (len == -1)
2789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 p = NULL;
2792 break;
2793 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 /* existing variable, need to check if it can be changed */
2795 else if (var_check_ro(lp->ll_di->di_flags, name))
2796 return NULL;
2797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 else
2803 {
2804 /*
2805 * Get the number and item for the only or first index of the List.
2806 */
2807 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
2810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_list = lp->ll_tv->vval.v_list;
2816 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2817 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (lp->ll_n1 < 0)
2820 {
2821 lp->ll_n1 = 0;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 }
2824 }
2825 if (lp->ll_li == NULL)
2826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
2834 /*
2835 * May need to find the item or absolute index for the second
2836 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 * When no index given: "lp->ll_empty2" is TRUE.
2838 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2857 if (lp->ll_n1 < 0)
2858 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2859 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 {
2861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return p;
2872}
2873
2874/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
2878clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 vim_free(lp->ll_exp_name);
2882 vim_free(lp->ll_newkey);
2883}
2884
2885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 */
2890 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *ri;
2900 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901
2902 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *endp;
2907 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002914 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 {
2916 if (tv_op(&tv, rettv, op) == OK)
2917 set_var(lp->ll_name, &tv, FALSE);
2918 clear_tv(&tv);
2919 }
2920 }
2921 else
2922 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else if (tv_check_lock(lp->ll_newkey == NULL
2927 ? lp->ll_tv->v_lock
2928 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2929 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 else if (lp->ll_range)
2931 {
2932 /*
2933 * Assign the List values to the list items.
2934 */
2935 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2939 else
2940 {
2941 clear_tv(&lp->ll_li->li_tv);
2942 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = ri->li_next;
2945 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2946 break;
2947 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002950 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 ri = NULL;
2953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_li = lp->ll_li->li_next;
2957 ++lp->ll_n1;
2958 }
2959 if (ri != NULL)
2960 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else if (lp->ll_empty2
2962 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 : lp->ll_n1 != lp->ll_n2)
2964 EMSG(_("E711: List value has not enough items"));
2965 }
2966 else
2967 {
2968 /*
2969 * Assign to a List or Dictionary item.
2970 */
2971 if (lp->ll_newkey != NULL)
2972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
2975 EMSG2(_(e_letwrong), op);
2976 return;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002980 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (di == NULL)
2982 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002983 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2984 {
2985 vim_free(di);
2986 return;
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 else if (op != NULL && *op != '=')
2991 {
2992 tv_op(lp->ll_tv, rettv, op);
2993 return;
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the value to the variable or list item.
3000 */
3001 if (copy)
3002 copy_tv(rettv, lp->ll_tv);
3003 else
3004 {
3005 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010}
3011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3014 * Returns OK or FAIL.
3015 */
3016 static int
3017tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T *tv1;
3019 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 char_u *op;
3021{
3022 long n;
3023 char_u numbuf[NUMBUFLEN];
3024 char_u *s;
3025
3026 /* Can't do anything with a Funcref or a Dict on the right. */
3027 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3028 {
3029 switch (tv1->v_type)
3030 {
3031 case VAR_DICT:
3032 case VAR_FUNC:
3033 break;
3034
3035 case VAR_LIST:
3036 if (*op != '+' || tv2->v_type != VAR_LIST)
3037 break;
3038 /* List += List */
3039 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3040 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3041 return OK;
3042
3043 case VAR_NUMBER:
3044 case VAR_STRING:
3045 if (tv2->v_type == VAR_LIST)
3046 break;
3047 if (*op == '+' || *op == '-')
3048 {
3049 /* nr += nr or nr -= nr*/
3050 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#ifdef FEAT_FLOAT
3052 if (tv2->v_type == VAR_FLOAT)
3053 {
3054 float_T f = n;
3055
3056 if (*op == '+')
3057 f += tv2->vval.v_float;
3058 else
3059 f -= tv2->vval.v_float;
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_FLOAT;
3062 tv1->vval.v_float = f;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
3066 {
3067 if (*op == '+')
3068 n += get_tv_number(tv2);
3069 else
3070 n -= get_tv_number(tv2);
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_NUMBER;
3073 tv1->vval.v_number = n;
3074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 }
3076 else
3077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078 if (tv2->v_type == VAR_FLOAT)
3079 break;
3080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 /* str .= str */
3082 s = get_tv_string(tv1);
3083 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3084 clear_tv(tv1);
3085 tv1->v_type = VAR_STRING;
3086 tv1->vval.v_string = s;
3087 }
3088 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089
3090#ifdef FEAT_FLOAT
3091 case VAR_FLOAT:
3092 {
3093 float_T f;
3094
3095 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3096 && tv2->v_type != VAR_NUMBER
3097 && tv2->v_type != VAR_STRING))
3098 break;
3099 if (tv2->v_type == VAR_FLOAT)
3100 f = tv2->vval.v_float;
3101 else
3102 f = get_tv_number(tv2);
3103 if (*op == '+')
3104 tv1->vval.v_float += f;
3105 else
3106 tv1->vval.v_float -= f;
3107 }
3108 return OK;
3109#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 }
3112
3113 EMSG2(_(e_letwrong), op);
3114 return FAIL;
3115}
3116
3117/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * Add a watcher to a list.
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
3125 lw->lw_next = l->lv_watch;
3126 l->lv_watch = lw;
3127}
3128
3129/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003130 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 * No warning when it isn't found...
3132 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003133 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 lwp = &l->lv_watch;
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 {
3143 if (lw == lwrem)
3144 {
3145 *lwp = lw->lw_next;
3146 break;
3147 }
3148 lwp = &lw->lw_next;
3149 }
3150}
3151
3152/*
3153 * Just before removing an item from a list: advance watchers to the next
3154 * item.
3155 */
3156 static void
3157list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 list_T *l;
3159 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3164 if (lw->lw_item == item)
3165 lw->lw_item = item->li_next;
3166}
3167
3168/*
3169 * Evaluate the expression used in a ":for var in expr" command.
3170 * "arg" points to "var".
3171 * Set "*errp" to TRUE for an error, FALSE otherwise;
3172 * Return a pointer that holds the info. Null when there is an error.
3173 */
3174 void *
3175eval_for_line(arg, errp, nextcmdp, skip)
3176 char_u *arg;
3177 int *errp;
3178 char_u **nextcmdp;
3179 int skip;
3180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 typval_T tv;
3184 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 *errp = TRUE; /* default: there is an error */
3187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 if (fi == NULL)
3190 return NULL;
3191
3192 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3193 if (expr == NULL)
3194 return fi;
3195
3196 expr = skipwhite(expr);
3197 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3198 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003199 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 return fi;
3201 }
3202
3203 if (skip)
3204 ++emsg_skip;
3205 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3206 {
3207 *errp = FALSE;
3208 if (!skip)
3209 {
3210 l = tv.vval.v_list;
3211 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 clear_tv(&tv);
3215 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 else
3217 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003218 /* No need to increment the refcount, it's already set for the
3219 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 fi->fi_list = l;
3221 list_add_watch(l, &fi->fi_lw);
3222 fi->fi_lw.lw_item = l->lv_first;
3223 }
3224 }
3225 }
3226 if (skip)
3227 --emsg_skip;
3228
3229 return fi;
3230}
3231
3232/*
3233 * Use the first item in a ":for" list. Advance to the next.
3234 * Assign the values to the variable (list). "arg" points to the first one.
3235 * Return TRUE when a valid item was found, FALSE when at end of list or
3236 * something wrong.
3237 */
3238 int
3239next_for_item(fi_void, arg)
3240 void *fi_void;
3241 char_u *arg;
3242{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 item = fi->fi_lw.lw_item;
3248 if (item == NULL)
3249 result = FALSE;
3250 else
3251 {
3252 fi->fi_lw.lw_item = item->li_next;
3253 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3254 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3255 }
3256 return result;
3257}
3258
3259/*
3260 * Free the structure used to store info used by ":for".
3261 */
3262 void
3263free_for_info(fi_void)
3264 void *fi_void;
3265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003268 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 list_unref(fi->fi_list);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 vim_free(fi);
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3277
3278 void
3279set_context_for_expression(xp, arg, cmdidx)
3280 expand_T *xp;
3281 char_u *arg;
3282 cmdidx_T cmdidx;
3283{
3284 int got_eq = FALSE;
3285 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 if (cmdidx == CMD_let)
3289 {
3290 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003291 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 {
3293 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 {
3296 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (vim_iswhite(*p))
3299 break;
3300 }
3301 return;
3302 }
3303 }
3304 else
3305 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3306 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while ((xp->xp_pattern = vim_strpbrk(arg,
3308 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3309 {
3310 c = *xp->xp_pattern;
3311 if (c == '&')
3312 {
3313 c = xp->xp_pattern[1];
3314 if (c == '&')
3315 {
3316 ++xp->xp_pattern;
3317 xp->xp_context = cmdidx != CMD_let || got_eq
3318 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3319 }
3320 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003323 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3324 xp->xp_pattern += 2;
3325
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 else if (c == '$')
3329 {
3330 /* environment variable */
3331 xp->xp_context = EXPAND_ENV_VARS;
3332 }
3333 else if (c == '=')
3334 {
3335 got_eq = TRUE;
3336 xp->xp_context = EXPAND_EXPRESSION;
3337 }
3338 else if (c == '<'
3339 && xp->xp_context == EXPAND_FUNCTIONS
3340 && vim_strchr(xp->xp_pattern, '(') == NULL)
3341 {
3342 /* Function name can start with "<SNR>" */
3343 break;
3344 }
3345 else if (cmdidx != CMD_let || got_eq)
3346 {
3347 if (c == '"') /* string */
3348 {
3349 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3350 if (c == '\\' && xp->xp_pattern[1] != NUL)
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '\'') /* literal string */
3355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3358 /* skip */ ;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '|')
3362 {
3363 if (xp->xp_pattern[1] == '|')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
3368 else
3369 xp->xp_context = EXPAND_COMMANDS;
3370 }
3371 else
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 /* Doesn't look like something valid, expand as an expression
3376 * anyway. */
3377 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 arg = xp->xp_pattern;
3379 if (*arg != NUL)
3380 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3381 /* skip */ ;
3382 }
3383 xp->xp_pattern = arg;
3384}
3385
3386#endif /* FEAT_CMDL_COMPL */
3387
3388/*
3389 * ":1,25call func(arg1, arg2)" function call.
3390 */
3391 void
3392ex_call(eap)
3393 exarg_T *eap;
3394{
3395 char_u *arg = eap->arg;
3396 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 linenr_T lnum;
3402 int doesrange;
3403 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 if (eap->skip)
3407 {
3408 /* trans_function_name() doesn't work well when skipping, use eval0()
3409 * instead to skip to any following command, e.g. for:
3410 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003411 ++emsg_skip;
3412 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3413 clear_tv(&rettv);
3414 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 return;
3416 }
3417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003418 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003419 if (fudi.fd_newkey != NULL)
3420 {
3421 /* Still need to give an error message for missing key. */
3422 EMSG2(_(e_dictkey), fudi.fd_newkey);
3423 vim_free(fudi.fd_newkey);
3424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 if (tofree == NULL)
3426 return;
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 /* Increase refcount on dictionary, it could get deleted when evaluating
3429 * the arguments. */
3430 if (fudi.fd_dict != NULL)
3431 ++fudi.fd_dict->dv_refcount;
3432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003435 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar532c7802005-01-27 14:44:31 +00003437 /* Skip white space to allow ":call func ()". Not good, but required for
3438 * backward compatibility. */
3439 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 if (*startarg != '(')
3443 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003444 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 goto end;
3446 }
3447
3448 /*
3449 * When skipping, evaluate the function once, to find the end of the
3450 * arguments.
3451 * When the function takes a range, this is discovered after the first
3452 * call, and the loop is broken.
3453 */
3454 if (eap->skip)
3455 {
3456 ++emsg_skip;
3457 lnum = eap->line2; /* do it once, also with an invalid range */
3458 }
3459 else
3460 lnum = eap->line1;
3461 for ( ; lnum <= eap->line2; ++lnum)
3462 {
3463 if (!eap->skip && eap->addr_count > 0)
3464 {
3465 curwin->w_cursor.lnum = lnum;
3466 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003467#ifdef FEAT_VIRTUALEDIT
3468 curwin->w_cursor.coladd = 0;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 eap->line1, eap->line2, &doesrange,
3474 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 failed = TRUE;
3477 break;
3478 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
3480 /* Handle a function returning a Funcref, Dictionary or List. */
3481 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3482 {
3483 failed = TRUE;
3484 break;
3485 }
3486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (doesrange || eap->skip)
3489 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (aborting())
3496 break;
3497 }
3498 if (eap->skip)
3499 --emsg_skip;
3500
3501 if (!failed)
3502 {
3503 /* Check for trailing illegal characters and a following command. */
3504 if (!ends_excmd(*arg))
3505 {
3506 emsg_severe = TRUE;
3507 EMSG(_(e_trailing));
3508 }
3509 else
3510 eap->nextcmd = check_nextcmd(arg);
3511 }
3512
3513end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * ":unlet[!] var1 ... " command.
3520 */
3521 void
3522ex_unlet(eap)
3523 exarg_T *eap;
3524{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 ex_unletlock(eap, eap->arg, 0);
3526}
3527
3528/*
3529 * ":lockvar" and ":unlockvar" commands
3530 */
3531 void
3532ex_lockvar(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int deep = 2;
3537
3538 if (eap->forceit)
3539 deep = -1;
3540 else if (vim_isdigit(*arg))
3541 {
3542 deep = getdigits(&arg);
3543 arg = skipwhite(arg);
3544 }
3545
3546 ex_unletlock(eap, arg, deep);
3547}
3548
3549/*
3550 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3551 */
3552 static void
3553ex_unletlock(eap, argstart, deep)
3554 exarg_T *eap;
3555 char_u *argstart;
3556 int deep;
3557{
3558 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 do
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003566 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003567 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (lv.ll_name == NULL)
3569 error = TRUE; /* error but continue parsing */
3570 if (name_end == NULL || (!vim_iswhite(*name_end)
3571 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (name_end != NULL)
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 if (!(eap->skip || error))
3579 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 {
3585 if (eap->cmdidx == CMD_unlet)
3586 {
3587 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3588 error = TRUE;
3589 }
3590 else
3591 {
3592 if (do_lock_var(&lv, name_end, deep,
3593 eap->cmdidx == CMD_lockvar) == FAIL)
3594 error = TRUE;
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 if (!eap->skip)
3599 clear_lval(&lv);
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 arg = skipwhite(name_end);
3602 } while (!ends_excmd(*arg));
3603
3604 eap->nextcmd = check_nextcmd(arg);
3605}
3606
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 int forceit;
3612{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 int ret = OK;
3614 int cc;
3615
3616 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3629 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 else if (lp->ll_range)
3631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633
3634 /* Delete a range of List items. */
3635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3636 {
3637 li = lp->ll_li->li_next;
3638 listitem_remove(lp->ll_list, lp->ll_li);
3639 lp->ll_li = li;
3640 ++lp->ll_n1;
3641 }
3642 }
3643 else
3644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 /* unlet a List item. */
3647 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003650 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 }
3652
3653 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hashtab_T *ht;
3666 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 ht = find_var_ht(name, &varname);
3671 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = hash_find(ht, varname);
3674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003676 di = HI2DI(hi);
3677 if (var_check_fixed(di->di_flags, name)
3678 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
3680 delete_var(ht, hi);
3681 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 if (forceit)
3685 return OK;
3686 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688}
3689
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690/*
3691 * Lock or unlock variable indicated by "lp".
3692 * "deep" is the levels to go (-1 for unlimited);
3693 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3694 */
3695 static int
3696do_lock_var(lp, name_end, deep, lock)
3697 lval_T *lp;
3698 char_u *name_end;
3699 int deep;
3700 int lock;
3701{
3702 int ret = OK;
3703 int cc;
3704 dictitem_T *di;
3705
3706 if (deep == 0) /* nothing to do */
3707 return OK;
3708
3709 if (lp->ll_tv == NULL)
3710 {
3711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
3717 else
3718 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003719 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 if (di == NULL)
3721 ret = FAIL;
3722 else
3723 {
3724 if (lock)
3725 di->di_flags |= DI_FLAGS_LOCK;
3726 else
3727 di->di_flags &= ~DI_FLAGS_LOCK;
3728 item_lock(&di->di_tv, deep, lock);
3729 }
3730 }
3731 *name_end = cc;
3732 }
3733 else if (lp->ll_range)
3734 {
3735 listitem_T *li = lp->ll_li;
3736
3737 /* (un)lock a range of List items. */
3738 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3739 {
3740 item_lock(&li->li_tv, deep, lock);
3741 li = li->li_next;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else if (lp->ll_list != NULL)
3746 /* (un)lock a List item. */
3747 item_lock(&lp->ll_li->li_tv, deep, lock);
3748 else
3749 /* un(lock) a Dictionary item. */
3750 item_lock(&lp->ll_di->di_tv, deep, lock);
3751
3752 return ret;
3753}
3754
3755/*
3756 * Lock or unlock an item. "deep" is nr of levels to go.
3757 */
3758 static void
3759item_lock(tv, deep, lock)
3760 typval_T *tv;
3761 int deep;
3762 int lock;
3763{
3764 static int recurse = 0;
3765 list_T *l;
3766 listitem_T *li;
3767 dict_T *d;
3768 hashitem_T *hi;
3769 int todo;
3770
3771 if (recurse >= DICT_MAXNEST)
3772 {
3773 EMSG(_("E743: variable nested too deep for (un)lock"));
3774 return;
3775 }
3776 if (deep == 0)
3777 return;
3778 ++recurse;
3779
3780 /* lock/unlock the item itself */
3781 if (lock)
3782 tv->v_lock |= VAR_LOCKED;
3783 else
3784 tv->v_lock &= ~VAR_LOCKED;
3785
3786 switch (tv->v_type)
3787 {
3788 case VAR_LIST:
3789 if ((l = tv->vval.v_list) != NULL)
3790 {
3791 if (lock)
3792 l->lv_lock |= VAR_LOCKED;
3793 else
3794 l->lv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 /* recursive: lock/unlock the items the List contains */
3797 for (li = l->lv_first; li != NULL; li = li->li_next)
3798 item_lock(&li->li_tv, deep - 1, lock);
3799 }
3800 break;
3801 case VAR_DICT:
3802 if ((d = tv->vval.v_dict) != NULL)
3803 {
3804 if (lock)
3805 d->dv_lock |= VAR_LOCKED;
3806 else
3807 d->dv_lock &= ~VAR_LOCKED;
3808 if (deep < 0 || deep > 1)
3809 {
3810 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003811 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3813 {
3814 if (!HASHITEM_EMPTY(hi))
3815 {
3816 --todo;
3817 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3818 }
3819 }
3820 }
3821 }
3822 }
3823 --recurse;
3824}
3825
Bram Moolenaara40058a2005-07-11 22:42:07 +00003826/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3828 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003829 */
3830 static int
3831tv_islocked(tv)
3832 typval_T *tv;
3833{
3834 return (tv->v_lock & VAR_LOCKED)
3835 || (tv->v_type == VAR_LIST
3836 && tv->vval.v_list != NULL
3837 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3838 || (tv->v_type == VAR_DICT
3839 && tv->vval.v_dict != NULL
3840 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3844/*
3845 * Delete all "menutrans_" variables.
3846 */
3847 void
3848del_menutrans_vars()
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3861 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 }
3863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866#endif
3867
3868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3869
3870/*
3871 * Local string buffer for the next two functions to store a variable name
3872 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3873 * get_user_var_name().
3874 */
3875
3876static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3877
3878static char_u *varnamebuf = NULL;
3879static int varnamebuflen = 0;
3880
3881/*
3882 * Function to concatenate a prefix and a variable name.
3883 */
3884 static char_u *
3885cat_prefix_varname(prefix, name)
3886 int prefix;
3887 char_u *name;
3888{
3889 int len;
3890
3891 len = (int)STRLEN(name) + 3;
3892 if (len > varnamebuflen)
3893 {
3894 vim_free(varnamebuf);
3895 len += 10; /* some additional space */
3896 varnamebuf = alloc(len);
3897 if (varnamebuf == NULL)
3898 {
3899 varnamebuflen = 0;
3900 return NULL;
3901 }
3902 varnamebuflen = len;
3903 }
3904 *varnamebuf = prefix;
3905 varnamebuf[1] = ':';
3906 STRCPY(varnamebuf + 2, name);
3907 return varnamebuf;
3908}
3909
3910/*
3911 * Function given to ExpandGeneric() to obtain the list of user defined
3912 * (global/buffer/window/built-in) variable names.
3913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *
3915get_user_var_name(xp, idx)
3916 expand_T *xp;
3917 int idx;
3918{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 static long_u gdone;
3920 static long_u bdone;
3921 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922#ifdef FEAT_WINDOWS
3923 static long_u tdone;
3924#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static int vidx;
3926 static hashitem_T *hi;
3927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 tdone = 0;
3934#endif
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* Global variables */
3938 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3947 return cat_prefix_varname('g', hi->hi_key);
3948 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003952 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return (char_u *)"b:changedtick";
3967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
3969 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003973 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 else
3976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 if (tdone < ht->ht_used)
3986 {
3987 if (tdone++ == 0)
3988 hi = ht->ht_array;
3989 else
3990 ++hi;
3991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('t', hi->hi_key);
3994 }
3995#endif
3996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 /* v: variables */
3998 if (vidx < VV_LEN)
3999 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 vim_free(varnamebuf);
4002 varnamebuf = NULL;
4003 varnamebuflen = 0;
4004 return NULL;
4005}
4006
4007#endif /* FEAT_CMDL_COMPL */
4008
4009/*
4010 * types for expressions.
4011 */
4012typedef enum
4013{
4014 TYPE_UNKNOWN = 0
4015 , TYPE_EQUAL /* == */
4016 , TYPE_NEQUAL /* != */
4017 , TYPE_GREATER /* > */
4018 , TYPE_GEQUAL /* >= */
4019 , TYPE_SMALLER /* < */
4020 , TYPE_SEQUAL /* <= */
4021 , TYPE_MATCH /* =~ */
4022 , TYPE_NOMATCH /* !~ */
4023} exptype_T;
4024
4025/*
4026 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4029 */
4030
4031/*
4032 * Handle zero level expression.
4033 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004035 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return OK or FAIL.
4037 */
4038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 char_u **nextcmd;
4043 int evaluate;
4044{
4045 int ret;
4046 char_u *p;
4047
4048 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (ret == FAIL || !ends_excmd(*p))
4051 {
4052 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * Report the invalid expression unless the expression evaluation has
4056 * been cancelled due to an aborting error, an interrupt, or an
4057 * exception.
4058 */
4059 if (!aborting())
4060 EMSG2(_(e_invexpr2), arg);
4061 ret = FAIL;
4062 }
4063 if (nextcmd != NULL)
4064 *nextcmd = check_nextcmd(p);
4065
4066 return ret;
4067}
4068
4069/*
4070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * "arg" must point to the first non-white of the expression.
4074 * "arg" is advanced to the next non-white after the recognized expression.
4075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004076 * Note: "rettv.v_lock" is not set.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
4086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 /*
4090 * Get the first variable.
4091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094
4095 if ((*arg)[0] == '?')
4096 {
4097 result = FALSE;
4098 if (evaluate)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 int error = FALSE;
4101
4102 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (error)
4106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115
4116 /*
4117 * Check for the ":".
4118 */
4119 if ((*arg)[0] != ':')
4120 {
4121 EMSG(_("E109: Missing ':' after '?'"));
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126
4127 /*
4128 * Get the third variable.
4129 */
4130 *arg = skipwhite(*arg + 1);
4131 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4132 {
4133 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * Handle first level expression:
4146 * expr2 || expr2 || expr2 logical OR
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 long result;
4161 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 /*
4171 * Repeat until there is no following "||".
4172 */
4173 first = TRUE;
4174 result = FALSE;
4175 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4176 {
4177 if (evaluate && first)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 first = FALSE;
4185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 2);
4191 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4192 return FAIL;
4193
4194 /*
4195 * Compute the result.
4196 */
4197 if (evaluate && !result)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (error)
4203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 if (evaluate)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_NUMBER;
4208 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 return OK;
4213}
4214
4215/*
4216 * Handle second level expression:
4217 * expr3 && expr3 && expr3 logical AND
4218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 long result;
4232 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * Get the first variable.
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240
4241 /*
4242 * Repeat until there is no following "&&".
4243 */
4244 first = TRUE;
4245 result = TRUE;
4246 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4247 {
4248 if (evaluate && first)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (error)
4254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 first = FALSE;
4256 }
4257
4258 /*
4259 * Get the second variable.
4260 */
4261 *arg = skipwhite(*arg + 2);
4262 if (eval4(arg, &var2, evaluate && result) == FAIL)
4263 return FAIL;
4264
4265 /*
4266 * Compute the result.
4267 */
4268 if (evaluate && result)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (evaluate)
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle third level expression:
4288 * var1 == var2
4289 * var1 =~ var2
4290 * var1 != var2
4291 * var1 !~ var2
4292 * var1 > var2
4293 * var1 >= var2
4294 * var1 < var2
4295 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 * var1 is var2
4297 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int evaluate;
4309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u *p;
4312 int i;
4313 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int len = 2;
4316 long n1, n2;
4317 char_u *s1, *s2;
4318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4319 regmatch_T regmatch;
4320 int ic;
4321 char_u *save_cpo;
4322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 p = *arg;
4330 switch (p[0])
4331 {
4332 case '=': if (p[1] == '=')
4333 type = TYPE_EQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_MATCH;
4336 break;
4337 case '!': if (p[1] == '=')
4338 type = TYPE_NEQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_NOMATCH;
4341 break;
4342 case '>': if (p[1] != '=')
4343 {
4344 type = TYPE_GREATER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_GEQUAL;
4349 break;
4350 case '<': if (p[1] != '=')
4351 {
4352 type = TYPE_SMALLER;
4353 len = 1;
4354 }
4355 else
4356 type = TYPE_SEQUAL;
4357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 case 'i': if (p[1] == 's')
4359 {
4360 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4361 len = 5;
4362 if (!vim_isIDc(p[len]))
4363 {
4364 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4365 type_is = TRUE;
4366 }
4367 }
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 if (type != TYPE_UNKNOWN)
4375 {
4376 /* extra question mark appended: ignore case */
4377 if (p[len] == '?')
4378 {
4379 ic = TRUE;
4380 ++len;
4381 }
4382 /* extra '#' appended: match case */
4383 else if (p[len] == '#')
4384 {
4385 ic = FALSE;
4386 ++len;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 else
4390 ic = p_ic;
4391
4392 /*
4393 * Get the second variable.
4394 */
4395 *arg = skipwhite(p + len);
4396 if (eval5(arg, &var2, evaluate) == FAIL)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400 }
4401
4402 if (evaluate)
4403 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type_is && rettv->v_type != var2.v_type)
4405 {
4406 /* For "is" a different type always means FALSE, for "notis"
4407 * it means TRUE. */
4408 n1 = (type == TYPE_NEQUAL);
4409 }
4410 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_list == var2.vval.v_list);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004425 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4434 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004440 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4441 {
4442 if (type_is)
4443 {
4444 n1 = (rettv->v_type == var2.v_type
4445 && rettv->vval.v_dict == var2.vval.v_dict);
4446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 else if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
4453 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4454 else
4455 EMSG(_("E736: Invalid operation for Dictionary"));
4456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004463 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4464 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4471 {
4472 if (rettv->v_type != var2.v_type
4473 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4474 {
4475 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 return FAIL;
4482 }
4483 else
4484 {
4485 /* Compare two Funcrefs for being equal or unequal. */
4486 if (rettv->vval.v_string == NULL
4487 || var2.vval.v_string == NULL)
4488 n1 = FALSE;
4489 else
4490 n1 = STRCMP(rettv->vval.v_string,
4491 var2.vval.v_string) == 0;
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004497#ifdef FEAT_FLOAT
4498 /*
4499 * If one of the two variables is a float, compare as a float.
4500 * When using "=~" or "!~", always compare as string.
4501 */
4502 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4503 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4504 {
4505 float_T f1, f2;
4506
4507 if (rettv->v_type == VAR_FLOAT)
4508 f1 = rettv->vval.v_float;
4509 else
4510 f1 = get_tv_number(rettv);
4511 if (var2.v_type == VAR_FLOAT)
4512 f2 = var2.vval.v_float;
4513 else
4514 f2 = get_tv_number(&var2);
4515 n1 = FALSE;
4516 switch (type)
4517 {
4518 case TYPE_EQUAL: n1 = (f1 == f2); break;
4519 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4520 case TYPE_GREATER: n1 = (f1 > f2); break;
4521 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4522 case TYPE_SMALLER: n1 = (f1 < f2); break;
4523 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4524 case TYPE_UNKNOWN:
4525 case TYPE_MATCH:
4526 case TYPE_NOMATCH: break; /* avoid gcc warning */
4527 }
4528 }
4529#endif
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * If one of the two variables is a number, compare as a number.
4533 * When using "=~" or "!~", always compare as string.
4534 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 n1 = get_tv_number(rettv);
4539 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (n1 == n2); break;
4543 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4544 case TYPE_GREATER: n1 = (n1 > n2); break;
4545 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4546 case TYPE_SMALLER: n1 = (n1 < n2); break;
4547 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4548 case TYPE_UNKNOWN:
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH: break; /* avoid gcc warning */
4551 }
4552 }
4553 else
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 s1 = get_tv_string_buf(rettv, buf1);
4556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4559 else
4560 i = 0;
4561 n1 = FALSE;
4562 switch (type)
4563 {
4564 case TYPE_EQUAL: n1 = (i == 0); break;
4565 case TYPE_NEQUAL: n1 = (i != 0); break;
4566 case TYPE_GREATER: n1 = (i > 0); break;
4567 case TYPE_GEQUAL: n1 = (i >= 0); break;
4568 case TYPE_SMALLER: n1 = (i < 0); break;
4569 case TYPE_SEQUAL: n1 = (i <= 0); break;
4570
4571 case TYPE_MATCH:
4572 case TYPE_NOMATCH:
4573 /* avoid 'l' flag in 'cpoptions' */
4574 save_cpo = p_cpo;
4575 p_cpo = (char_u *)"";
4576 regmatch.regprog = vim_regcomp(s2,
4577 RE_MAGIC + RE_STRING);
4578 regmatch.rm_ic = ic;
4579 if (regmatch.regprog != NULL)
4580 {
4581 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (type == TYPE_NOMATCH)
4584 n1 = !n1;
4585 }
4586 p_cpo = save_cpo;
4587 break;
4588
4589 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4590 }
4591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Handle fourth level expression:
4604 * + number addition
4605 * - number subtraction
4606 * . string concatenation
4607 *
4608 * "arg" must point to the first non-white of the expression.
4609 * "arg" is advanced to the next non-white after the recognized expression.
4610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int evaluate;
4618{
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var2;
4620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int op;
4622 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623#ifdef FEAT_FLOAT
4624 float_T f1 = 0, f2 = 0;
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *s1, *s2;
4627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4628 char_u *p;
4629
4630 /*
4631 * Get the first variable.
4632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635
4636 /*
4637 * Repeat computing, until no '+', '-' or '.' is following.
4638 */
4639 for (;;)
4640 {
4641 op = **arg;
4642 if (op != '+' && op != '-' && op != '.')
4643 break;
4644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 if ((op != '+' || rettv->v_type != VAR_LIST)
4646#ifdef FEAT_FLOAT
4647 && (op == '.' || rettv->v_type != VAR_FLOAT)
4648#endif
4649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 {
4651 /* For "list + ...", an illegal use of the first operand as
4652 * a number cannot be determined before evaluating the 2nd
4653 * operand: if this is also a list, all is ok.
4654 * For "something . ...", "something - ..." or "non-list + ...",
4655 * we know that the first operand needs to be a string or number
4656 * without evaluating the 2nd operand. So check before to avoid
4657 * side effects after an error. */
4658 if (evaluate && get_tv_string_chk(rettv) == NULL)
4659 {
4660 clear_tv(rettv);
4661 return FAIL;
4662 }
4663 }
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * Get the second variable.
4667 */
4668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674
4675 if (evaluate)
4676 {
4677 /*
4678 * Compute the result.
4679 */
4680 if (op == '.')
4681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4683 s2 = get_tv_string_buf_chk(&var2, buf2);
4684 if (s2 == NULL) /* type error ? */
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 else if (op == '+' && rettv->v_type == VAR_LIST
4696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004697 {
4698 /* concatenate Lists */
4699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4700 &var3) == FAIL)
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
4706 clear_tv(rettv);
4707 *rettv = var3;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 int error = FALSE;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 f1 = rettv->vval.v_float;
4717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 else
4720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 n1 = get_tv_number_chk(rettv, &error);
4723 if (error)
4724 {
4725 /* This can only happen for "list + non-list". For
4726 * "non-list + ..." or "something - ...", we returned
4727 * before evaluating the 2nd operand. */
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 f1 = n1;
4734#endif
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 {
4739 f2 = var2.vval.v_float;
4740 n2 = 0;
4741 }
4742 else
4743#endif
4744 {
4745 n2 = get_tv_number_chk(&var2, &error);
4746 if (error)
4747 {
4748 clear_tv(rettv);
4749 clear_tv(&var2);
4750 return FAIL;
4751 }
4752#ifdef FEAT_FLOAT
4753 if (rettv->v_type == VAR_FLOAT)
4754 f2 = n2;
4755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758
4759#ifdef FEAT_FLOAT
4760 /* If there is a float on either side the result is a float. */
4761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4762 {
4763 if (op == '+')
4764 f1 = f1 + f2;
4765 else
4766 f1 = f1 - f2;
4767 rettv->v_type = VAR_FLOAT;
4768 rettv->vval.v_float = f1;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#endif
4772 {
4773 if (op == '+')
4774 n1 = n1 + n2;
4775 else
4776 n1 = n1 - n2;
4777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = n1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Handle fifth level expression:
4789 * * number multiplication
4790 * / number division
4791 * % number modulo
4792 *
4793 * "arg" must point to the first non-white of the expression.
4794 * "arg" is advanced to the next non-white after the recognized expression.
4795 *
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar33570922005-01-25 22:26:29 +00004805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 int op;
4807 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 int use_float = FALSE;
4810 float_T f1 = 0, f2;
4811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Get the first variable.
4816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819
4820 /*
4821 * Repeat computing, until no '*', '/' or '%' is following.
4822 */
4823 for (;;)
4824 {
4825 op = **arg;
4826 if (op != '*' && op != '/' && op != '%')
4827 break;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 {
4834 f1 = rettv->vval.v_float;
4835 use_float = TRUE;
4836 n1 = 0;
4837 }
4838 else
4839#endif
4840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (error)
4843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
4846 n1 = 0;
4847
4848 /*
4849 * Get the second variable.
4850 */
4851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return FAIL;
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (var2.v_type == VAR_FLOAT)
4859 {
4860 if (!use_float)
4861 {
4862 f1 = n1;
4863 use_float = TRUE;
4864 }
4865 f2 = var2.vval.v_float;
4866 n2 = 0;
4867 }
4868 else
4869#endif
4870 {
4871 n2 = get_tv_number_chk(&var2, &error);
4872 clear_tv(&var2);
4873 if (error)
4874 return FAIL;
4875#ifdef FEAT_FLOAT
4876 if (use_float)
4877 f2 = n2;
4878#endif
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 f1 = f1 * f2;
4890 else if (op == '/')
4891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# ifdef VMS
4893 /* VMS crashes on divide by zero, work around it */
4894 if (f2 == 0.0)
4895 {
4896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902 }
4903 else
4904 f1 = f1 / f2;
4905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 /* We rely on the floating point library to handle divide
4907 * by zero to result in "inf" and not a crash. */
4908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 return FAIL;
4915 }
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 if (op == '*')
4923 n1 = n1 * n2;
4924 else if (op == '/')
4925 {
4926 if (n2 == 0) /* give an error message? */
4927 {
4928 if (n1 == 0)
4929 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4930 else if (n1 < 0)
4931 n1 = -0x7fffffffL;
4932 else
4933 n1 = 0x7fffffffL;
4934 }
4935 else
4936 n1 = n1 / n2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 {
4940 if (n2 == 0) /* give an error message? */
4941 n1 = 0;
4942 else
4943 n1 = n1 % n2;
4944 }
4945 rettv->v_type = VAR_NUMBER;
4946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 return OK;
4952}
4953
4954/*
4955 * Handle sixth level expression:
4956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004957 * "string" string constant
4958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * &option-name option value
4960 * @r register contents
4961 * identifier variable value
4962 * function() function call
4963 * $VAR environment variable
4964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004965 * [expr, expr] List
4966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * Also handle:
4969 * ! in front logical NOT
4970 * - in front unary minus
4971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * trailing [] subscript in String or List
4973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * "arg" must point to the first non-white of the expression.
4976 * "arg" is advanced to the next non-white after the recognized expression.
4977 *
4978 * Return OK or FAIL.
4979 */
4980 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004985 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 long n;
4988 int len;
4989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *start_leader, *end_leader;
4991 int ret = OK;
4992 char_u *alias;
4993
4994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 /*
5001 * Skip '!' and '-' characters. They are handled later.
5002 */
5003 start_leader = *arg;
5004 while (**arg == '!' || **arg == '-' || **arg == '+')
5005 *arg = skipwhite(*arg + 1);
5006 end_leader = *arg;
5007
5008 switch (**arg)
5009 {
5010 /*
5011 * Number constant.
5012 */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
5020 case '7':
5021 case '8':
5022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 {
5024#ifdef FEAT_FLOAT
5025 char_u *p = skipdigits(*arg + 1);
5026 int get_float = FALSE;
5027
5028 /* We accept a float when the format matches
5029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005030 * strict to avoid backwards compatibility problems.
5031 * Don't look for a float after the "." operator, so that
5032 * ":let vers = 1.2.3" doesn't fail. */
5033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 get_float = TRUE;
5036 p = skipdigits(p + 2);
5037 if (*p == 'e' || *p == 'E')
5038 {
5039 ++p;
5040 if (*p == '-' || *p == '+')
5041 ++p;
5042 if (!vim_isdigit(*p))
5043 get_float = FALSE;
5044 else
5045 p = skipdigits(p + 1);
5046 }
5047 if (ASCII_ISALPHA(*p) || *p == '.')
5048 get_float = FALSE;
5049 }
5050 if (get_float)
5051 {
5052 float_T f;
5053
5054 *arg += string2float(*arg, &f);
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_FLOAT;
5058 rettv->vval.v_float = f;
5059 }
5060 }
5061 else
5062#endif
5063 {
5064 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5065 *arg += len;
5066 if (evaluate)
5067 {
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /*
5076 * String constant: "string".
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 break;
5086
5087 /*
5088 * List: [expr, expr]
5089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Dictionary: {key: val, key: val}
5095 */
5096 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5097 break;
5098
5099 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104
5105 /*
5106 * Environment variable: $VAR.
5107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Register contents: @r.
5113 */
5114 case '@': ++*arg;
5115 if (evaluate)
5116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005118 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (**arg != NUL)
5121 ++*arg;
5122 break;
5123
5124 /*
5125 * nested expression: (expression).
5126 */
5127 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (**arg == ')')
5130 ++*arg;
5131 else if (ret == OK)
5132 {
5133 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ret = FAIL;
5136 }
5137 break;
5138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142
5143 if (ret == NOTDONE)
5144 {
5145 /*
5146 * Must be a variable or function name.
5147 * Can also be a curly-braces kind of name: {expr}.
5148 */
5149 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 if (alias != NULL)
5152 s = alias;
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ret = FAIL;
5156 else
5157 {
5158 if (**arg == '(') /* recursive! */
5159 {
5160 /* If "s" is the name of a variable of type VAR_FUNC
5161 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005162 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163
5164 /* Invoke the function. */
5165 ret = get_func_tv(s, len, rettv, arg,
5166 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168
5169 /* If evaluate is FALSE rettv->v_type was not set in
5170 * get_func_tv, but it's needed in handle_subscript() to parse
5171 * what follows. So set it here. */
5172 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5173 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005174 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175 rettv->v_type = VAR_FUNC;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /* Stop the expression evaluation when immediately
5179 * aborting on error, or when an interrupt occurred or
5180 * an exception was thrown but not caught. */
5181 if (aborting())
5182 {
5183 if (ret == OK)
5184 clear_tv(rettv);
5185 ret = FAIL;
5186 }
5187 }
5188 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005189 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005190 else
5191 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005193 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 }
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *arg = skipwhite(*arg);
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5199 * expr(expr). */
5200 if (ret == OK)
5201 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5205 */
5206 if (ret == OK && evaluate && end_leader > start_leader)
5207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 int val = 0;
5210#ifdef FEAT_FLOAT
5211 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 if (rettv->v_type == VAR_FLOAT)
5214 f = rettv->vval.v_float;
5215 else
5216#endif
5217 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 clear_tv(rettv);
5221 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else
5224 {
5225 while (end_leader > start_leader)
5226 {
5227 --end_leader;
5228 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = !f;
5233 else
5234#endif
5235 val = !val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 {
5239#ifdef FEAT_FLOAT
5240 if (rettv->v_type == VAR_FLOAT)
5241 f = -f;
5242 else
5243#endif
5244 val = -val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 {
5250 clear_tv(rettv);
5251 rettv->vval.v_float = f;
5252 }
5253 else
5254#endif
5255 {
5256 clear_tv(rettv);
5257 rettv->v_type = VAR_NUMBER;
5258 rettv->vval.v_number = val;
5259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5268 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5270 */
5271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
5278 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 long len = -1;
5282 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005286 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 if (verbose)
5289 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
5291 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292#ifdef FEAT_FLOAT
5293 else if (rettv->v_type == VAR_FLOAT)
5294 {
5295 if (verbose)
5296 EMSG(_(e_float_as_string));
5297 return FAIL;
5298 }
5299#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * dict.name
5305 */
5306 key = *arg + 1;
5307 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5308 ;
5309 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 /*
5316 * something[idx]
5317 *
5318 * Get the (first) variable from inside the [].
5319 */
5320 *arg = skipwhite(*arg + 1);
5321 if (**arg == ':')
5322 empty1 = TRUE;
5323 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5324 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5326 {
5327 /* not a number or string */
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331
5332 /*
5333 * Get the second variable from inside the [:].
5334 */
5335 if (**arg == ':')
5336 {
5337 range = TRUE;
5338 *arg = skipwhite(*arg + 1);
5339 if (**arg == ']')
5340 empty2 = TRUE;
5341 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5348 {
5349 /* not a number or string */
5350 if (!empty1)
5351 clear_tv(&var1);
5352 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 return FAIL;
5354 }
5355 }
5356
5357 /* Check for the ']'. */
5358 if (**arg != ']')
5359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (verbose)
5361 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 clear_tv(&var1);
5363 if (range)
5364 clear_tv(&var2);
5365 return FAIL;
5366 }
5367 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369
5370 if (evaluate)
5371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 n1 = 0;
5373 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n1 = get_tv_number(&var1);
5376 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 if (range)
5379 {
5380 if (empty2)
5381 n2 = -1;
5382 else
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 n2 = get_tv_number(&var2);
5385 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 }
5388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
5391 case VAR_NUMBER:
5392 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 len = (long)STRLEN(s);
5395 if (range)
5396 {
5397 /* The resulting variable is a substring. If the indexes
5398 * are out of range the result is empty. */
5399 if (n1 < 0)
5400 {
5401 n1 = len + n1;
5402 if (n1 < 0)
5403 n1 = 0;
5404 }
5405 if (n2 < 0)
5406 n2 = len + n2;
5407 else if (n2 >= len)
5408 n2 = len;
5409 if (n1 >= len || n2 < 0 || n1 > n2)
5410 s = NULL;
5411 else
5412 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5413 }
5414 else
5415 {
5416 /* The resulting variable is a string of a single
5417 * character. If the index is too big or negative the
5418 * result is empty. */
5419 if (n1 >= len || n1 < 0)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, 1);
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 break;
5428
5429 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 if (n1 < 0)
5432 n1 = len + n1;
5433 if (!empty1 && (n1 < 0 || n1 >= len))
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 /* For a range we allow invalid values and return an empty
5436 * list. A list index out of range is an error. */
5437 if (!range)
5438 {
5439 if (verbose)
5440 EMSGN(_(e_listidx), n1);
5441 return FAIL;
5442 }
5443 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 if (range)
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 list_T *l;
5448 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449
5450 if (n2 < 0)
5451 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 else if (n2 >= len)
5453 n2 = len - 1;
5454 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 l = list_alloc();
5457 if (l == NULL)
5458 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 n1 <= n2; ++n1)
5461 {
5462 if (list_append_tv(l, &item->li_tv) == FAIL)
5463 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005464 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 return FAIL;
5466 }
5467 item = item->li_next;
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_LIST;
5471 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005472 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
5474 else
5475 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005476 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 case VAR_DICT:
5483 if (range)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
5494 if (len == -1)
5495 {
5496 key = get_tv_string(&var1);
5497 if (*key == NUL)
5498 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (verbose)
5500 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&var1);
5502 return FAIL;
5503 }
5504 }
5505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 if (len == -1)
5511 clear_tv(&var1);
5512 if (item == NULL)
5513 return FAIL;
5514
5515 copy_tv(&item->di_tv, &var1);
5516 clear_tv(rettv);
5517 *rettv = var1;
5518 }
5519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 }
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return OK;
5524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Get an option value.
5528 * "arg" points to the '&' or '+' before the option name.
5529 * "arg" is advanced to character after the option name.
5530 * Return OK or FAIL.
5531 */
5532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 int evaluate;
5537{
5538 char_u *option_end;
5539 long numval;
5540 char_u *stringval;
5541 int opt_type;
5542 int c;
5543 int working = (**arg == '+'); /* has("+option") */
5544 int ret = OK;
5545 int opt_flags;
5546
5547 /*
5548 * Isolate the option name and find its value.
5549 */
5550 option_end = find_option_end(arg, &opt_flags);
5551 if (option_end == NULL)
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 EMSG2(_("E112: Option name missing: %s"), *arg);
5555 return FAIL;
5556 }
5557
5558 if (!evaluate)
5559 {
5560 *arg = option_end;
5561 return OK;
5562 }
5563
5564 c = *option_end;
5565 *option_end = NUL;
5566 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (opt_type == -3) /* invalid name */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 EMSG2(_("E113: Unknown option: %s"), *arg);
5573 ret = FAIL;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (opt_type == -2) /* hidden string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == -1) /* hidden number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == 1) /* number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else /* string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
5598 else if (working && (opt_type == -2 || opt_type == -1))
5599 ret = FAIL;
5600
5601 *option_end = c; /* put back for error messages */
5602 *arg = option_end;
5603
5604 return ret;
5605}
5606
5607/*
5608 * Allocate a variable for a string constant.
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
5618 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int extra = 0;
5620
5621 /*
5622 * Find the end of the string, skipping backslashed characters.
5623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 if (*p == '\\' && p[1] != NUL)
5627 {
5628 ++p;
5629 /* A "\<x>" form occupies at least 4 characters, and produces up
5630 * to 6 characters: reserve space for 2 extra */
5631 if (*p == '<')
5632 extra += 2;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 if (*p != '"')
5637 {
5638 EMSG2(_("E114: Missing quote: %s"), *arg);
5639 return FAIL;
5640 }
5641
5642 /* If only parsing, set *arg and return here */
5643 if (!evaluate)
5644 {
5645 *arg = p + 1;
5646 return OK;
5647 }
5648
5649 /*
5650 * Copy the string into allocated memory, handling backslashed
5651 * characters.
5652 */
5653 name = alloc((unsigned)(p - *arg + extra));
5654 if (name == NULL)
5655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (*p == '\\')
5662 {
5663 switch (*++p)
5664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 case 'b': *name++ = BS; ++p; break;
5666 case 'e': *name++ = ESC; ++p; break;
5667 case 'f': *name++ = FF; ++p; break;
5668 case 'n': *name++ = NL; ++p; break;
5669 case 'r': *name++ = CAR; ++p; break;
5670 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 case 'X': /* hex: "\x1", "\x12" */
5673 case 'x':
5674 case 'u': /* Unicode: "\u0023" */
5675 case 'U':
5676 if (vim_isxdigit(p[1]))
5677 {
5678 int n, nr;
5679 int c = toupper(*p);
5680
5681 if (c == 'X')
5682 n = 2;
5683 else
5684 n = 4;
5685 nr = 0;
5686 while (--n >= 0 && vim_isxdigit(p[1]))
5687 {
5688 ++p;
5689 nr = (nr << 4) + hex2nr(*p);
5690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_MBYTE
5693 /* For "\u" store the number according to
5694 * 'encoding'. */
5695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702
5703 /* octal: "\1", "\12", "\123" */
5704 case '0':
5705 case '1':
5706 case '2':
5707 case '3':
5708 case '4':
5709 case '5':
5710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '7': *name = *p++ - '0';
5712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name = (*name << 3) + *p++ - '0';
5715 if (*p >= '0' && *p <= '7')
5716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720
5721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (extra != 0)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 /* FALLTHROUGH */
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 }
5734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 *arg = p + 1;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 int evaluate;
5753{
5754 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 int reduce = 0;
5757
5758 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 */
5761 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 break;
5767 ++reduce;
5768 ++p;
5769 }
5770 }
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 return FAIL;
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 if (!evaluate)
5780 {
5781 *arg = p + 1;
5782 return OK;
5783 }
5784
5785 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 */
5788 str = alloc((unsigned)((p - *arg) - reduce));
5789 if (str == NULL)
5790 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 break;
5800 ++p;
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 *arg = p + 1;
5806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Allocate a variable for a List and fill it from "*arg".
5812 * Return OK or FAIL.
5813 */
5814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int evaluate;
5819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l = NULL;
5821 typval_T tv;
5822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
5824 if (evaluate)
5825 {
5826 l = list_alloc();
5827 if (l == NULL)
5828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 while (**arg != ']' && **arg != NUL)
5833 {
5834 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5835 goto failret;
5836 if (evaluate)
5837 {
5838 item = listitem_alloc();
5839 if (item != NULL)
5840 {
5841 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 list_append(l, item);
5844 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005845 else
5846 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 }
5848
5849 if (**arg == ']')
5850 break;
5851 if (**arg != ',')
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 goto failret;
5855 }
5856 *arg = skipwhite(*arg + 1);
5857 }
5858
5859 if (**arg != ']')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862failret:
5863 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005864 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 if (evaluate)
5870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 rettv->v_type = VAR_LIST;
5872 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 ++l->lv_refcount;
5874 }
5875
5876 return OK;
5877}
5878
5879/*
5880 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005881 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005883 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_alloc()
5885{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 list_T *l;
5887
5888 l = (list_T *)alloc_clear(sizeof(list_T));
5889 if (l != NULL)
5890 {
5891 /* Prepend the list to the list of lists for garbage collection. */
5892 if (first_list != NULL)
5893 first_list->lv_used_prev = l;
5894 l->lv_used_prev = NULL;
5895 l->lv_used_next = first_list;
5896 first_list = l;
5897 }
5898 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005902 * Allocate an empty list for a return value.
5903 * Returns OK or FAIL.
5904 */
5905 static int
5906rettv_list_alloc(rettv)
5907 typval_T *rettv;
5908{
5909 list_T *l = list_alloc();
5910
5911 if (l == NULL)
5912 return FAIL;
5913
5914 rettv->vval.v_list = l;
5915 rettv->v_type = VAR_LIST;
5916 ++l->lv_refcount;
5917 return OK;
5918}
5919
5920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 * Unreference a list: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005924 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928 if (l != NULL && --l->lv_refcount <= 0)
5929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
5933 * Free a list, including all items it points to.
5934 * Ignores the reference count.
5935 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005936 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937list_free(l, recurse)
5938 list_T *l;
5939 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 /* Remove the list from the list of lists for garbage collection. */
5944 if (l->lv_used_prev == NULL)
5945 first_list = l->lv_used_next;
5946 else
5947 l->lv_used_prev->lv_used_next = l->lv_used_next;
5948 if (l->lv_used_next != NULL)
5949 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5950
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953 /* Remove the item before deleting it. */
5954 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 if (recurse || (item->li_tv.v_type != VAR_LIST
5956 && item->li_tv.v_type != VAR_DICT))
5957 clear_tv(&item->li_tv);
5958 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960 vim_free(l);
5961}
5962
5963/*
5964 * Allocate a list item.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_alloc()
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 vim_free(item);
5981}
5982
5983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005984 * Remove a list item from a List and free it. Also clears the value.
5985 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
5989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990{
5991 list_remove(l, item, item);
5992 listitem_free(item);
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Get the number of items in a list.
5997 */
5998 static long
5999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (l == NULL)
6003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 * Return TRUE when two lists have exactly the same values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l1;
6013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006019 if (l1 == NULL || l2 == NULL)
6020 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006021 if (l1 == l2)
6022 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 if (list_len(l1) != list_len(l2))
6024 return FALSE;
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026 for (item1 = l1->lv_first, item2 = l2->lv_first;
6027 item1 != NULL && item2 != NULL;
6028 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 return FALSE;
6031 return item1 == NULL && item2 == NULL;
6032}
6033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006034#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6035 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036/*
6037 * Return the dictitem that an entry in a hashtable points to.
6038 */
6039 dictitem_T *
6040dict_lookup(hi)
6041 hashitem_T *hi;
6042{
6043 return HI2DI(hi);
6044}
6045#endif
6046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 * Return TRUE when two dictionaries have exactly the same key/values.
6049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *d1;
6053 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 hashitem_T *hi;
6058 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006061 if (d1 == NULL || d2 == NULL)
6062 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (d1 == d2)
6064 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 if (dict_len(d1) != dict_len(d2))
6066 return FALSE;
6067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 item2 = dict_find(d2, hi->hi_key, -1);
6074 if (item2 == NULL)
6075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return FALSE;
6078 --todo;
6079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 }
6081 return TRUE;
6082}
6083
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084static int tv_equal_recurse_limit;
6085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 */
6091 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T *tv1;
6094 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 int ic; /* ignore case */
6096 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
6098 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006101 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 * recursiveness to a limit. We guess they are equal then.
6108 * A fixed limit has the problem of still taking an awful long time.
6109 * Reduce the limit every time running into it. That should work fine for
6110 * deeply linked structures that are not recursively linked and catch
6111 * recursiveness quickly. */
6112 if (!recursive)
6113 tv_equal_recurse_limit = 1000;
6114 if (recursive_cnt >= tv_equal_recurse_limit)
6115 {
6116 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 ++recursive_cnt;
6124 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6125 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006126 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_FUNC:
6135 return (tv1->vval.v_string != NULL
6136 && tv2->vval.v_string != NULL
6137 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6138
6139 case VAR_NUMBER:
6140 return tv1->vval.v_number == tv2->vval.v_number;
6141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142#ifdef FEAT_FLOAT
6143 case VAR_FLOAT:
6144 return tv1->vval.v_float == tv2->vval.v_float;
6145#endif
6146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 case VAR_STRING:
6148 s1 = get_tv_string_buf(tv1, buf1);
6149 s2 = get_tv_string_buf(tv2, buf2);
6150 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152
6153 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154 return TRUE;
6155}
6156
6157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 * Locate item with index "n" in list "l" and return it.
6159 * A negative index is counted from the end; -1 is the last item.
6160 * Returns NULL when "n" is out of range.
6161 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 long n;
6166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 long idx;
6169
6170 if (l == NULL)
6171 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172
6173 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 n = l->lv_len + n;
6176
6177 /* Check for index out of range. */
6178 if (n < 0 || n >= l->lv_len)
6179 return NULL;
6180
6181 /* When there is a cached index may start search from there. */
6182 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_idx / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else if (n > (l->lv_idx + l->lv_len) / 2)
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
6196 else
6197 {
6198 /* closest to the cached index */
6199 item = l->lv_idx_item;
6200 idx = l->lv_idx;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
6203 else
6204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 if (n < l->lv_len / 2)
6206 {
6207 /* closest to the start of the list */
6208 item = l->lv_first;
6209 idx = 0;
6210 }
6211 else
6212 {
6213 /* closest to the end of the list */
6214 item = l->lv_last;
6215 idx = l->lv_len - 1;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 while (n > idx)
6220 {
6221 /* search forward */
6222 item = item->li_next;
6223 ++idx;
6224 }
6225 while (n < idx)
6226 {
6227 /* search backward */
6228 item = item->li_prev;
6229 --idx;
6230 }
6231
6232 /* cache the used index */
6233 l->lv_idx = idx;
6234 l->lv_idx_item = item;
6235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 return item;
6237}
6238
6239/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006240 * Get list item "l[idx]" as a number.
6241 */
6242 static long
6243list_find_nr(l, idx, errorp)
6244 list_T *l;
6245 long idx;
6246 int *errorp; /* set to TRUE when something wrong */
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx);
6251 if (li == NULL)
6252 {
6253 if (errorp != NULL)
6254 *errorp = TRUE;
6255 return -1L;
6256 }
6257 return get_tv_number_chk(&li->li_tv, errorp);
6258}
6259
6260/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6262 */
6263 char_u *
6264list_find_str(l, idx)
6265 list_T *l;
6266 long idx;
6267{
6268 listitem_T *li;
6269
6270 li = list_find(l, idx - 1);
6271 if (li == NULL)
6272 {
6273 EMSGN(_(e_listidx), idx);
6274 return NULL;
6275 }
6276 return get_tv_string(&li->li_tv);
6277}
6278
6279/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280 * Locate "item" list "l" and return its index.
6281 * Returns -1 when "item" is not in the list.
6282 */
6283 static long
6284list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287{
6288 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290
6291 if (l == NULL)
6292 return -1;
6293 idx = 0;
6294 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6295 ++idx;
6296 if (li == NULL)
6297 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006298 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006299}
6300
6301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 * Append item "item" to the end of list "l".
6303 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l;
6307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308{
6309 if (l->lv_last == NULL)
6310 {
6311 /* empty list */
6312 l->lv_first = item;
6313 l->lv_last = item;
6314 item->li_prev = NULL;
6315 }
6316 else
6317 {
6318 l->lv_last->li_next = item;
6319 item->li_prev = l->lv_last;
6320 l->lv_last = item;
6321 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 item->li_next = NULL;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006330 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
6333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 copy_tv(tv, &li->li_tv);
6340 list_append(l, li);
6341 return OK;
6342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 * Return FAIL when out of memory.
6347 */
6348 int
6349list_append_dict(list, dict)
6350 list_T *list;
6351 dict_T *dict;
6352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_DICT;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_dict = dict;
6360 list_append(list, li);
6361 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return OK;
6363}
6364
6365/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 * Returns FAIL when out of memory.
6369 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 list_T *l;
6373 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375{
6376 listitem_T *li = listitem_alloc();
6377
6378 if (li == NULL)
6379 return FAIL;
6380 list_append(l, li);
6381 li->li_tv.v_type = VAR_STRING;
6382 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006383 if (str == NULL)
6384 li->li_tv.vval.v_string = NULL;
6385 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 return FAIL;
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006392 * Append "n" to list "l".
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_append_number(l, n)
6397 list_T *l;
6398 varnumber_T n;
6399{
6400 listitem_T *li;
6401
6402 li = listitem_alloc();
6403 if (li == NULL)
6404 return FAIL;
6405 li->li_tv.v_type = VAR_NUMBER;
6406 li->li_tv.v_lock = 0;
6407 li->li_tv.vval.v_number = n;
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * If "item" is NULL append at the end.
6415 * Return FAIL when out of memory.
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l;
6420 typval_T *tv;
6421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
6425 if (ni == NULL)
6426 return FAIL;
6427 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006428 list_insert(l, ni, item);
6429 return OK;
6430}
6431
6432 void
6433list_insert(l, ni, item)
6434 list_T *l;
6435 listitem_T *ni;
6436 listitem_T *item;
6437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 if (item == NULL)
6439 /* Append new item at end of list. */
6440 list_append(l, ni);
6441 else
6442 {
6443 /* Insert new item before existing item. */
6444 ni->li_prev = item->li_prev;
6445 ni->li_next = item;
6446 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 ++l->lv_idx;
6450 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 l->lv_idx_item = NULL;
6455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459}
6460
6461/*
6462 * Extend "l1" with "l2".
6463 * If "bef" is NULL append at the end, otherwise insert before this item.
6464 * Returns FAIL when out of memory.
6465 */
6466 static int
6467list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006473 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006475 /* We also quit the loop when we have inserted the original item count of
6476 * the list, avoid a hang when we extend a list with itself. */
6477 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6479 return FAIL;
6480 return OK;
6481}
6482
6483/*
6484 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6485 * Return FAIL when out of memory.
6486 */
6487 static int
6488list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l1;
6490 list_T *l2;
6491 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006495 if (l1 == NULL || l2 == NULL)
6496 return FAIL;
6497
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 if (l == NULL)
6501 return FAIL;
6502 tv->v_type = VAR_LIST;
6503 tv->vval.v_list = l;
6504
6505 /* append all items from the second list */
6506 return list_extend(l, l2, NULL);
6507}
6508
6509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * Returns NULL when out of memory.
6514 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 list_T *copy;
6522 listitem_T *item;
6523 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
6525 if (orig == NULL)
6526 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527
6528 copy = list_alloc();
6529 if (copy != NULL)
6530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (copyID != 0)
6532 {
6533 /* Do this before adding the items, because one of the items may
6534 * refer back to this list. */
6535 orig->lv_copyID = copyID;
6536 orig->lv_copylist = copy;
6537 }
6538 for (item = orig->lv_first; item != NULL && !got_int;
6539 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 {
6541 ni = listitem_alloc();
6542 if (ni == NULL)
6543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 {
6546 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6547 {
6548 vim_free(ni);
6549 break;
6550 }
6551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 list_append(copy, ni);
6555 }
6556 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 if (item != NULL)
6558 {
6559 list_unref(copy);
6560 copy = NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
6563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 return copy;
6565}
6566
6567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006569 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006572list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l;
6574 listitem_T *item;
6575 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 /* notify watchers */
6580 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 list_fix_watch(l, ip);
6584 if (ip == item2)
6585 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587
6588 if (item2->li_next == NULL)
6589 l->lv_last = item->li_prev;
6590 else
6591 item2->li_next->li_prev = item->li_prev;
6592 if (item->li_prev == NULL)
6593 l->lv_first = item2->li_next;
6594 else
6595 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006596 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597}
6598
6599/*
6600 * Return an allocated string with the string representation of a list.
6601 * May return NULL.
6602 */
6603 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607{
6608 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 if (tv->vval.v_list == NULL)
6611 return NULL;
6612 ga_init2(&ga, (int)sizeof(char), 80);
6613 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 {
6616 vim_free(ga.ga_data);
6617 return NULL;
6618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 ga_append(&ga, ']');
6620 ga_append(&ga, NUL);
6621 return (char_u *)ga.ga_data;
6622}
6623
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006624typedef struct join_S {
6625 char_u *s;
6626 char_u *tofree;
6627} join_T;
6628
6629 static int
6630list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6631 garray_T *gap; /* to store the result in */
6632 list_T *l;
6633 char_u *sep;
6634 int echo_style;
6635 int copyID;
6636 garray_T *join_gap; /* to keep each list item string */
6637{
6638 int i;
6639 join_T *p;
6640 int len;
6641 int sumlen = 0;
6642 int first = TRUE;
6643 char_u *tofree;
6644 char_u numbuf[NUMBUFLEN];
6645 listitem_T *item;
6646 char_u *s;
6647
6648 /* Stringify each item in the list. */
6649 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6650 {
6651 if (echo_style)
6652 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6653 else
6654 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6655 if (s == NULL)
6656 return FAIL;
6657
6658 len = (int)STRLEN(s);
6659 sumlen += len;
6660
6661 ga_grow(join_gap, 1);
6662 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6663 if (tofree != NULL || s != numbuf)
6664 {
6665 p->s = s;
6666 p->tofree = tofree;
6667 }
6668 else
6669 {
6670 p->s = vim_strnsave(s, len);
6671 p->tofree = p->s;
6672 }
6673
6674 line_breakcheck();
6675 }
6676
6677 /* Allocate result buffer with its total size, avoid re-allocation and
6678 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6679 if (join_gap->ga_len >= 2)
6680 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6681 if (ga_grow(gap, sumlen + 2) == FAIL)
6682 return FAIL;
6683
6684 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6685 {
6686 if (first)
6687 first = FALSE;
6688 else
6689 ga_concat(gap, sep);
6690 p = ((join_T *)join_gap->ga_data) + i;
6691
6692 if (p->s != NULL)
6693 ga_concat(gap, p->s);
6694 line_breakcheck();
6695 }
6696
6697 return OK;
6698}
6699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006702 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006706list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006710 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713 garray_T join_ga;
6714 int retval;
6715 join_T *p;
6716 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6719 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6720
6721 /* Dispose each item in join_ga. */
6722 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = (join_T *)join_ga.ga_data;
6725 for (i = 0; i < join_ga.ga_len; ++i)
6726 {
6727 vim_free(p->tofree);
6728 ++p;
6729 }
6730 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732
6733 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734}
6735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Garbage collection for lists and dictionaries.
6738 *
6739 * We use reference counts to be able to free most items right away when they
6740 * are no longer used. But for composite items it's possible that it becomes
6741 * unused while the reference count is > 0: When there is a recursive
6742 * reference. Example:
6743 * :let l = [1, 2, 3]
6744 * :let d = {9: l}
6745 * :let l[1] = d
6746 *
6747 * Since this is quite unusual we handle this with garbage collection: every
6748 * once in a while find out which lists and dicts are not referenced from any
6749 * variable.
6750 *
6751 * Here is a good reference text about garbage collection (refers to Python
6752 * but it applies to all reference-counting mechanisms):
6753 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 * Do garbage collection for lists and dicts.
6758 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 int
6761garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 buf_T *buf;
6765 win_T *wp;
6766 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006767 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 int did_free;
6769 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006770#ifdef FEAT_WINDOWS
6771 tabpage_T *tp;
6772#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006774 /* Only do this once. */
6775 want_garbage_collect = FALSE;
6776 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006777 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006778
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006779 /* We advance by two because we add one for items referenced through
6780 * previous_funccal. */
6781 current_copyID += COPYID_INC;
6782 copyID = current_copyID;
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /*
6785 * 1. Go through all accessible variables and mark all lists and dicts
6786 * with copyID.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788
6789 /* Don't free variables in the previous_funccal list unless they are only
6790 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006791 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6796 }
6797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* script-local variables */
6799 for (i = 1; i <= ga_scripts.ga_len; ++i)
6800 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6801
6802 /* buffer-local variables */
6803 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006804 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805
6806 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006807 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006808 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006809#ifdef FEAT_AUTOCMD
6810 if (aucmd_win != NULL)
6811 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814#ifdef FEAT_WINDOWS
6815 /* tabpage-local variables */
6816 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006817 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818#endif
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /* global variables */
6821 set_ref_in_ht(&globvarht, copyID);
6822
6823 /* function-local variables */
6824 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6825 {
6826 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6827 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6828 }
6829
Bram Moolenaard812df62008-11-09 12:46:09 +00006830 /* v: vars */
6831 set_ref_in_ht(&vimvarht, copyID);
6832
Bram Moolenaar1dced572012-04-05 16:54:08 +02006833#ifdef FEAT_LUA
6834 set_ref_in_lua(copyID);
6835#endif
6836
Bram Moolenaardb913952012-06-29 12:54:53 +02006837#ifdef FEAT_PYTHON
6838 set_ref_in_python(copyID);
6839#endif
6840
6841#ifdef FEAT_PYTHON3
6842 set_ref_in_python3(copyID);
6843#endif
6844
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006845 /*
6846 * 2. Free lists and dictionaries that are not referenced.
6847 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 did_free = free_unref_items(copyID);
6849
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 /*
6851 * 3. Check if any funccal can be freed now.
6852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 for (pfc = &previous_funccal; *pfc != NULL; )
6854 {
6855 if (can_free_funccal(*pfc, copyID))
6856 {
6857 fc = *pfc;
6858 *pfc = fc->caller;
6859 free_funccal(fc, TRUE);
6860 did_free = TRUE;
6861 did_free_funccal = TRUE;
6862 }
6863 else
6864 pfc = &(*pfc)->caller;
6865 }
6866 if (did_free_funccal)
6867 /* When a funccal was freed some more items might be garbage
6868 * collected, so run again. */
6869 (void)garbage_collect();
6870
6871 return did_free;
6872}
6873
6874/*
6875 * Free lists and dictionaries that are no longer referenced.
6876 */
6877 static int
6878free_unref_items(copyID)
6879 int copyID;
6880{
6881 dict_T *dd;
6882 list_T *ll;
6883 int did_free = FALSE;
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the Dictionary and ordinary items it contains, but don't
6892 * recurse into Lists and Dictionaries, they will be in the list
6893 * of dicts or list of lists. */
6894 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
6897 /* restart, next dict may also have been freed */
6898 dd = first_dict;
6899 }
6900 else
6901 dd = dd->dv_used_next;
6902
6903 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006904 * Go through the list of lists and free items without the copyID.
6905 * But don't free a list that has a watcher (used in a for loop), these
6906 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 */
6908 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6910 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the List and ordinary items it contains, but don't recurse
6913 * into Lists and Dictionaries, they will be in the list of dicts
6914 * or list of lists. */
6915 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006918 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 ll = first_list;
6920 }
6921 else
6922 ll = ll->lv_used_next;
6923
6924 return did_free;
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_ht(ht, copyID)
6932 hashtab_T *ht;
6933 int copyID;
6934{
6935 int todo;
6936 hashitem_T *hi;
6937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006938 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 for (hi = ht->ht_array; todo > 0; ++hi)
6940 if (!HASHITEM_EMPTY(hi))
6941 {
6942 --todo;
6943 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6944 }
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through list "l" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_list(l, copyID)
6952 list_T *l;
6953 int copyID;
6954{
6955 listitem_T *li;
6956
6957 for (li = l->lv_first; li != NULL; li = li->li_next)
6958 set_ref_in_item(&li->li_tv, copyID);
6959}
6960
6961/*
6962 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6963 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006964 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965set_ref_in_item(tv, copyID)
6966 typval_T *tv;
6967 int copyID;
6968{
6969 dict_T *dd;
6970 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971
6972 switch (tv->v_type)
6973 {
6974 case VAR_DICT:
6975 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006976 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /* Didn't see this dict yet. */
6979 dd->dv_copyID = copyID;
6980 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 case VAR_LIST:
6985 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this list yet. */
6989 ll->lv_copyID = copyID;
6990 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995}
6996
6997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998 * Allocate an empty header for a dictionary.
6999 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001dict_alloc()
7002{
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007007 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007008 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 if (first_dict != NULL)
7010 first_dict->dv_used_prev = d;
7011 d->dv_used_next = first_dict;
7012 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007013 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007016 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007017 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007018 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007020 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007021 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007025 * Allocate an empty dict for a return value.
7026 * Returns OK or FAIL.
7027 */
7028 static int
7029rettv_dict_alloc(rettv)
7030 typval_T *rettv;
7031{
7032 dict_T *d = dict_alloc();
7033
7034 if (d == NULL)
7035 return FAIL;
7036
7037 rettv->vval.v_dict = d;
7038 rettv->v_type = VAR_DICT;
7039 ++d->dv_refcount;
7040 return OK;
7041}
7042
7043
7044/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 * Unreference a Dictionary: decrement the reference count and free it when it
7046 * becomes zero.
7047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007048 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052 if (d != NULL && --d->dv_refcount <= 0)
7053 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054}
7055
7056/*
7057 * Free a Dictionary, including all items it contains.
7058 * Ignores the reference count.
7059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007060 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007061dict_free(d, recurse)
7062 dict_T *d;
7063 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007067 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 /* Remove the dict from the list of dicts for garbage collection. */
7070 if (d->dv_used_prev == NULL)
7071 first_dict = d->dv_used_next;
7072 else
7073 d->dv_used_prev->dv_used_next = d->dv_used_next;
7074 if (d->dv_used_next != NULL)
7075 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7076
7077 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (!HASHITEM_EMPTY(hi))
7083 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 /* Remove the item before deleting it, just in case there is
7085 * something recursive causing trouble. */
7086 di = HI2DI(hi);
7087 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007088 if (recurse || (di->di_tv.v_type != VAR_LIST
7089 && di->di_tv.v_type != VAR_DICT))
7090 clear_tv(&di->di_tv);
7091 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 --todo;
7093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 vim_free(d);
7097}
7098
7099/*
7100 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 * The "key" is copied to the new item.
7102 * Note that the value of the item "di_tv" still needs to be initialized!
7103 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007105 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106dictitem_alloc(key)
7107 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007115 di->di_flags = 0;
7116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118}
7119
7120/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 * Make a copy of a Dictionary item.
7122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007124dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7130 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 if (di != NULL)
7132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 copy_tv(&org->di_tv, &di->di_tv);
7136 }
7137 return di;
7138}
7139
7140/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 * Remove item "item" from Dictionary "dict" and free it.
7142 */
7143 static void
7144dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *dict;
7146 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 if (HASHITEM_EMPTY(hi))
7152 EMSG2(_(e_intern2), "dictitem_remove()");
7153 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 dictitem_free(item);
7156}
7157
7158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159 * Free a dict item. Also clears the value.
7160 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007161 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 clear_tv(&item->di_tv);
7166 vim_free(item);
7167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7171 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 * Returns NULL when out of memory.
7174 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *copy;
7182 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185
7186 if (orig == NULL)
7187 return NULL;
7188
7189 copy = dict_alloc();
7190 if (copy != NULL)
7191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 if (copyID != 0)
7193 {
7194 orig->dv_copyID = copyID;
7195 orig->dv_copydict = copy;
7196 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 di = dictitem_alloc(hi->hi_key);
7205 if (di == NULL)
7206 break;
7207 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 {
7209 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7210 copyID) == FAIL)
7211 {
7212 vim_free(di);
7213 break;
7214 }
7215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 else
7217 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7218 if (dict_add(copy, di) == FAIL)
7219 {
7220 dictitem_free(di);
7221 break;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 if (todo > 0)
7228 {
7229 dict_unref(copy);
7230 copy = NULL;
7231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 }
7233
7234 return copy;
7235}
7236
7237/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007239 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007241 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *d;
7244 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247}
7248
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007250 * Add a number or string entry to dictionary "d".
7251 * When "str" is NULL use number "nr", otherwise use "str".
7252 * Returns FAIL when out of memory and when key already exists.
7253 */
7254 int
7255dict_add_nr_str(d, key, nr, str)
7256 dict_T *d;
7257 char *key;
7258 long nr;
7259 char_u *str;
7260{
7261 dictitem_T *item;
7262
7263 item = dictitem_alloc((char_u *)key);
7264 if (item == NULL)
7265 return FAIL;
7266 item->di_tv.v_lock = 0;
7267 if (str == NULL)
7268 {
7269 item->di_tv.v_type = VAR_NUMBER;
7270 item->di_tv.vval.v_number = nr;
7271 }
7272 else
7273 {
7274 item->di_tv.v_type = VAR_STRING;
7275 item->di_tv.vval.v_string = vim_strsave(str);
7276 }
7277 if (dict_add(d, item) == FAIL)
7278 {
7279 dictitem_free(item);
7280 return FAIL;
7281 }
7282 return OK;
7283}
7284
7285/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007286 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007287 * Returns FAIL when out of memory and when key already exists.
7288 */
7289 int
7290dict_add_list(d, key, list)
7291 dict_T *d;
7292 char *key;
7293 list_T *list;
7294{
7295 dictitem_T *item;
7296
7297 item = dictitem_alloc((char_u *)key);
7298 if (item == NULL)
7299 return FAIL;
7300 item->di_tv.v_lock = 0;
7301 item->di_tv.v_type = VAR_LIST;
7302 item->di_tv.vval.v_list = list;
7303 if (dict_add(d, item) == FAIL)
7304 {
7305 dictitem_free(item);
7306 return FAIL;
7307 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007308 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007309 return OK;
7310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Get the number of items in a Dictionary.
7314 */
7315 static long
7316dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 if (d == NULL)
7320 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322}
7323
7324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 * Find item "key[len]" in Dictionary "d".
7326 * If "len" is negative use strlen(key).
7327 * Returns NULL when not found.
7328 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007329 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 char_u *key;
7333 int len;
7334{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335#define AKEYLEN 200
7336 char_u buf[AKEYLEN];
7337 char_u *akey;
7338 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 if (len < 0)
7342 akey = key;
7343 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 tofree = akey = vim_strnsave(key, len);
7346 if (akey == NULL)
7347 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 else
7350 {
7351 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007352 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 akey = buf;
7354 }
7355
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 vim_free(tofree);
7358 if (HASHITEM_EMPTY(hi))
7359 return NULL;
7360 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361}
7362
7363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007364 * Get a string item from a dictionary.
7365 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366 * Returns NULL if the entry doesn't exist or out of memory.
7367 */
7368 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007369get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007370 dict_T *d;
7371 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007372 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007373{
7374 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 s = get_tv_string(&di->di_tv);
7381 if (save && s != NULL)
7382 s = vim_strsave(s);
7383 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007384}
7385
7386/*
7387 * Get a number item from a dictionary.
7388 * Returns 0 if the entry doesn't exist or out of memory.
7389 */
7390 long
7391get_dict_number(d, key)
7392 dict_T *d;
7393 char_u *key;
7394{
7395 dictitem_T *di;
7396
7397 di = dict_find(d, key, -1);
7398 if (di == NULL)
7399 return 0;
7400 return get_tv_number(&di->di_tv);
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Return an allocated string with the string representation of a Dictionary.
7405 * May return NULL.
7406 */
7407 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411{
7412 garray_T ga;
7413 int first = TRUE;
7414 char_u *tofree;
7415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 return NULL;
7423 ga_init2(&ga, (int)sizeof(char), 80);
7424 ga_append(&ga, '{');
7425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007426 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 --todo;
7432
7433 if (first)
7434 first = FALSE;
7435 else
7436 ga_concat(&ga, (char_u *)", ");
7437
7438 tofree = string_quote(hi->hi_key, FALSE);
7439 if (tofree != NULL)
7440 {
7441 ga_concat(&ga, tofree);
7442 vim_free(tofree);
7443 }
7444 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007445 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 if (s != NULL)
7447 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007449 if (s == NULL)
7450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 if (todo > 0)
7454 {
7455 vim_free(ga.ga_data);
7456 return NULL;
7457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458
7459 ga_append(&ga, '}');
7460 ga_append(&ga, NUL);
7461 return (char_u *)ga.ga_data;
7462}
7463
7464/*
7465 * Allocate a variable for a Dictionary and fill it from "*arg".
7466 * Return OK or FAIL. Returns NOTDONE for {expr}.
7467 */
7468 static int
7469get_dict_tv(arg, rettv, evaluate)
7470 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 int evaluate;
7473{
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 dict_T *d = NULL;
7475 typval_T tvkey;
7476 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007477 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 /*
7483 * First check if it's not a curly-braces thing: {expr}.
7484 * Must do this without evaluating, otherwise a function may be called
7485 * twice. Unfortunately this means we need to call eval1() twice for the
7486 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 if (*start != '}')
7490 {
7491 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7492 return FAIL;
7493 if (*start == '}')
7494 return NOTDONE;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
7497 if (evaluate)
7498 {
7499 d = dict_alloc();
7500 if (d == NULL)
7501 return FAIL;
7502 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tvkey.v_type = VAR_UNKNOWN;
7504 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
7506 *arg = skipwhite(*arg + 1);
7507 while (**arg != '}' && **arg != NUL)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 goto failret;
7511 if (**arg != ':')
7512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007513 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007517 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007519 key = get_tv_string_buf_chk(&tvkey, buf);
7520 if (key == NULL || *key == NUL)
7521 {
7522 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7523 if (key != NULL)
7524 EMSG(_(e_emptykey));
7525 clear_tv(&tvkey);
7526 goto failret;
7527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529
7530 *arg = skipwhite(*arg + 1);
7531 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7532 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007533 if (evaluate)
7534 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 if (evaluate)
7538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 if (item != NULL)
7541 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007542 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 clear_tv(&tv);
7545 goto failret;
7546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 item = dictitem_alloc(key);
7548 clear_tv(&tvkey);
7549 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if (dict_add(d, item) == FAIL)
7554 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 }
7556 }
7557
7558 if (**arg == '}')
7559 break;
7560 if (**arg != ',')
7561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007562 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 goto failret;
7564 }
7565 *arg = skipwhite(*arg + 1);
7566 }
7567
7568 if (**arg != '}')
7569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007570 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571failret:
7572 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007573 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 return FAIL;
7575 }
7576
7577 *arg = skipwhite(*arg + 1);
7578 if (evaluate)
7579 {
7580 rettv->v_type = VAR_DICT;
7581 rettv->vval.v_dict = d;
7582 ++d->dv_refcount;
7583 }
7584
7585 return OK;
7586}
7587
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 * Return a string with the string representation of a variable.
7590 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007594 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 */
7596 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 static int recurse = 0;
7604 char_u *r = NULL;
7605
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
7610 return NULL;
7611 }
7612 ++recurse;
7613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 switch (tv->v_type)
7615 {
7616 case VAR_FUNC:
7617 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 r = tv->vval.v_string;
7619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 if (tv->vval.v_list == NULL)
7623 {
7624 *tofree = NULL;
7625 r = NULL;
7626 }
7627 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7628 {
7629 *tofree = NULL;
7630 r = (char_u *)"[...]";
7631 }
7632 else
7633 {
7634 tv->vval.v_list->lv_copyID = copyID;
7635 *tofree = list2string(tv, copyID);
7636 r = *tofree;
7637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007639
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641 if (tv->vval.v_dict == NULL)
7642 {
7643 *tofree = NULL;
7644 r = NULL;
7645 }
7646 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7647 {
7648 *tofree = NULL;
7649 r = (char_u *)"{...}";
7650 }
7651 else
7652 {
7653 tv->vval.v_dict->dv_copyID = copyID;
7654 *tofree = dict2string(tv, copyID);
7655 r = *tofree;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 case VAR_STRING:
7660 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 *tofree = NULL;
7662 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007665#ifdef FEAT_FLOAT
7666 case VAR_FLOAT:
7667 *tofree = NULL;
7668 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7669 r = numbuf;
7670 break;
7671#endif
7672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677
7678 --recurse;
7679 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680}
7681
7682/*
7683 * Return a string with the string representation of a variable.
7684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7685 * "numbuf" is used for a number.
7686 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007687 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 */
7689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 char_u **tofree;
7693 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695{
7696 switch (tv->v_type)
7697 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 case VAR_FUNC:
7699 *tofree = string_quote(tv->vval.v_string, TRUE);
7700 return *tofree;
7701 case VAR_STRING:
7702 *tofree = string_quote(tv->vval.v_string, FALSE);
7703 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#ifdef FEAT_FLOAT
7705 case VAR_FLOAT:
7706 *tofree = NULL;
7707 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7708 return numbuf;
7709#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007714 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007717 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718}
7719
7720/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 * Return string "str" in ' quotes, doubling ' characters.
7722 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 */
7725 static char_u *
7726string_quote(str, function)
7727 char_u *str;
7728 int function;
7729{
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 char_u *p, *r, *s;
7732
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 len = (function ? 13 : 3);
7734 if (str != NULL)
7735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007736 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 for (p = str; *p != NUL; mb_ptr_adv(p))
7738 if (*p == '\'')
7739 ++len;
7740 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 s = r = alloc(len);
7742 if (r != NULL)
7743 {
7744 if (function)
7745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 r += 10;
7748 }
7749 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 if (str != NULL)
7752 for (p = str; *p != NUL; )
7753 {
7754 if (*p == '\'')
7755 *r++ = '\'';
7756 MB_COPY_CHAR(p, r);
7757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 if (function)
7760 *r++ = ')';
7761 *r++ = NUL;
7762 }
7763 return s;
7764}
7765
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766#ifdef FEAT_FLOAT
7767/*
7768 * Convert the string "text" to a floating point number.
7769 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7770 * this always uses a decimal point.
7771 * Returns the length of the text that was consumed.
7772 */
7773 static int
7774string2float(text, value)
7775 char_u *text;
7776 float_T *value; /* result stored here */
7777{
7778 char *s = (char *)text;
7779 float_T f;
7780
7781 f = strtod(s, &s);
7782 *value = f;
7783 return (int)((char_u *)s - text);
7784}
7785#endif
7786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * Get the value of an environment variable.
7789 * "arg" is pointing to the '$'. It is advanced to after the name.
7790 * If the environment variable was not set, silently assume it is empty.
7791 * Always return OK.
7792 */
7793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 int evaluate;
7798{
7799 char_u *string = NULL;
7800 int len;
7801 int cc;
7802 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 ++*arg;
7806 name = *arg;
7807 len = get_env_len(arg);
7808 if (evaluate)
7809 {
7810 if (len != 0)
7811 {
7812 cc = name[len];
7813 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007814 /* first try vim_getenv(), fast for normal environment vars */
7815 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007817 {
7818 if (!mustfree)
7819 string = vim_strsave(string);
7820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
7822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 if (mustfree)
7824 vim_free(string);
7825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /* next try expanding things like $VIM and ${HOME} */
7827 string = expand_env_save(name - 1);
7828 if (string != NULL && *string == '$')
7829 {
7830 vim_free(string);
7831 string = NULL;
7832 }
7833 }
7834 name[len] = cc;
7835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->v_type = VAR_STRING;
7837 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839
7840 return OK;
7841}
7842
7843/*
7844 * Array with names and number of arguments of all internal functions
7845 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7846 */
7847static struct fst
7848{
7849 char *f_name; /* function name */
7850 char f_min_argc; /* minimal number of arguments */
7851 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007853 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854} functions[] =
7855{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007858 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007860 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007861 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"append", 2, 2, f_append},
7863 {"argc", 0, 0, f_argc},
7864 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007865 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007869 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"bufexists", 1, 1, f_bufexists},
7874 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7875 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7876 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7877 {"buflisted", 1, 1, f_buflisted},
7878 {"bufloaded", 1, 1, f_bufloaded},
7879 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007880 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"bufwinnr", 1, 1, f_bufwinnr},
7882 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007883 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007884 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"ceil", 1, 1, f_ceil},
7888#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007889 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007890 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007892 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007894#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007895 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007896 {"complete_add", 1, 1, f_complete_add},
7897 {"complete_check", 0, 0, f_complete_check},
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007907 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007908 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"delete", 1, 1, f_delete},
7910 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007911 {"diff_filler", 1, 1, f_diff_filler},
7912 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007913 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"eventhandler", 0, 0, f_eventhandler},
7917 {"executable", 1, 1, f_executable},
7918 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919#ifdef FEAT_FLOAT
7920 {"exp", 1, 1, f_exp},
7921#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007922 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007923 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007924 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7926 {"filereadable", 1, 1, f_filereadable},
7927 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007929 {"finddir", 1, 3, f_finddir},
7930 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#ifdef FEAT_FLOAT
7932 {"float2nr", 1, 1, f_float2nr},
7933 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007936 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"fnamemodify", 2, 2, f_fnamemodify},
7938 {"foldclosed", 1, 1, f_foldclosed},
7939 {"foldclosedend", 1, 1, f_foldclosedend},
7940 {"foldlevel", 1, 1, f_foldlevel},
7941 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007942 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007945 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007946 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007947 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007948 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getchar", 0, 1, f_getchar},
7950 {"getcharmod", 0, 0, f_getcharmod},
7951 {"getcmdline", 0, 0, f_getcmdline},
7952 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007953 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007955 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007956 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"getfsize", 1, 1, f_getfsize},
7958 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007959 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007961 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007962 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007963 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007964 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007965 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007966 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007968 {"gettabvar", 2, 3, f_gettabvar},
7969 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"getwinposx", 0, 0, f_getwinposx},
7971 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007972 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007973 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007974 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007977 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007978 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7980 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7981 {"histadd", 2, 2, f_histadd},
7982 {"histdel", 1, 2, f_histdel},
7983 {"histget", 1, 2, f_histget},
7984 {"histnr", 1, 1, f_histnr},
7985 {"hlID", 1, 1, f_hlID},
7986 {"hlexists", 1, 1, f_hlexists},
7987 {"hostname", 0, 0, f_hostname},
7988 {"iconv", 3, 3, f_iconv},
7989 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007991 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007993 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"inputrestore", 0, 0, f_inputrestore},
7995 {"inputsave", 0, 0, f_inputsave},
7996 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007998 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008000 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008003 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"libcall", 3, 3, f_libcall},
8007 {"libcallnr", 3, 3, f_libcallnr},
8008 {"line", 1, 1, f_line},
8009 {"line2byte", 1, 1, f_line2byte},
8010 {"lispindent", 1, 1, f_lispindent},
8011 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008013 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 {"log10", 1, 1, f_log10},
8015#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008016#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008017 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008018#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008020 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008021 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008022 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008023 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008024 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008025 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008026 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008027 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008028 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008029 {"max", 1, 1, f_max},
8030 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031#ifdef vim_mkdir
8032 {"mkdir", 1, 3, f_mkdir},
8033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008035#ifdef FEAT_MZSCHEME
8036 {"mzeval", 1, 1, f_mzeval},
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008039 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008040 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008041 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"pow", 2, 2, f_pow},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008046 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008048#ifdef FEAT_PYTHON3
8049 {"py3eval", 1, 1, f_py3eval},
8050#endif
8051#ifdef FEAT_PYTHON
8052 {"pyeval", 1, 1, f_pyeval},
8053#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008055 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008056 {"reltime", 0, 2, f_reltime},
8057 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"remote_expr", 2, 3, f_remote_expr},
8059 {"remote_foreground", 1, 1, f_remote_foreground},
8060 {"remote_peek", 1, 2, f_remote_peek},
8061 {"remote_read", 1, 1, f_remote_read},
8062 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008065 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008067 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"round", 1, 1, f_round},
8070#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008071 {"screenattr", 2, 2, f_screenattr},
8072 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008073 {"screencol", 0, 0, f_screencol},
8074 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008075 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008076 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008077 {"searchpair", 3, 7, f_searchpair},
8078 {"searchpairpos", 3, 7, f_searchpairpos},
8079 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"server2client", 2, 2, f_server2client},
8081 {"serverlist", 0, 0, f_serverlist},
8082 {"setbufvar", 3, 3, f_setbufvar},
8083 {"setcmdpos", 1, 1, f_setcmdpos},
8084 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008085 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008086 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008087 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008088 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008090 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008091 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008093#ifdef FEAT_CRYPT
8094 {"sha256", 1, 1, f_sha256},
8095#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008096 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008097 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008103 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008104 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008105 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008106 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008107 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"sqrt", 1, 1, f_sqrt},
8110 {"str2float", 1, 1, f_str2float},
8111#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008112 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008113 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008114 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#ifdef HAVE_STRFTIME
8116 {"strftime", 1, 2, f_strftime},
8117#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"strlen", 1, 1, f_strlen},
8121 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008122 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"submatch", 1, 1, f_submatch},
8126 {"substitute", 4, 4, f_substitute},
8127 {"synID", 3, 3, f_synID},
8128 {"synIDattr", 2, 3, f_synIDattr},
8129 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008130 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008131 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008132 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008133 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008134 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008135 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008136 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008137 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138#ifdef FEAT_FLOAT
8139 {"tan", 1, 1, f_tan},
8140 {"tanh", 1, 1, f_tanh},
8141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008143 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"tolower", 1, 1, f_tolower},
8145 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008146 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"trunc", 1, 1, f_trunc},
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008151 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008152 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008153 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"virtcol", 1, 1, f_virtcol},
8155 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008156 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"winbufnr", 1, 1, f_winbufnr},
8158 {"wincol", 0, 0, f_wincol},
8159 {"winheight", 1, 1, f_winheight},
8160 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008163 {"winrestview", 1, 1, f_winrestview},
8164 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008166 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168};
8169
8170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8171
8172/*
8173 * Function given to ExpandGeneric() to obtain the list of internal
8174 * or user defined function names.
8175 */
8176 char_u *
8177get_function_name(xp, idx)
8178 expand_T *xp;
8179 int idx;
8180{
8181 static int intidx = -1;
8182 char_u *name;
8183
8184 if (idx == 0)
8185 intidx = -1;
8186 if (intidx < 0)
8187 {
8188 name = get_user_func_name(xp, idx);
8189 if (name != NULL)
8190 return name;
8191 }
8192 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8193 {
8194 STRCPY(IObuff, functions[intidx].f_name);
8195 STRCAT(IObuff, "(");
8196 if (functions[intidx].f_max_argc == 0)
8197 STRCAT(IObuff, ")");
8198 return IObuff;
8199 }
8200
8201 return NULL;
8202}
8203
8204/*
8205 * Function given to ExpandGeneric() to obtain the list of internal or
8206 * user defined variable or function names.
8207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *
8209get_expr_name(xp, idx)
8210 expand_T *xp;
8211 int idx;
8212{
8213 static int intidx = -1;
8214 char_u *name;
8215
8216 if (idx == 0)
8217 intidx = -1;
8218 if (intidx < 0)
8219 {
8220 name = get_function_name(xp, idx);
8221 if (name != NULL)
8222 return name;
8223 }
8224 return get_user_var_name(xp, ++intidx);
8225}
8226
8227#endif /* FEAT_CMDL_COMPL */
8228
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008229#if defined(EBCDIC) || defined(PROTO)
8230/*
8231 * Compare struct fst by function name.
8232 */
8233 static int
8234compare_func_name(s1, s2)
8235 const void *s1;
8236 const void *s2;
8237{
8238 struct fst *p1 = (struct fst *)s1;
8239 struct fst *p2 = (struct fst *)s2;
8240
8241 return STRCMP(p1->f_name, p2->f_name);
8242}
8243
8244/*
8245 * Sort the function table by function name.
8246 * The sorting of the table above is ASCII dependant.
8247 * On machines using EBCDIC we have to sort it.
8248 */
8249 static void
8250sortFunctions()
8251{
8252 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8253
8254 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8255}
8256#endif
8257
8258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259/*
8260 * Find internal function in table above.
8261 * Return index, or -1 if not found
8262 */
8263 static int
8264find_internal_func(name)
8265 char_u *name; /* name of the function */
8266{
8267 int first = 0;
8268 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269 int cmp;
8270 int x;
8271
8272 /*
8273 * Find the function name in the table. Binary search.
8274 */
8275 while (first <= last)
8276 {
8277 x = first + ((unsigned)(last - first) >> 1);
8278 cmp = STRCMP(name, functions[x].f_name);
8279 if (cmp < 0)
8280 last = x - 1;
8281 else if (cmp > 0)
8282 first = x + 1;
8283 else
8284 return x;
8285 }
8286 return -1;
8287}
8288
8289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8291 * name it contains, otherwise return "name".
8292 */
8293 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008294deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 char_u *name;
8296 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008297 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298{
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 int cc;
8301
8302 cc = name[*lenp];
8303 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008304 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008307 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 {
8310 *lenp = 0;
8311 return (char_u *)""; /* just in case */
8312 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008313 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 }
8316
8317 return name;
8318}
8319
8320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 * Allocate a variable for the result of a function.
8322 * Return OK or FAIL.
8323 */
8324 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8326 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 char_u *name; /* name of the function */
8328 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 char_u **arg; /* argument, pointing to the '(' */
8331 linenr_T firstline; /* first line of range */
8332 linenr_T lastline; /* last line of range */
8333 int *doesrange; /* return: function handled range */
8334 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 char_u *argp;
8338 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008339 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 int argcount = 0; /* number of arguments found */
8341
8342 /*
8343 * Get the arguments.
8344 */
8345 argp = *arg;
8346 while (argcount < MAX_FUNC_ARGS)
8347 {
8348 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8349 if (*argp == ')' || *argp == ',' || *argp == NUL)
8350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8352 {
8353 ret = FAIL;
8354 break;
8355 }
8356 ++argcount;
8357 if (*argp != ',')
8358 break;
8359 }
8360 if (*argp == ')')
8361 ++argp;
8362 else
8363 ret = FAIL;
8364
8365 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008367 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 {
8370 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008371 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008373 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
8376 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 *arg = skipwhite(argp);
8380 return ret;
8381}
8382
8383
8384/*
8385 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008386 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008387 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 */
8389 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008390call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008392 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008394 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008396 typval_T *argvars; /* vars for arguments, must have "argcount"
8397 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 linenr_T firstline; /* first line of range */
8399 linenr_T lastline; /* last line of range */
8400 int *doesrange; /* return: function handled range */
8401 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405#define ERROR_UNKNOWN 0
8406#define ERROR_TOOMANY 1
8407#define ERROR_TOOFEW 2
8408#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409#define ERROR_DICT 4
8410#define ERROR_NONE 5
8411#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 int error = ERROR_NONE;
8413 int i;
8414 int llen;
8415 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#define FLEN_FIXED 40
8417 char_u fname_buf[FLEN_FIXED + 1];
8418 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008419 char_u *name;
8420
8421 /* Make a copy of the name, if it comes from a funcref variable it could
8422 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008423 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008424 if (name == NULL)
8425 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
8427 /*
8428 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8429 * Change <SNR>123_name() to K_SNR 123_name().
8430 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 llen = eval_fname_script(name);
8433 if (llen > 0)
8434 {
8435 fname_buf[0] = K_SPECIAL;
8436 fname_buf[1] = KS_EXTRA;
8437 fname_buf[2] = (int)KE_SNR;
8438 i = 3;
8439 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8440 {
8441 if (current_SID <= 0)
8442 error = ERROR_SCRIPT;
8443 else
8444 {
8445 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8446 i = (int)STRLEN(fname_buf);
8447 }
8448 }
8449 if (i + STRLEN(name + llen) < FLEN_FIXED)
8450 {
8451 STRCPY(fname_buf + i, name + llen);
8452 fname = fname_buf;
8453 }
8454 else
8455 {
8456 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8457 if (fname == NULL)
8458 error = ERROR_OTHER;
8459 else
8460 {
8461 mch_memmove(fname, fname_buf, (size_t)i);
8462 STRCPY(fname + i, name + llen);
8463 }
8464 }
8465 }
8466 else
8467 fname = name;
8468
8469 *doesrange = FALSE;
8470
8471
8472 /* execute the function if no errors detected and executing */
8473 if (evaluate && error == ERROR_NONE)
8474 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008475 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8476 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_UNKNOWN;
8478
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008479 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 /*
8482 * User defined function.
8483 */
8484 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 /* Trigger FuncUndefined event, may load the function. */
8488 if (fp == NULL
8489 && apply_autocmds(EVENT_FUNCUNDEFINED,
8490 fname, fname, TRUE, NULL)
8491 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 fp = find_func(fname);
8495 }
8496#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008497 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008498 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008499 {
8500 /* loaded a package, search for the function again */
8501 fp = find_func(fname);
8502 }
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (fp != NULL)
8505 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008506 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008508 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008510 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008512 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 else
8515 {
8516 /*
8517 * Call the user function.
8518 * Save and restore search patterns, script variables and
8519 * redo buffer.
8520 */
8521 save_search_patterns();
8522 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008523 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008524 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008526 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8527 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8528 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008529 /* Function was unreferenced while being used, free it
8530 * now. */
8531 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 restoreRedobuff();
8533 restore_search_patterns();
8534 error = ERROR_NONE;
8535 }
8536 }
8537 }
8538 else
8539 {
8540 /*
8541 * Find the function name in the table, call its implementation.
8542 */
8543 i = find_internal_func(fname);
8544 if (i >= 0)
8545 {
8546 if (argcount < functions[i].f_min_argc)
8547 error = ERROR_TOOFEW;
8548 else if (argcount > functions[i].f_max_argc)
8549 error = ERROR_TOOMANY;
8550 else
8551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_NONE;
8555 }
8556 }
8557 }
8558 /*
8559 * The function call (or "FuncUndefined" autocommand sequence) might
8560 * have been aborted by an error, an interrupt, or an explicitly thrown
8561 * exception that has not been caught so far. This situation can be
8562 * tested for by calling aborting(). For an error in an internal
8563 * function or for the "E132" error in call_user_func(), however, the
8564 * throw point at which the "force_abort" flag (temporarily reset by
8565 * emsg()) is normally updated has not been reached yet. We need to
8566 * update that flag first to make aborting() reliable.
8567 */
8568 update_force_abort();
8569 }
8570 if (error == ERROR_NONE)
8571 ret = OK;
8572
8573 /*
8574 * Report an error unless the argument evaluation or function call has been
8575 * cancelled due to an aborting error, an interrupt, or an exception.
8576 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 if (!aborting())
8578 {
8579 switch (error)
8580 {
8581 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008582 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008583 break;
8584 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008586 break;
8587 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008588 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008589 name);
8590 break;
8591 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008592 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008593 name);
8594 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 name);
8598 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 }
8600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (fname != name && fname != fname_buf)
8603 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008604 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 return ret;
8607}
8608
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008609/*
8610 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008612 */
8613 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008614emsg_funcname(ermsg, name)
8615 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008616 char_u *name;
8617{
8618 char_u *p;
8619
8620 if (*name == K_SPECIAL)
8621 p = concat_str((char_u *)"<SNR>", name + 3);
8622 else
8623 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008624 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 if (p != name)
8626 vim_free(p);
8627}
8628
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008629/*
8630 * Return TRUE for a non-zero Number and a non-empty String.
8631 */
8632 static int
8633non_zero_arg(argvars)
8634 typval_T *argvars;
8635{
8636 return ((argvars[0].v_type == VAR_NUMBER
8637 && argvars[0].vval.v_number != 0)
8638 || (argvars[0].v_type == VAR_STRING
8639 && argvars[0].vval.v_string != NULL
8640 && *argvars[0].vval.v_string != NUL));
8641}
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643/*********************************************
8644 * Implementation of the built-in functions
8645 */
8646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008647#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008648static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8649
8650/*
8651 * Get the float value of "argvars[0]" into "f".
8652 * Returns FAIL when the argument is not a Number or Float.
8653 */
8654 static int
8655get_float_arg(argvars, f)
8656 typval_T *argvars;
8657 float_T *f;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 *f = argvars[0].vval.v_float;
8662 return OK;
8663 }
8664 if (argvars[0].v_type == VAR_NUMBER)
8665 {
8666 *f = (float_T)argvars[0].vval.v_number;
8667 return OK;
8668 }
8669 EMSG(_("E808: Number or Float required"));
8670 return FAIL;
8671}
8672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673/*
8674 * "abs(expr)" function
8675 */
8676 static void
8677f_abs(argvars, rettv)
8678 typval_T *argvars;
8679 typval_T *rettv;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 rettv->v_type = VAR_FLOAT;
8684 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8685 }
8686 else
8687 {
8688 varnumber_T n;
8689 int error = FALSE;
8690
8691 n = get_tv_number_chk(&argvars[0], &error);
8692 if (error)
8693 rettv->vval.v_number = -1;
8694 else if (n > 0)
8695 rettv->vval.v_number = n;
8696 else
8697 rettv->vval.v_number = -n;
8698 }
8699}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008700
8701/*
8702 * "acos()" function
8703 */
8704 static void
8705f_acos(argvars, rettv)
8706 typval_T *argvars;
8707 typval_T *rettv;
8708{
8709 float_T f;
8710
8711 rettv->v_type = VAR_FLOAT;
8712 if (get_float_arg(argvars, &f) == OK)
8713 rettv->vval.v_float = acos(f);
8714 else
8715 rettv->vval.v_float = 0.0;
8716}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717#endif
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 */
8722 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008723f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008730 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008732 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008733 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008734 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 }
8737 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 EMSG(_(e_listreq));
8739}
8740
8741/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008742 * "and(expr, expr)" function
8743 */
8744 static void
8745f_and(argvars, rettv)
8746 typval_T *argvars;
8747 typval_T *rettv;
8748{
8749 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8750 & get_tv_number_chk(&argvars[1], NULL);
8751}
8752
8753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 * "append(lnum, string/list)" function
8755 */
8756 static void
8757f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760{
8761 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008762 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 list_T *l = NULL;
8764 listitem_T *li = NULL;
8765 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 long added = 0;
8767
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008768 /* When coming here from Insert mode, sync undo, so that this can be
8769 * undone separately from what was previously inserted. */
8770 if (u_sync_once == 2)
8771 {
8772 u_sync_once = 1; /* notify that u_sync() was called */
8773 u_sync(TRUE);
8774 }
8775
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 lnum = get_tv_lnum(argvars);
8777 if (lnum >= 0
8778 && lnum <= curbuf->b_ml.ml_line_count
8779 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008782 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008783 l = argvars[1].vval.v_list;
8784 if (l == NULL)
8785 return;
8786 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 for (;;)
8789 {
8790 if (l == NULL)
8791 tv = &argvars[1]; /* append a string */
8792 else if (li == NULL)
8793 break; /* end of list */
8794 else
8795 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 line = get_tv_string_chk(tv);
8797 if (line == NULL) /* type error */
8798 {
8799 rettv->vval.v_number = 1; /* Failed */
8800 break;
8801 }
8802 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 ++added;
8804 if (l == NULL)
8805 break;
8806 li = li->li_next;
8807 }
8808
8809 appended_lines_mark(lnum, added);
8810 if (curwin->w_cursor.lnum > lnum)
8811 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 else
8814 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "argc()" function
8819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "argidx()" function
8830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008833 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argv(nr)" function
8841 */
8842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 typval_T *argvars;
8845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 int idx;
8848
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008849 if (argvars[0].v_type != VAR_UNKNOWN)
8850 {
8851 idx = get_tv_number_chk(&argvars[0], NULL);
8852 if (idx >= 0 && idx < ARGCOUNT)
8853 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8854 else
8855 rettv->vval.v_string = NULL;
8856 rettv->v_type = VAR_STRING;
8857 }
8858 else if (rettv_list_alloc(rettv) == OK)
8859 for (idx = 0; idx < ARGCOUNT; ++idx)
8860 list_append_string(rettv->vval.v_list,
8861 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008865/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008866 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008867 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868 static void
8869f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008871 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008873 float_T f;
8874
8875 rettv->v_type = VAR_FLOAT;
8876 if (get_float_arg(argvars, &f) == OK)
8877 rettv->vval.v_float = asin(f);
8878 else
8879 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880}
8881
8882/*
8883 * "atan()" function
8884 */
8885 static void
8886f_atan(argvars, rettv)
8887 typval_T *argvars;
8888 typval_T *rettv;
8889{
8890 float_T f;
8891
8892 rettv->v_type = VAR_FLOAT;
8893 if (get_float_arg(argvars, &f) == OK)
8894 rettv->vval.v_float = atan(f);
8895 else
8896 rettv->vval.v_float = 0.0;
8897}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008898
8899/*
8900 * "atan2()" function
8901 */
8902 static void
8903f_atan2(argvars, rettv)
8904 typval_T *argvars;
8905 typval_T *rettv;
8906{
8907 float_T fx, fy;
8908
8909 rettv->v_type = VAR_FLOAT;
8910 if (get_float_arg(argvars, &fx) == OK
8911 && get_float_arg(&argvars[1], &fy) == OK)
8912 rettv->vval.v_float = atan2(fx, fy);
8913 else
8914 rettv->vval.v_float = 0.0;
8915}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008916#endif
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918/*
8919 * "browse(save, title, initdir, default)" function
8920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926#ifdef FEAT_BROWSE
8927 int save;
8928 char_u *title;
8929 char_u *initdir;
8930 char_u *defname;
8931 char_u buf[NUMBUFLEN];
8932 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 save = get_tv_number_chk(&argvars[0], &error);
8936 title = get_tv_string_chk(&argvars[1]);
8937 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8938 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 if (error || title == NULL || initdir == NULL || defname == NULL)
8941 rettv->vval.v_string = NULL;
8942 else
8943 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008944 do_browse(save ? BROWSE_SAVE : 0,
8945 title, defname, NULL, initdir, NULL, curbuf);
8946#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950}
8951
8952/*
8953 * "browsedir(title, initdir)" function
8954 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008959{
8960#ifdef FEAT_BROWSE
8961 char_u *title;
8962 char_u *initdir;
8963 char_u buf[NUMBUFLEN];
8964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 title = get_tv_string_chk(&argvars[0]);
8966 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 if (title == NULL || initdir == NULL)
8969 rettv->vval.v_string = NULL;
8970 else
8971 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977}
8978
Bram Moolenaar33570922005-01-25 22:26:29 +00008979static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981/*
8982 * Find a buffer by number or exact name.
8983 */
8984 static buf_T *
8985find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 if (avar->v_type == VAR_NUMBER)
8991 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008992 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008995 if (buf == NULL)
8996 {
8997 /* No full path name match, try a match with a URL or a "nofile"
8998 * buffer, these don't use the full path. */
8999 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9000 if (buf->b_fname != NULL
9001 && (path_with_url(buf->b_fname)
9002#ifdef FEAT_QUICKFIX
9003 || bt_nofile(buf)
9004#endif
9005 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009007 break;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
9010 return buf;
9011}
9012
9013/*
9014 * "bufexists(expr)" function
9015 */
9016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "buflisted(expr)" function
9026 */
9027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 buf_T *buf;
9033
9034 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * "bufloaded(expr)" function
9040 */
9041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 typval_T *argvars;
9044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 buf_T *buf;
9047
9048 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009052static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054/*
9055 * Get buffer by number or pattern.
9056 */
9057 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009058get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 int save_magic;
9064 char_u *save_cpo;
9065 buf_T *buf;
9066
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 if (tv->v_type == VAR_NUMBER)
9068 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009069 if (tv->v_type != VAR_STRING)
9070 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (name == NULL || *name == NUL)
9072 return curbuf;
9073 if (name[0] == '$' && name[1] == NUL)
9074 return lastbuf;
9075
9076 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9077 save_magic = p_magic;
9078 p_magic = TRUE;
9079 save_cpo = p_cpo;
9080 p_cpo = (char_u *)"";
9081
9082 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009083 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
9085 p_magic = save_magic;
9086 p_cpo = save_cpo;
9087
9088 /* If not found, try expanding the name, like done for bufexists(). */
9089 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092 return buf;
9093}
9094
9095/*
9096 * "bufname(expr)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103 buf_T *buf;
9104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 --emsg_off;
9114}
9115
9116/*
9117 * "bufnr(expr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009125 int error = FALSE;
9126 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009130 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009131 --emsg_off;
9132
9133 /* If the buffer isn't found and the second argument is not zero create a
9134 * new buffer. */
9135 if (buf == NULL
9136 && argvars[1].v_type != VAR_UNKNOWN
9137 && get_tv_number_chk(&argvars[1], &error) != 0
9138 && !error
9139 && (name = get_tv_string_chk(&argvars[0])) != NULL
9140 && !error)
9141 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9142
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * "bufwinnr(nr)" function
9151 */
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifdef FEAT_WINDOWS
9158 win_T *wp;
9159 int winnr = 0;
9160#endif
9161 buf_T *buf;
9162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009165 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#ifdef FEAT_WINDOWS
9167 for (wp = firstwin; wp; wp = wp->w_next)
9168 {
9169 ++winnr;
9170 if (wp->w_buffer == buf)
9171 break;
9172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176#endif
9177 --emsg_off;
9178}
9179
9180/*
9181 * "byte2line(byte)" function
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#else
9191 long boff = 0;
9192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 (linenr_T)0, &boff);
9199#endif
9200}
9201
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009202 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009203byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009206 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009207{
9208#ifdef FEAT_MBYTE
9209 char_u *t;
9210#endif
9211 char_u *str;
9212 long idx;
9213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 str = get_tv_string_chk(&argvars[0]);
9215 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009218 return;
9219
9220#ifdef FEAT_MBYTE
9221 t = str;
9222 for ( ; idx > 0; idx--)
9223 {
9224 if (*t == NUL) /* EOL reached */
9225 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009226 if (enc_utf8 && comp)
9227 t += utf_ptr2len(t);
9228 else
9229 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009230 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009231 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009232#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009235#endif
9236}
9237
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009238/*
9239 * "byteidx()" function
9240 */
9241 static void
9242f_byteidx(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
9246 byteidx(argvars, rettv, FALSE);
9247}
9248
9249/*
9250 * "byteidxcomp()" function
9251 */
9252 static void
9253f_byteidxcomp(argvars, rettv)
9254 typval_T *argvars;
9255 typval_T *rettv;
9256{
9257 byteidx(argvars, rettv, TRUE);
9258}
9259
Bram Moolenaardb913952012-06-29 12:54:53 +02009260 int
9261func_call(name, args, selfdict, rettv)
9262 char_u *name;
9263 typval_T *args;
9264 dict_T *selfdict;
9265 typval_T *rettv;
9266{
9267 listitem_T *item;
9268 typval_T argv[MAX_FUNC_ARGS + 1];
9269 int argc = 0;
9270 int dummy;
9271 int r = 0;
9272
9273 for (item = args->vval.v_list->lv_first; item != NULL;
9274 item = item->li_next)
9275 {
9276 if (argc == MAX_FUNC_ARGS)
9277 {
9278 EMSG(_("E699: Too many arguments"));
9279 break;
9280 }
9281 /* Make a copy of each argument. This is needed to be able to set
9282 * v_lock to VAR_FIXED in the copy without changing the original list.
9283 */
9284 copy_tv(&item->li_tv, &argv[argc++]);
9285 }
9286
9287 if (item == NULL)
9288 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9289 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9290 &dummy, TRUE, selfdict);
9291
9292 /* Free the arguments. */
9293 while (argc > 0)
9294 clear_tv(&argv[--argc]);
9295
9296 return r;
9297}
9298
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009300 * "call(func, arglist)" function
9301 */
9302 static void
9303f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009306{
9307 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009309
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009310 if (argvars[1].v_type != VAR_LIST)
9311 {
9312 EMSG(_(e_listreq));
9313 return;
9314 }
9315 if (argvars[1].vval.v_list == NULL)
9316 return;
9317
9318 if (argvars[0].v_type == VAR_FUNC)
9319 func = argvars[0].vval.v_string;
9320 else
9321 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 if (*func == NUL)
9323 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009324
Bram Moolenaare9a41262005-01-15 22:18:47 +00009325 if (argvars[2].v_type != VAR_UNKNOWN)
9326 {
9327 if (argvars[2].v_type != VAR_DICT)
9328 {
9329 EMSG(_(e_dictreq));
9330 return;
9331 }
9332 selfdict = argvars[2].vval.v_dict;
9333 }
9334
Bram Moolenaardb913952012-06-29 12:54:53 +02009335 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009336}
9337
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#ifdef FEAT_FLOAT
9339/*
9340 * "ceil({float})" function
9341 */
9342 static void
9343f_ceil(argvars, rettv)
9344 typval_T *argvars;
9345 typval_T *rettv;
9346{
9347 float_T f;
9348
9349 rettv->v_type = VAR_FLOAT;
9350 if (get_float_arg(argvars, &f) == OK)
9351 rettv->vval.v_float = ceil(f);
9352 else
9353 rettv->vval.v_float = 0.0;
9354}
9355#endif
9356
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009357/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009358 * "changenr()" function
9359 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009360 static void
9361f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009362 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009363 typval_T *rettv;
9364{
9365 rettv->vval.v_number = curbuf->b_u_seq_cur;
9366}
9367
9368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * "char2nr(string)" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376#ifdef FEAT_MBYTE
9377 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009378 {
9379 int utf8 = 0;
9380
9381 if (argvars[1].v_type != VAR_UNKNOWN)
9382 utf8 = get_tv_number_chk(&argvars[1], NULL);
9383
9384 if (utf8)
9385 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9386 else
9387 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 else
9390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
9395 * "cindent(lnum)" function
9396 */
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402#ifdef FEAT_CINDENT
9403 pos_T pos;
9404 linenr_T lnum;
9405
9406 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9409 {
9410 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 curwin->w_cursor = pos;
9413 }
9414 else
9415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417}
9418
9419/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009420 * "clearmatches()" function
9421 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009422 static void
9423f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009424 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009425 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009426{
9427#ifdef FEAT_SEARCH_EXTRA
9428 clear_matches(curwin);
9429#endif
9430}
9431
9432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 * "col(string)" function
9434 */
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 colnr_T col = 0;
9441 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009442 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009444 fp = var2fpos(&argvars[0], FALSE, &fnum);
9445 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 if (fp->col == MAXCOL)
9448 {
9449 /* '> can be MAXCOL, get the length of the line then */
9450 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009451 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453 col = MAXCOL;
9454 }
9455 else
9456 {
9457 col = fp->col + 1;
9458#ifdef FEAT_VIRTUALEDIT
9459 /* col(".") when the cursor is on the NUL at the end of the line
9460 * because of "coladd" can be seen as an extra column. */
9461 if (virtual_active() && fp == &curwin->w_cursor)
9462 {
9463 char_u *p = ml_get_cursor();
9464
9465 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9466 curwin->w_virtcol - curwin->w_cursor.coladd))
9467 {
9468# ifdef FEAT_MBYTE
9469 int l;
9470
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009471 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 col += l;
9473# else
9474 if (*p != NUL && p[1] == NUL)
9475 ++col;
9476# endif
9477 }
9478 }
9479#endif
9480 }
9481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
Bram Moolenaar572cb562005-08-05 21:35:02 +00009485#if defined(FEAT_INS_EXPAND)
9486/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009487 * "complete()" function
9488 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009489 static void
9490f_complete(argvars, rettv)
9491 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009493{
9494 int startcol;
9495
9496 if ((State & INSERT) == 0)
9497 {
9498 EMSG(_("E785: complete() can only be used in Insert mode"));
9499 return;
9500 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009501
9502 /* Check for undo allowed here, because if something was already inserted
9503 * the line was already saved for undo and this check isn't done. */
9504 if (!undo_allowed())
9505 return;
9506
Bram Moolenaarade00832006-03-10 21:46:58 +00009507 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9508 {
9509 EMSG(_(e_invarg));
9510 return;
9511 }
9512
9513 startcol = get_tv_number_chk(&argvars[0], NULL);
9514 if (startcol <= 0)
9515 return;
9516
9517 set_completion(startcol - 1, argvars[1].vval.v_list);
9518}
9519
9520/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009521 * "complete_add()" function
9522 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009523 static void
9524f_complete_add(argvars, rettv)
9525 typval_T *argvars;
9526 typval_T *rettv;
9527{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009528 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529}
9530
9531/*
9532 * "complete_check()" function
9533 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009534 static void
9535f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009536 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009537 typval_T *rettv;
9538{
9539 int saved = RedrawingDisabled;
9540
9541 RedrawingDisabled = 0;
9542 ins_compl_check_keys(0);
9543 rettv->vval.v_number = compl_interrupted;
9544 RedrawingDisabled = saved;
9545}
9546#endif
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548/*
9549 * "confirm(message, buttons[, default [, type]])" function
9550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009553 typval_T *argvars UNUSED;
9554 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9557 char_u *message;
9558 char_u *buttons = NULL;
9559 char_u buf[NUMBUFLEN];
9560 char_u buf2[NUMBUFLEN];
9561 int def = 1;
9562 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 char_u *typestr;
9564 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 message = get_tv_string_chk(&argvars[0]);
9567 if (message == NULL)
9568 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9572 if (buttons == NULL)
9573 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9580 if (typestr == NULL)
9581 error = TRUE;
9582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 switch (TOUPPER_ASC(*typestr))
9585 {
9586 case 'E': type = VIM_ERROR; break;
9587 case 'Q': type = VIM_QUESTION; break;
9588 case 'I': type = VIM_INFO; break;
9589 case 'W': type = VIM_WARNING; break;
9590 case 'G': type = VIM_GENERIC; break;
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593 }
9594 }
9595 }
9596
9597 if (buttons == NULL || *buttons == NUL)
9598 buttons = (char_u *)_("&Ok");
9599
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009600 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009602 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#endif
9604}
9605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009606/*
9607 * "copy()" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009614 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009617#ifdef FEAT_FLOAT
9618/*
9619 * "cos()" function
9620 */
9621 static void
9622f_cos(argvars, rettv)
9623 typval_T *argvars;
9624 typval_T *rettv;
9625{
9626 float_T f;
9627
9628 rettv->v_type = VAR_FLOAT;
9629 if (get_float_arg(argvars, &f) == OK)
9630 rettv->vval.v_float = cos(f);
9631 else
9632 rettv->vval.v_float = 0.0;
9633}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009634
9635/*
9636 * "cosh()" function
9637 */
9638 static void
9639f_cosh(argvars, rettv)
9640 typval_T *argvars;
9641 typval_T *rettv;
9642{
9643 float_T f;
9644
9645 rettv->v_type = VAR_FLOAT;
9646 if (get_float_arg(argvars, &f) == OK)
9647 rettv->vval.v_float = cosh(f);
9648 else
9649 rettv->vval.v_float = 0.0;
9650}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009651#endif
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009654 * "count()" function
9655 */
9656 static void
9657f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 typval_T *argvars;
9659 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009661 long n = 0;
9662 int ic = FALSE;
9663
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 listitem_T *li;
9667 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009669
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 if ((l = argvars[0].vval.v_list) != NULL)
9671 {
9672 li = l->lv_first;
9673 if (argvars[2].v_type != VAR_UNKNOWN)
9674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 int error = FALSE;
9676
9677 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if (argvars[3].v_type != VAR_UNKNOWN)
9679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 idx = get_tv_number_chk(&argvars[3], &error);
9681 if (!error)
9682 {
9683 li = list_find(l, idx);
9684 if (li == NULL)
9685 EMSGN(_(e_listidx), idx);
9686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 if (error)
9689 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 }
9691
9692 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009693 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009694 ++n;
9695 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 else if (argvars[0].v_type == VAR_DICT)
9698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009699 int todo;
9700 dict_T *d;
9701 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009703 if ((d = argvars[0].vval.v_dict) != NULL)
9704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 int error = FALSE;
9706
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 if (argvars[2].v_type != VAR_UNKNOWN)
9708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 if (argvars[3].v_type != VAR_UNKNOWN)
9711 EMSG(_(e_invarg));
9712 }
9713
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009714 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 {
9717 if (!HASHITEM_EMPTY(hi))
9718 {
9719 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009720 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009721 ++n;
9722 }
9723 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724 }
9725 }
9726 else
9727 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 rettv->vval.v_number = n;
9729}
9730
9731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9733 *
9734 * Checks the existence of a cscope connection.
9735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009738 typval_T *argvars UNUSED;
9739 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_CSCOPE
9742 int num = 0;
9743 char_u *dbpath = NULL;
9744 char_u *prepend = NULL;
9745 char_u buf[NUMBUFLEN];
9746
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009747 if (argvars[0].v_type != VAR_UNKNOWN
9748 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 num = (int)get_tv_number(&argvars[0]);
9751 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009752 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758}
9759
9760/*
9761 * "cursor(lnum, col)" function
9762 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009763 * Moves the cursor to the specified line and column.
9764 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009772#ifdef FEAT_VIRTUALEDIT
9773 long coladd = 0;
9774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009777 if (argvars[1].v_type == VAR_UNKNOWN)
9778 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009779 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009780
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009781 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009782 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009783 line = pos.lnum;
9784 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009786 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009787#endif
9788 }
9789 else
9790 {
9791 line = get_tv_lnum(argvars);
9792 col = get_tv_number_chk(&argvars[1], NULL);
9793#ifdef FEAT_VIRTUALEDIT
9794 if (argvars[2].v_type != VAR_UNKNOWN)
9795 coladd = get_tv_number_chk(&argvars[2], NULL);
9796#endif
9797 }
9798 if (line < 0 || col < 0
9799#ifdef FEAT_VIRTUALEDIT
9800 || coladd < 0
9801#endif
9802 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (line > 0)
9805 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (col > 0)
9807 curwin->w_cursor.col = col - 1;
9808#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009809 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#endif
9811
9812 /* Make sure the cursor is in a valid position. */
9813 check_cursor();
9814#ifdef FEAT_MBYTE
9815 /* Correct cursor for multi-byte character. */
9816 if (has_mbyte)
9817 mb_adjust_cursor();
9818#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009819
9820 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009821 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822}
9823
9824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 */
9827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009829 typval_T *argvars;
9830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009832 int noref = 0;
9833
9834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009836 if (noref < 0 || noref > 1)
9837 EMSG(_(e_invarg));
9838 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009839 {
9840 current_copyID += COPYID_INC;
9841 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843}
9844
9845/*
9846 * "delete()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
9860 * "did_filetype()" function
9861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009864 typval_T *argvars UNUSED;
9865 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869#endif
9870}
9871
9872/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009873 * "diff_filler()" function
9874 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009877 typval_T *argvars UNUSED;
9878 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879{
9880#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882#endif
9883}
9884
9885/*
9886 * "diff_hlID()" function
9887 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009890 typval_T *argvars UNUSED;
9891 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892{
9893#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 static linenr_T prev_lnum = 0;
9896 static int changedtick = 0;
9897 static int fnum = 0;
9898 static int change_start = 0;
9899 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009900 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 int filler_lines;
9902 int col;
9903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (lnum < 0) /* ignore type error in {lnum} arg */
9905 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009906 if (lnum != prev_lnum
9907 || changedtick != curbuf->b_changedtick
9908 || fnum != curbuf->b_fnum)
9909 {
9910 /* New line, buffer, change: need to get the values. */
9911 filler_lines = diff_check(curwin, lnum);
9912 if (filler_lines < 0)
9913 {
9914 if (filler_lines == -1)
9915 {
9916 change_start = MAXCOL;
9917 change_end = -1;
9918 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9919 hlID = HLF_ADD; /* added line */
9920 else
9921 hlID = HLF_CHD; /* changed line */
9922 }
9923 else
9924 hlID = HLF_ADD; /* added line */
9925 }
9926 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009927 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009928 prev_lnum = lnum;
9929 changedtick = curbuf->b_changedtick;
9930 fnum = curbuf->b_fnum;
9931 }
9932
9933 if (hlID == HLF_CHD || hlID == HLF_TXD)
9934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009936 if (col >= change_start && col <= change_end)
9937 hlID = HLF_TXD; /* changed text */
9938 else
9939 hlID = HLF_CHD; /* changed line */
9940 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009941 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009942#endif
9943}
9944
9945/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009946 * "empty({expr})" function
9947 */
9948 static void
9949f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009952{
9953 int n;
9954
9955 switch (argvars[0].v_type)
9956 {
9957 case VAR_STRING:
9958 case VAR_FUNC:
9959 n = argvars[0].vval.v_string == NULL
9960 || *argvars[0].vval.v_string == NUL;
9961 break;
9962 case VAR_NUMBER:
9963 n = argvars[0].vval.v_number == 0;
9964 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009965#ifdef FEAT_FLOAT
9966 case VAR_FLOAT:
9967 n = argvars[0].vval.v_float == 0.0;
9968 break;
9969#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009970 case VAR_LIST:
9971 n = argvars[0].vval.v_list == NULL
9972 || argvars[0].vval.v_list->lv_first == NULL;
9973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 case VAR_DICT:
9975 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009978 default:
9979 EMSG2(_(e_intern2), "f_empty()");
9980 n = 0;
9981 }
9982
9983 rettv->vval.v_number = n;
9984}
9985
9986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 * "escape({string}, {chars})" function
9988 */
9989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *argvars;
9992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 char_u buf[NUMBUFLEN];
9995
Bram Moolenaar758711c2005-02-02 23:11:38 +00009996 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9997 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010002 * "eval()" function
10003 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010004 static void
10005f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 typval_T *argvars;
10007 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008{
10009 char_u *s;
10010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 s = get_tv_string_chk(&argvars[0]);
10012 if (s != NULL)
10013 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10016 {
10017 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010018 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010020 else if (*s != NUL)
10021 EMSG(_(e_trailing));
10022}
10023
10024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 * "eventhandler()" function
10026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "executable()" function
10037 */
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
10046/*
10047 * "exists()" function
10048 */
10049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010051 typval_T *argvars;
10052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 char_u *p;
10055 char_u *name;
10056 int n = FALSE;
10057 int len = 0;
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (*p == '$') /* environment variable */
10061 {
10062 /* first try "normal" environment variables (fast) */
10063 if (mch_getenv(p + 1) != NULL)
10064 n = TRUE;
10065 else
10066 {
10067 /* try expanding things like $VIM and ${HOME} */
10068 p = expand_env_save(p);
10069 if (p != NULL && *p != '$')
10070 n = TRUE;
10071 vim_free(p);
10072 }
10073 }
10074 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010077 if (*skipwhite(p) != NUL)
10078 n = FALSE; /* trailing garbage */
10079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 else if (*p == '*') /* internal or user defined function */
10081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010082 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 else if (*p == ':')
10085 {
10086 n = cmd_exists(p + 1);
10087 }
10088 else if (*p == '#')
10089 {
10090#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010091 if (p[1] == '#')
10092 n = autocmd_supported(p + 2);
10093 else
10094 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#endif
10096 }
10097 else /* internal variable */
10098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010099 char_u *tofree;
10100 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010102 /* get_name_len() takes care of expanding curly braces */
10103 name = p;
10104 len = get_name_len(&p, &tofree, TRUE, FALSE);
10105 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010107 if (tofree != NULL)
10108 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010109 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010110 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010112 /* handle d.key, l[idx], f(expr) */
10113 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10114 if (n)
10115 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 }
10117 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010118 if (*p != NUL)
10119 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010121 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125}
10126
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010127#ifdef FEAT_FLOAT
10128/*
10129 * "exp()" function
10130 */
10131 static void
10132f_exp(argvars, rettv)
10133 typval_T *argvars;
10134 typval_T *rettv;
10135{
10136 float_T f;
10137
10138 rettv->v_type = VAR_FLOAT;
10139 if (get_float_arg(argvars, &f) == OK)
10140 rettv->vval.v_float = exp(f);
10141 else
10142 rettv->vval.v_float = 0.0;
10143}
10144#endif
10145
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146/*
10147 * "expand()" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 char_u *s;
10155 int len;
10156 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010157 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010160 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010163 if (argvars[1].v_type != VAR_UNKNOWN
10164 && argvars[2].v_type != VAR_UNKNOWN
10165 && get_tv_number_chk(&argvars[2], &error)
10166 && !error)
10167 {
10168 rettv->v_type = VAR_LIST;
10169 rettv->vval.v_list = NULL;
10170 }
10171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 if (*s == '%' || *s == '#' || *s == '<')
10174 {
10175 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010176 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010178 if (rettv->v_type == VAR_LIST)
10179 {
10180 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10181 list_append_string(rettv->vval.v_list, result, -1);
10182 else
10183 vim_free(result);
10184 }
10185 else
10186 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188 else
10189 {
10190 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010191 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 if (argvars[1].v_type != VAR_UNKNOWN
10193 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010194 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 if (!error)
10196 {
10197 ExpandInit(&xpc);
10198 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010199 if (p_wic)
10200 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 if (rettv->v_type == VAR_STRING)
10202 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10203 options, WILD_ALL);
10204 else if (rettv_list_alloc(rettv) != FAIL)
10205 {
10206 int i;
10207
10208 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10209 for (i = 0; i < xpc.xp_numfiles; i++)
10210 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10211 ExpandCleanup(&xpc);
10212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 }
10214 else
10215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217}
10218
10219/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010220 * Go over all entries in "d2" and add them to "d1".
10221 * When "action" is "error" then a duplicate key is an error.
10222 * When "action" is "force" then a duplicate key is overwritten.
10223 * Otherwise duplicate keys are ignored ("action" is "keep").
10224 */
10225 void
10226dict_extend(d1, d2, action)
10227 dict_T *d1;
10228 dict_T *d2;
10229 char_u *action;
10230{
10231 dictitem_T *di1;
10232 hashitem_T *hi2;
10233 int todo;
10234
10235 todo = (int)d2->dv_hashtab.ht_used;
10236 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10237 {
10238 if (!HASHITEM_EMPTY(hi2))
10239 {
10240 --todo;
10241 di1 = dict_find(d1, hi2->hi_key, -1);
10242 if (d1->dv_scope != 0)
10243 {
10244 /* Disallow replacing a builtin function in l: and g:.
10245 * Check the key to be valid when adding to any
10246 * scope. */
10247 if (d1->dv_scope == VAR_DEF_SCOPE
10248 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10249 && var_check_func_name(hi2->hi_key,
10250 di1 == NULL))
10251 break;
10252 if (!valid_varname(hi2->hi_key))
10253 break;
10254 }
10255 if (di1 == NULL)
10256 {
10257 di1 = dictitem_copy(HI2DI(hi2));
10258 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10259 dictitem_free(di1);
10260 }
10261 else if (*action == 'e')
10262 {
10263 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10264 break;
10265 }
10266 else if (*action == 'f' && HI2DI(hi2) != di1)
10267 {
10268 clear_tv(&di1->di_tv);
10269 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10270 }
10271 }
10272 }
10273}
10274
10275/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010278 */
10279 static void
10280f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010283{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010284 char *arg_errmsg = N_("extend() argument");
10285
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 list_T *l1, *l2;
10289 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010292
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 l1 = argvars[0].vval.v_list;
10294 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010295 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010296 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 {
10298 if (argvars[2].v_type != VAR_UNKNOWN)
10299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 before = get_tv_number_chk(&argvars[2], &error);
10301 if (error)
10302 return; /* type error; errmsg already given */
10303
Bram Moolenaar758711c2005-02-02 23:11:38 +000010304 if (before == l1->lv_len)
10305 item = NULL;
10306 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010308 item = list_find(l1, before);
10309 if (item == NULL)
10310 {
10311 EMSGN(_(e_listidx), before);
10312 return;
10313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 }
10315 }
10316 else
10317 item = NULL;
10318 list_extend(l1, l2, item);
10319
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 copy_tv(&argvars[0], rettv);
10321 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10324 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010325 dict_T *d1, *d2;
10326 char_u *action;
10327 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010328
10329 d1 = argvars[0].vval.v_dict;
10330 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010331 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010332 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 {
10334 /* Check the third argument. */
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 {
10337 static char *(av[]) = {"keep", "force", "error"};
10338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 action = get_tv_string_chk(&argvars[2]);
10340 if (action == NULL)
10341 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 for (i = 0; i < 3; ++i)
10343 if (STRCMP(action, av[i]) == 0)
10344 break;
10345 if (i == 3)
10346 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010347 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 return;
10349 }
10350 }
10351 else
10352 action = (char_u *)"force";
10353
Bram Moolenaara9922d62013-05-30 13:01:18 +020010354 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 copy_tv(&argvars[0], rettv);
10357 }
10358 }
10359 else
10360 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010361}
10362
10363/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010364 * "feedkeys()" function
10365 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010366 static void
10367f_feedkeys(argvars, rettv)
10368 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010369 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370{
10371 int remap = TRUE;
10372 char_u *keys, *flags;
10373 char_u nbuf[NUMBUFLEN];
10374 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010375 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010376
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010377 /* This is not allowed in the sandbox. If the commands would still be
10378 * executed in the sandbox it would be OK, but it probably happens later,
10379 * when "sandbox" is no longer set. */
10380 if (check_secure())
10381 return;
10382
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010383 keys = get_tv_string(&argvars[0]);
10384 if (*keys != NUL)
10385 {
10386 if (argvars[1].v_type != VAR_UNKNOWN)
10387 {
10388 flags = get_tv_string_buf(&argvars[1], nbuf);
10389 for ( ; *flags != NUL; ++flags)
10390 {
10391 switch (*flags)
10392 {
10393 case 'n': remap = FALSE; break;
10394 case 'm': remap = TRUE; break;
10395 case 't': typed = TRUE; break;
10396 }
10397 }
10398 }
10399
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010400 /* Need to escape K_SPECIAL and CSI before putting the string in the
10401 * typeahead buffer. */
10402 keys_esc = vim_strsave_escape_csi(keys);
10403 if (keys_esc != NULL)
10404 {
10405 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010407 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010408 if (vgetc_busy)
10409 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010410 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411 }
10412}
10413
10414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 * "filereadable()" function
10416 */
10417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010422 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 char_u *p;
10424 int n;
10425
Bram Moolenaarc236c162008-07-13 17:41:49 +000010426#ifndef O_NONBLOCK
10427# define O_NONBLOCK 0
10428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010430 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10431 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 {
10433 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010434 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 }
10436 else
10437 n = FALSE;
10438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010443 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 * rights to write into.
10445 */
10446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 typval_T *argvars;
10449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010451 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452}
10453
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010454static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010455
10456 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010457findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010460 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461{
10462#ifdef FEAT_SEARCHPATH
10463 char_u *fname;
10464 char_u *fresult = NULL;
10465 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10466 char_u *p;
10467 char_u pathbuf[NUMBUFLEN];
10468 int count = 1;
10469 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470 int error = FALSE;
10471#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010472
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010473 rettv->vval.v_string = NULL;
10474 rettv->v_type = VAR_STRING;
10475
10476#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10482 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010483 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 else
10485 {
10486 if (*p != NUL)
10487 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010490 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010491 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010492 }
10493
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010494 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10495 error = TRUE;
10496
10497 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 do
10500 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010501 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010502 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 fresult = find_file_in_path_option(first ? fname : NULL,
10504 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010505 0, first, path,
10506 find_what,
10507 curbuf->b_ffname,
10508 find_what == FINDFILE_DIR
10509 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010511
10512 if (fresult != NULL && rettv->v_type == VAR_LIST)
10513 list_append_string(rettv->vval.v_list, fresult, -1);
10514
10515 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 if (rettv->v_type == VAR_STRING)
10519 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010521}
10522
Bram Moolenaar33570922005-01-25 22:26:29 +000010523static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10524static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010525
10526/*
10527 * Implementation of map() and filter().
10528 */
10529 static void
10530filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *argvars;
10532 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 int map;
10534{
10535 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 listitem_T *li, *nli;
10538 list_T *l = NULL;
10539 dictitem_T *di;
10540 hashtab_T *ht;
10541 hashitem_T *hi;
10542 dict_T *d = NULL;
10543 typval_T save_val;
10544 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010546 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010547 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10548 char *arg_errmsg = (map ? N_("map() argument")
10549 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010550 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010551 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010555 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 return;
10558 }
10559 else if (argvars[0].v_type == VAR_DICT)
10560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010561 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010562 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 return;
10564 }
10565 else
10566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010567 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 return;
10569 }
10570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 expr = get_tv_string_buf_chk(&argvars[1], buf);
10572 /* On type errors, the preceding call has already displayed an error
10573 * message. Avoid a misleading error message for an empty string that
10574 * was not passed as argument. */
10575 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 prepare_vimvar(VV_VAL, &save_val);
10578 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010579
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010580 /* We reset "did_emsg" to be able to detect whether an error
10581 * occurred during evaluation of the expression. */
10582 save_did_emsg = did_emsg;
10583 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010584
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010585 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 vimvars[VV_KEY].vv_type = VAR_STRING;
10589
10590 ht = &d->dv_hashtab;
10591 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 if (!HASHITEM_EMPTY(hi))
10596 {
10597 --todo;
10598 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010599 if (tv_check_lock(di->di_tv.v_lock,
10600 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 break;
10602 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010603 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010604 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 break;
10606 if (!map && rem)
10607 dictitem_remove(d, di);
10608 clear_tv(&vimvars[VV_KEY].vv_tv);
10609 }
10610 }
10611 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 }
10613 else
10614 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010615 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 for (li = l->lv_first; li != NULL; li = nli)
10618 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010619 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010620 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010622 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010623 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010624 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010625 break;
10626 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010628 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010631
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010632 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010634
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010635 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637
10638 copy_tv(&argvars[0], rettv);
10639}
10640
10641 static int
10642filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010644 char_u *expr;
10645 int map;
10646 int *remp;
10647{
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010650 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 s = expr;
10654 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010655 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010656 if (*s != NUL) /* check for trailing chars after expr */
10657 {
10658 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010659 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660 }
10661 if (map)
10662 {
10663 /* map(): replace the list item value */
10664 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010665 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 *tv = rettv;
10667 }
10668 else
10669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 int error = FALSE;
10671
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 /* On type error, nothing has been removed; return FAIL to stop the
10676 * loop. The error message was given by get_tv_number_chk(). */
10677 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010680 retval = OK;
10681theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684}
10685
10686/*
10687 * "filter()" function
10688 */
10689 static void
10690f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693{
10694 filter_map(argvars, rettv, FALSE);
10695}
10696
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698 * "finddir({fname}[, {path}[, {count}]])" function
10699 */
10700 static void
10701f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010705 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706}
10707
10708/*
10709 * "findfile({fname}[, {path}[, {count}]])" function
10710 */
10711 static void
10712f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010716 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717}
10718
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010719#ifdef FEAT_FLOAT
10720/*
10721 * "float2nr({float})" function
10722 */
10723 static void
10724f_float2nr(argvars, rettv)
10725 typval_T *argvars;
10726 typval_T *rettv;
10727{
10728 float_T f;
10729
10730 if (get_float_arg(argvars, &f) == OK)
10731 {
10732 if (f < -0x7fffffff)
10733 rettv->vval.v_number = -0x7fffffff;
10734 else if (f > 0x7fffffff)
10735 rettv->vval.v_number = 0x7fffffff;
10736 else
10737 rettv->vval.v_number = (varnumber_T)f;
10738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010739}
10740
10741/*
10742 * "floor({float})" function
10743 */
10744 static void
10745f_floor(argvars, rettv)
10746 typval_T *argvars;
10747 typval_T *rettv;
10748{
10749 float_T f;
10750
10751 rettv->v_type = VAR_FLOAT;
10752 if (get_float_arg(argvars, &f) == OK)
10753 rettv->vval.v_float = floor(f);
10754 else
10755 rettv->vval.v_float = 0.0;
10756}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010757
10758/*
10759 * "fmod()" function
10760 */
10761 static void
10762f_fmod(argvars, rettv)
10763 typval_T *argvars;
10764 typval_T *rettv;
10765{
10766 float_T fx, fy;
10767
10768 rettv->v_type = VAR_FLOAT;
10769 if (get_float_arg(argvars, &fx) == OK
10770 && get_float_arg(&argvars[1], &fy) == OK)
10771 rettv->vval.v_float = fmod(fx, fy);
10772 else
10773 rettv->vval.v_float = 0.0;
10774}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010775#endif
10776
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010778 * "fnameescape({string})" function
10779 */
10780 static void
10781f_fnameescape(argvars, rettv)
10782 typval_T *argvars;
10783 typval_T *rettv;
10784{
10785 rettv->vval.v_string = vim_strsave_fnameescape(
10786 get_tv_string(&argvars[0]), FALSE);
10787 rettv->v_type = VAR_STRING;
10788}
10789
10790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 * "fnamemodify({fname}, {mods})" function
10792 */
10793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 typval_T *argvars;
10796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797{
10798 char_u *fname;
10799 char_u *mods;
10800 int usedlen = 0;
10801 int len;
10802 char_u *fbuf = NULL;
10803 char_u buf[NUMBUFLEN];
10804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 fname = get_tv_string_chk(&argvars[0]);
10806 mods = get_tv_string_buf_chk(&argvars[1], buf);
10807 if (fname == NULL || mods == NULL)
10808 fname = NULL;
10809 else
10810 {
10811 len = (int)STRLEN(fname);
10812 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 vim_free(fbuf);
10821}
10822
Bram Moolenaar33570922005-01-25 22:26:29 +000010823static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824
10825/*
10826 * "foldclosed()" function
10827 */
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010832 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834#ifdef FEAT_FOLDING
10835 linenr_T lnum;
10836 linenr_T first, last;
10837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10840 {
10841 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10842 {
10843 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 return;
10848 }
10849 }
10850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852}
10853
10854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 * "foldclosed()" function
10856 */
10857 static void
10858f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *argvars;
10860 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861{
10862 foldclosed_both(argvars, rettv, FALSE);
10863}
10864
10865/*
10866 * "foldclosedend()" function
10867 */
10868 static void
10869f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872{
10873 foldclosed_both(argvars, rettv, TRUE);
10874}
10875
10876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 * "foldlevel()" function
10878 */
10879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010881 typval_T *argvars UNUSED;
10882 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883{
10884#ifdef FEAT_FOLDING
10885 linenr_T lnum;
10886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
10894 * "foldtext()" function
10895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#ifdef FEAT_FOLDING
10902 linenr_T lnum;
10903 char_u *s;
10904 char_u *r;
10905 int len;
10906 char *txt;
10907#endif
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
10910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10913 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10914 <= curbuf->b_ml.ml_line_count
10915 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 {
10917 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10919 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
10921 if (!linewhite(lnum))
10922 break;
10923 ++lnum;
10924 }
10925
10926 /* Find interesting text in this line. */
10927 s = skipwhite(ml_get(lnum));
10928 /* skip C comment-start */
10929 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010932 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010934 {
10935 s = skipwhite(ml_get(lnum + 1));
10936 if (*s == '*')
10937 s = skipwhite(s + 1);
10938 }
10939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 txt = _("+-%s%3ld lines: ");
10941 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 + 20 /* for %3ld */
10944 + STRLEN(s))); /* concatenated */
10945 if (r != NULL)
10946 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10948 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10949 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 len = (int)STRLEN(r);
10951 STRCAT(r, s);
10952 /* remove 'foldmarker' and 'commentstring' */
10953 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956 }
10957#endif
10958}
10959
10960/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010961 * "foldtextresult(lnum)" function
10962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010967{
10968#ifdef FEAT_FOLDING
10969 linenr_T lnum;
10970 char_u *text;
10971 char_u buf[51];
10972 foldinfo_T foldinfo;
10973 int fold_count;
10974#endif
10975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->v_type = VAR_STRING;
10977 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010978#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 /* treat illegal types and illegal string values for {lnum} the same */
10981 if (lnum < 0)
10982 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010983 fold_count = foldedCount(curwin, lnum, &foldinfo);
10984 if (fold_count > 0)
10985 {
10986 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10987 &foldinfo, buf);
10988 if (text == buf)
10989 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991 }
10992#endif
10993}
10994
10995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 * "foreground()" function
10997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
11001 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#ifdef FEAT_GUI
11004 if (gui.in_use)
11005 gui_mch_set_foreground();
11006#else
11007# ifdef WIN32
11008 win32_set_foreground();
11009# endif
11010#endif
11011}
11012
11013/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 * "function()" function
11015 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020{
11021 char_u *s;
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011024 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011026 /* Don't check an autoload name for existence here. */
11027 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011028 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011029 else
11030 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011031 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011032 {
11033 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011034 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011035
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011036 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11037 * also be called from another script. Using trans_function_name()
11038 * would also work, but some plugins depend on the name being
11039 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011040 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011041 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011042 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011043 if (rettv->vval.v_string != NULL)
11044 {
11045 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011046 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011047 }
11048 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011049 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011050 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 }
11053}
11054
11055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011056 * "garbagecollect()" function
11057 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011058 static void
11059f_garbagecollect(argvars, rettv)
11060 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011061 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011062{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011063 /* This is postponed until we are back at the toplevel, because we may be
11064 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11065 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011066
11067 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11068 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011069}
11070
11071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011072 * "get()" function
11073 */
11074 static void
11075f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078{
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 listitem_T *li;
11080 list_T *l;
11081 dictitem_T *di;
11082 dict_T *d;
11083 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011086 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011087 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 int error = FALSE;
11090
11091 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11092 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 else if (argvars[0].v_type == VAR_DICT)
11097 {
11098 if ((d = argvars[0].vval.v_dict) != NULL)
11099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 if (di != NULL)
11102 tv = &di->di_tv;
11103 }
11104 }
11105 else
11106 EMSG2(_(e_listdictarg), "get()");
11107
11108 if (tv == NULL)
11109 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 copy_tv(&argvars[2], rettv);
11112 }
11113 else
11114 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115}
11116
Bram Moolenaar342337a2005-07-21 21:11:17 +000011117static 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 +000011118
11119/*
11120 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011121 * Return a range (from start to end) of lines in rettv from the specified
11122 * buffer.
11123 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011124 */
11125 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011126get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011127 buf_T *buf;
11128 linenr_T start;
11129 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011130 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011131 typval_T *rettv;
11132{
11133 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011134
Bram Moolenaar959a1432013-12-14 12:17:38 +010011135 rettv->v_type = VAR_STRING;
11136 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011137 if (retlist && rettv_list_alloc(rettv) == FAIL)
11138 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011139
11140 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11141 return;
11142
11143 if (!retlist)
11144 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11146 p = ml_get_buf(buf, start, FALSE);
11147 else
11148 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149 rettv->vval.v_string = vim_strsave(p);
11150 }
11151 else
11152 {
11153 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154 return;
11155
11156 if (start < 1)
11157 start = 1;
11158 if (end > buf->b_ml.ml_line_count)
11159 end = buf->b_ml.ml_line_count;
11160 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011161 if (list_append_string(rettv->vval.v_list,
11162 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 }
11165}
11166
11167/*
11168 * "getbufline()" function
11169 */
11170 static void
11171f_getbufline(argvars, rettv)
11172 typval_T *argvars;
11173 typval_T *rettv;
11174{
11175 linenr_T lnum;
11176 linenr_T end;
11177 buf_T *buf;
11178
11179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11180 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011181 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011182 --emsg_off;
11183
Bram Moolenaar661b1822005-07-28 22:36:45 +000011184 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011185 if (argvars[2].v_type == VAR_UNKNOWN)
11186 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011188 end = get_tv_lnum_buf(&argvars[2], buf);
11189
Bram Moolenaar342337a2005-07-21 21:11:17 +000011190 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011191}
11192
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193/*
11194 * "getbufvar()" function
11195 */
11196 static void
11197f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 typval_T *argvars;
11199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200{
11201 buf_T *buf;
11202 buf_T *save_curbuf;
11203 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011204 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011205 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11208 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011212 rettv->v_type = VAR_STRING;
11213 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214
11215 if (buf != NULL && varname != NULL)
11216 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011217 /* set curbuf to be our buf, temporarily */
11218 save_curbuf = curbuf;
11219 curbuf = buf;
11220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011222 {
11223 if (get_option_tv(&varname, rettv, TRUE) == OK)
11224 done = TRUE;
11225 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011226 else if (STRCMP(varname, "changedtick") == 0)
11227 {
11228 rettv->v_type = VAR_NUMBER;
11229 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011230 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011231 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 else
11233 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011234 /* Look up the variable. */
11235 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11236 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11237 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011239 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011241 done = TRUE;
11242 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011244
11245 /* restore previous notion of curbuf */
11246 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247 }
11248
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011249 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11250 /* use the default value */
11251 copy_tv(&argvars[2], rettv);
11252
Bram Moolenaar0d660222005-01-07 21:51:51 +000011253 --emsg_off;
11254}
11255
11256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 * "getchar()" function
11258 */
11259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 typval_T *argvars;
11262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
11264 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011267 /* Position the cursor. Needed after a message that ends in a space. */
11268 windgoto(msg_row, msg_col);
11269
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 ++no_mapping;
11271 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011272 for (;;)
11273 {
11274 if (argvars[0].v_type == VAR_UNKNOWN)
11275 /* getchar(): blocking wait. */
11276 n = safe_vgetc();
11277 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11278 /* getchar(1): only check if char avail */
11279 n = vpeekc();
11280 else if (error || vpeekc() == NUL)
11281 /* illegal argument or getchar(0) and no char avail: return zero */
11282 n = 0;
11283 else
11284 /* getchar(0) and char avail: return char */
11285 n = safe_vgetc();
11286 if (n == K_IGNORE)
11287 continue;
11288 break;
11289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 --no_mapping;
11291 --allow_keys;
11292
Bram Moolenaar219b8702006-11-01 14:32:36 +000011293 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11294 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11295 vimvars[VV_MOUSE_COL].vv_nr = 0;
11296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 if (IS_SPECIAL(n) || mod_mask != 0)
11299 {
11300 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11301 int i = 0;
11302
11303 /* Turn a special key into three bytes, plus modifier. */
11304 if (mod_mask != 0)
11305 {
11306 temp[i++] = K_SPECIAL;
11307 temp[i++] = KS_MODIFIER;
11308 temp[i++] = mod_mask;
11309 }
11310 if (IS_SPECIAL(n))
11311 {
11312 temp[i++] = K_SPECIAL;
11313 temp[i++] = K_SECOND(n);
11314 temp[i++] = K_THIRD(n);
11315 }
11316#ifdef FEAT_MBYTE
11317 else if (has_mbyte)
11318 i += (*mb_char2bytes)(n, temp + i);
11319#endif
11320 else
11321 temp[i++] = n;
11322 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011325
11326#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011327 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011328 {
11329 int row = mouse_row;
11330 int col = mouse_col;
11331 win_T *win;
11332 linenr_T lnum;
11333# ifdef FEAT_WINDOWS
11334 win_T *wp;
11335# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011336 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011337
11338 if (row >= 0 && col >= 0)
11339 {
11340 /* Find the window at the mouse coordinates and compute the
11341 * text position. */
11342 win = mouse_find_win(&row, &col);
11343 (void)mouse_comp_pos(win, &row, &col, &lnum);
11344# ifdef FEAT_WINDOWS
11345 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011346 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011347# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011348 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011349 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11350 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11351 }
11352 }
11353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 }
11355}
11356
11357/*
11358 * "getcharmod()" function
11359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366}
11367
11368/*
11369 * "getcmdline()" function
11370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378}
11379
11380/*
11381 * "getcmdpos()" function
11382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011392 * "getcmdtype()" function
11393 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011394 static void
11395f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397 typval_T *rettv;
11398{
11399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = alloc(2);
11401 if (rettv->vval.v_string != NULL)
11402 {
11403 rettv->vval.v_string[0] = get_cmdline_type();
11404 rettv->vval.v_string[1] = NUL;
11405 }
11406}
11407
11408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 * "getcwd()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011416 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011419 rettv->vval.v_string = NULL;
11420 cwd = alloc(MAXPATHL);
11421 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011423 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11424 {
11425 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011427 if (rettv->vval.v_string != NULL)
11428 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011430 }
11431 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011436 * "getfontname()" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011445#ifdef FEAT_GUI
11446 if (gui.in_use)
11447 {
11448 GuiFont font;
11449 char_u *name = NULL;
11450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011452 {
11453 /* Get the "Normal" font. Either the name saved by
11454 * hl_set_font_name() or from the font ID. */
11455 font = gui.norm_font;
11456 name = hl_get_font_name();
11457 }
11458 else
11459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011461 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11462 return;
11463 font = gui_mch_get_font(name, FALSE);
11464 if (font == NOFONT)
11465 return; /* Invalid font name, return empty string. */
11466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011469 gui_mch_free_font(font);
11470 }
11471#endif
11472}
11473
11474/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011475 * "getfperm({fname})" function
11476 */
11477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *argvars;
11480 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481{
11482 char_u *fname;
11483 struct stat st;
11484 char_u *perm = NULL;
11485 char_u flags[] = "rwx";
11486 int i;
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491 if (mch_stat((char *)fname, &st) >= 0)
11492 {
11493 perm = vim_strsave((char_u *)"---------");
11494 if (perm != NULL)
11495 {
11496 for (i = 0; i < 9; i++)
11497 {
11498 if (st.st_mode & (1 << (8 - i)))
11499 perm[i] = flags[i % 3];
11500 }
11501 }
11502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504}
11505
11506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 * "getfsize({fname})" function
11508 */
11509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
11514 char_u *fname;
11515 struct stat st;
11516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520
11521 if (mch_stat((char *)fname, &st) >= 0)
11522 {
11523 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011528
11529 /* non-perfect check for overflow */
11530 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11531 rettv->vval.v_number = -2;
11532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 }
11534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "getftime({fname})" function
11540 */
11541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546 char_u *fname;
11547 struct stat st;
11548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550
11551 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555}
11556
11557/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011558 * "getftype({fname})" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564{
11565 char_u *fname;
11566 struct stat st;
11567 char_u *type = NULL;
11568 char *t;
11569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011573 if (mch_lstat((char *)fname, &st) >= 0)
11574 {
11575#ifdef S_ISREG
11576 if (S_ISREG(st.st_mode))
11577 t = "file";
11578 else if (S_ISDIR(st.st_mode))
11579 t = "dir";
11580# ifdef S_ISLNK
11581 else if (S_ISLNK(st.st_mode))
11582 t = "link";
11583# endif
11584# ifdef S_ISBLK
11585 else if (S_ISBLK(st.st_mode))
11586 t = "bdev";
11587# endif
11588# ifdef S_ISCHR
11589 else if (S_ISCHR(st.st_mode))
11590 t = "cdev";
11591# endif
11592# ifdef S_ISFIFO
11593 else if (S_ISFIFO(st.st_mode))
11594 t = "fifo";
11595# endif
11596# ifdef S_ISSOCK
11597 else if (S_ISSOCK(st.st_mode))
11598 t = "fifo";
11599# endif
11600 else
11601 t = "other";
11602#else
11603# ifdef S_IFMT
11604 switch (st.st_mode & S_IFMT)
11605 {
11606 case S_IFREG: t = "file"; break;
11607 case S_IFDIR: t = "dir"; break;
11608# ifdef S_IFLNK
11609 case S_IFLNK: t = "link"; break;
11610# endif
11611# ifdef S_IFBLK
11612 case S_IFBLK: t = "bdev"; break;
11613# endif
11614# ifdef S_IFCHR
11615 case S_IFCHR: t = "cdev"; break;
11616# endif
11617# ifdef S_IFIFO
11618 case S_IFIFO: t = "fifo"; break;
11619# endif
11620# ifdef S_IFSOCK
11621 case S_IFSOCK: t = "socket"; break;
11622# endif
11623 default: t = "other";
11624 }
11625# else
11626 if (mch_isdir(fname))
11627 t = "dir";
11628 else
11629 t = "file";
11630# endif
11631#endif
11632 type = vim_strsave((char_u *)t);
11633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011635}
11636
11637/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011638 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639 */
11640 static void
11641f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 typval_T *argvars;
11643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
11645 linenr_T lnum;
11646 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011647 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011648
11649 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011650 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011653 retlist = FALSE;
11654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011656 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011658 retlist = TRUE;
11659 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011660
Bram Moolenaar342337a2005-07-21 21:11:17 +000011661 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
11664/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011665 * "getmatches()" function
11666 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011667 static void
11668f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011669 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011670 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011671{
11672#ifdef FEAT_SEARCH_EXTRA
11673 dict_T *dict;
11674 matchitem_T *cur = curwin->w_match_head;
11675
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011676 if (rettv_list_alloc(rettv) == OK)
11677 {
11678 while (cur != NULL)
11679 {
11680 dict = dict_alloc();
11681 if (dict == NULL)
11682 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011683 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11684 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11685 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11686 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11687 list_append_dict(rettv->vval.v_list, dict);
11688 cur = cur->next;
11689 }
11690 }
11691#endif
11692}
11693
11694/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011695 * "getpid()" function
11696 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011697 static void
11698f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011699 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011700 typval_T *rettv;
11701{
11702 rettv->vval.v_number = mch_get_pid();
11703}
11704
11705/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011706 * "getpos(string)" function
11707 */
11708 static void
11709f_getpos(argvars, rettv)
11710 typval_T *argvars;
11711 typval_T *rettv;
11712{
11713 pos_T *fp;
11714 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011715 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011716
11717 if (rettv_list_alloc(rettv) == OK)
11718 {
11719 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011720 fp = var2fpos(&argvars[0], TRUE, &fnum);
11721 if (fnum != -1)
11722 list_append_number(l, (varnumber_T)fnum);
11723 else
11724 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011725 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11726 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011727 list_append_number(l, (fp != NULL)
11728 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011729 : (varnumber_T)0);
11730 list_append_number(l,
11731#ifdef FEAT_VIRTUALEDIT
11732 (fp != NULL) ? (varnumber_T)fp->coladd :
11733#endif
11734 (varnumber_T)0);
11735 }
11736 else
11737 rettv->vval.v_number = FALSE;
11738}
11739
11740/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011741 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011742 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011743 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011744f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011745 typval_T *argvars UNUSED;
11746 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011747{
11748#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011749 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011750#endif
11751
Bram Moolenaar2641f772005-03-25 21:58:17 +000011752#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011753 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011754 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011755 wp = NULL;
11756 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11757 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011758 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011759 if (wp == NULL)
11760 return;
11761 }
11762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011763 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011764 }
11765#endif
11766}
11767
11768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 * "getreg()" function
11770 */
11771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011773 typval_T *argvars;
11774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 char_u *strregname;
11777 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011778 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011781 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 strregname = get_tv_string_chk(&argvars[0]);
11784 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011789 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 regname = (strregname == NULL ? '"' : *strregname);
11791 if (regname == 0)
11792 regname = '"';
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 rettv->vval.v_string = error ? NULL :
11796 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797}
11798
11799/*
11800 * "getregtype()" function
11801 */
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
11807 char_u *strregname;
11808 int regname;
11809 char_u buf[NUMBUFLEN + 2];
11810 long reglen = 0;
11811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 {
11814 strregname = get_tv_string_chk(&argvars[0]);
11815 if (strregname == NULL) /* type error; errmsg already given */
11816 {
11817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = NULL;
11819 return;
11820 }
11821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 else
11823 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011824 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
11826 regname = (strregname == NULL ? '"' : *strregname);
11827 if (regname == 0)
11828 regname = '"';
11829
11830 buf[0] = NUL;
11831 buf[1] = NUL;
11832 switch (get_reg_type(regname, &reglen))
11833 {
11834 case MLINE: buf[0] = 'V'; break;
11835 case MCHAR: buf[0] = 'v'; break;
11836#ifdef FEAT_VISUAL
11837 case MBLOCK:
11838 buf[0] = Ctrl_V;
11839 sprintf((char *)buf + 1, "%ld", reglen + 1);
11840 break;
11841#endif
11842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843 rettv->v_type = VAR_STRING;
11844 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845}
11846
11847/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011848 * "gettabvar()" function
11849 */
11850 static void
11851f_gettabvar(argvars, rettv)
11852 typval_T *argvars;
11853 typval_T *rettv;
11854{
11855 tabpage_T *tp;
11856 dictitem_T *v;
11857 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011858 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011859
11860 rettv->v_type = VAR_STRING;
11861 rettv->vval.v_string = NULL;
11862
11863 varname = get_tv_string_chk(&argvars[1]);
11864 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11865 if (tp != NULL && varname != NULL)
11866 {
11867 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011868 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011869 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011870 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011871 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011872 done = TRUE;
11873 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011874 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011875
11876 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011877 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011878}
11879
11880/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881 * "gettabwinvar()" function
11882 */
11883 static void
11884f_gettabwinvar(argvars, rettv)
11885 typval_T *argvars;
11886 typval_T *rettv;
11887{
11888 getwinvar(argvars, rettv, 1);
11889}
11890
11891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 * "getwinposx()" function
11893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900#ifdef FEAT_GUI
11901 if (gui.in_use)
11902 {
11903 int x, y;
11904
11905 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 }
11908#endif
11909}
11910
11911/*
11912 * "getwinposy()" function
11913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011916 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920#ifdef FEAT_GUI
11921 if (gui.in_use)
11922 {
11923 int x, y;
11924
11925 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 }
11928#endif
11929}
11930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011932 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011933 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011934 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011935find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011936 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011937 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011938{
11939#ifdef FEAT_WINDOWS
11940 win_T *wp;
11941#endif
11942 int nr;
11943
11944 nr = get_tv_number_chk(vp, NULL);
11945
11946#ifdef FEAT_WINDOWS
11947 if (nr < 0)
11948 return NULL;
11949 if (nr == 0)
11950 return curwin;
11951
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011952 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11953 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011954 if (--nr <= 0)
11955 break;
11956 return wp;
11957#else
11958 if (nr == 0 || nr == 1)
11959 return curwin;
11960 return NULL;
11961#endif
11962}
11963
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964/*
11965 * "getwinvar()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011972 getwinvar(argvars, rettv, 0);
11973}
11974
11975/*
11976 * getwinvar() and gettabwinvar()
11977 */
11978 static void
11979getwinvar(argvars, rettv, off)
11980 typval_T *argvars;
11981 typval_T *rettv;
11982 int off; /* 1 for gettabwinvar() */
11983{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 win_T *win, *oldcurwin;
11985 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011987 tabpage_T *tp = NULL;
11988 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011989 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011991#ifdef FEAT_WINDOWS
11992 if (off == 1)
11993 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11994 else
11995 tp = curtab;
11996#endif
11997 win = find_win_by_nr(&argvars[off], tp);
11998 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011999 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012001 rettv->v_type = VAR_STRING;
12002 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003
12004 if (win != NULL && varname != NULL)
12005 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012006 /* Set curwin to be our win, temporarily. Also set the tabpage,
12007 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012008 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012009
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012011 {
12012 if (get_option_tv(&varname, rettv, 1) == OK)
12013 done = TRUE;
12014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 else
12016 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012017 /* Look up the variable. */
12018 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12019 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012023 done = TRUE;
12024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012026
12027 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012028 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 }
12030
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012031 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12032 /* use the default return value */
12033 copy_tv(&argvars[off + 2], rettv);
12034
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 --emsg_off;
12036}
12037
12038/*
12039 * "glob()" function
12040 */
12041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 typval_T *argvars;
12044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012046 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012048 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012050 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012051 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012053 if (argvars[1].v_type != VAR_UNKNOWN)
12054 {
12055 if (get_tv_number_chk(&argvars[1], &error))
12056 options |= WILD_KEEP_ALL;
12057 if (argvars[2].v_type != VAR_UNKNOWN
12058 && get_tv_number_chk(&argvars[2], &error))
12059 {
12060 rettv->v_type = VAR_LIST;
12061 rettv->vval.v_list = NULL;
12062 }
12063 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012064 if (!error)
12065 {
12066 ExpandInit(&xpc);
12067 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012068 if (p_wic)
12069 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012070 if (rettv->v_type == VAR_STRING)
12071 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012072 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012073 else if (rettv_list_alloc(rettv) != FAIL)
12074 {
12075 int i;
12076
12077 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12078 NULL, options, WILD_ALL_KEEP);
12079 for (i = 0; i < xpc.xp_numfiles; i++)
12080 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12081
12082 ExpandCleanup(&xpc);
12083 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012084 }
12085 else
12086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087}
12088
12089/*
12090 * "globpath()" function
12091 */
12092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012097 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012100 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012102 /* When the optional second argument is non-zero, don't remove matches
12103 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12104 if (argvars[2].v_type != VAR_UNKNOWN
12105 && get_tv_number_chk(&argvars[2], &error))
12106 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012108 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 rettv->vval.v_string = NULL;
12110 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012111 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12112 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113}
12114
12115/*
12116 * "has()" function
12117 */
12118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
12123 int i;
12124 char_u *name;
12125 int n = FALSE;
12126 static char *(has_list[]) =
12127 {
12128#ifdef AMIGA
12129 "amiga",
12130# ifdef FEAT_ARP
12131 "arp",
12132# endif
12133#endif
12134#ifdef __BEOS__
12135 "beos",
12136#endif
12137#ifdef MSDOS
12138# ifdef DJGPP
12139 "dos32",
12140# else
12141 "dos16",
12142# endif
12143#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012144#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 "mac",
12146#endif
12147#if defined(MACOS_X_UNIX)
12148 "macunix",
12149#endif
12150#ifdef OS2
12151 "os2",
12152#endif
12153#ifdef __QNX__
12154 "qnx",
12155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156#ifdef UNIX
12157 "unix",
12158#endif
12159#ifdef VMS
12160 "vms",
12161#endif
12162#ifdef WIN16
12163 "win16",
12164#endif
12165#ifdef WIN32
12166 "win32",
12167#endif
12168#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12169 "win32unix",
12170#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012171#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 "win64",
12173#endif
12174#ifdef EBCDIC
12175 "ebcdic",
12176#endif
12177#ifndef CASE_INSENSITIVE_FILENAME
12178 "fname_case",
12179#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012180#ifdef HAVE_ACL
12181 "acl",
12182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183#ifdef FEAT_ARABIC
12184 "arabic",
12185#endif
12186#ifdef FEAT_AUTOCMD
12187 "autocmd",
12188#endif
12189#ifdef FEAT_BEVAL
12190 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012191# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12192 "balloon_multiline",
12193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#endif
12195#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12196 "builtin_terms",
12197# ifdef ALL_BUILTIN_TCAPS
12198 "all_builtin_terms",
12199# endif
12200#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012201#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12202 || defined(FEAT_GUI_W32) \
12203 || defined(FEAT_GUI_MOTIF))
12204 "browsefilter",
12205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#ifdef FEAT_BYTEOFF
12207 "byte_offset",
12208#endif
12209#ifdef FEAT_CINDENT
12210 "cindent",
12211#endif
12212#ifdef FEAT_CLIENTSERVER
12213 "clientserver",
12214#endif
12215#ifdef FEAT_CLIPBOARD
12216 "clipboard",
12217#endif
12218#ifdef FEAT_CMDL_COMPL
12219 "cmdline_compl",
12220#endif
12221#ifdef FEAT_CMDHIST
12222 "cmdline_hist",
12223#endif
12224#ifdef FEAT_COMMENTS
12225 "comments",
12226#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012227#ifdef FEAT_CONCEAL
12228 "conceal",
12229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230#ifdef FEAT_CRYPT
12231 "cryptv",
12232#endif
12233#ifdef FEAT_CSCOPE
12234 "cscope",
12235#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012236#ifdef FEAT_CURSORBIND
12237 "cursorbind",
12238#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012239#ifdef CURSOR_SHAPE
12240 "cursorshape",
12241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242#ifdef DEBUG
12243 "debug",
12244#endif
12245#ifdef FEAT_CON_DIALOG
12246 "dialog_con",
12247#endif
12248#ifdef FEAT_GUI_DIALOG
12249 "dialog_gui",
12250#endif
12251#ifdef FEAT_DIFF
12252 "diff",
12253#endif
12254#ifdef FEAT_DIGRAPHS
12255 "digraphs",
12256#endif
12257#ifdef FEAT_DND
12258 "dnd",
12259#endif
12260#ifdef FEAT_EMACS_TAGS
12261 "emacs_tags",
12262#endif
12263 "eval", /* always present, of course! */
12264#ifdef FEAT_EX_EXTRA
12265 "ex_extra",
12266#endif
12267#ifdef FEAT_SEARCH_EXTRA
12268 "extra_search",
12269#endif
12270#ifdef FEAT_FKMAP
12271 "farsi",
12272#endif
12273#ifdef FEAT_SEARCHPATH
12274 "file_in_path",
12275#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012276#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012277 "filterpipe",
12278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279#ifdef FEAT_FIND_ID
12280 "find_in_path",
12281#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012282#ifdef FEAT_FLOAT
12283 "float",
12284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285#ifdef FEAT_FOLDING
12286 "folding",
12287#endif
12288#ifdef FEAT_FOOTER
12289 "footer",
12290#endif
12291#if !defined(USE_SYSTEM) && defined(UNIX)
12292 "fork",
12293#endif
12294#ifdef FEAT_GETTEXT
12295 "gettext",
12296#endif
12297#ifdef FEAT_GUI
12298 "gui",
12299#endif
12300#ifdef FEAT_GUI_ATHENA
12301# ifdef FEAT_GUI_NEXTAW
12302 "gui_neXtaw",
12303# else
12304 "gui_athena",
12305# endif
12306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307#ifdef FEAT_GUI_GTK
12308 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012311#ifdef FEAT_GUI_GNOME
12312 "gui_gnome",
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef FEAT_GUI_MAC
12315 "gui_mac",
12316#endif
12317#ifdef FEAT_GUI_MOTIF
12318 "gui_motif",
12319#endif
12320#ifdef FEAT_GUI_PHOTON
12321 "gui_photon",
12322#endif
12323#ifdef FEAT_GUI_W16
12324 "gui_win16",
12325#endif
12326#ifdef FEAT_GUI_W32
12327 "gui_win32",
12328#endif
12329#ifdef FEAT_HANGULIN
12330 "hangul_input",
12331#endif
12332#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12333 "iconv",
12334#endif
12335#ifdef FEAT_INS_EXPAND
12336 "insert_expand",
12337#endif
12338#ifdef FEAT_JUMPLIST
12339 "jumplist",
12340#endif
12341#ifdef FEAT_KEYMAP
12342 "keymap",
12343#endif
12344#ifdef FEAT_LANGMAP
12345 "langmap",
12346#endif
12347#ifdef FEAT_LIBCALL
12348 "libcall",
12349#endif
12350#ifdef FEAT_LINEBREAK
12351 "linebreak",
12352#endif
12353#ifdef FEAT_LISP
12354 "lispindent",
12355#endif
12356#ifdef FEAT_LISTCMDS
12357 "listcmds",
12358#endif
12359#ifdef FEAT_LOCALMAP
12360 "localmap",
12361#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012362#ifdef FEAT_LUA
12363# ifndef DYNAMIC_LUA
12364 "lua",
12365# endif
12366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367#ifdef FEAT_MENU
12368 "menu",
12369#endif
12370#ifdef FEAT_SESSION
12371 "mksession",
12372#endif
12373#ifdef FEAT_MODIFY_FNAME
12374 "modify_fname",
12375#endif
12376#ifdef FEAT_MOUSE
12377 "mouse",
12378#endif
12379#ifdef FEAT_MOUSESHAPE
12380 "mouseshape",
12381#endif
12382#if defined(UNIX) || defined(VMS)
12383# ifdef FEAT_MOUSE_DEC
12384 "mouse_dec",
12385# endif
12386# ifdef FEAT_MOUSE_GPM
12387 "mouse_gpm",
12388# endif
12389# ifdef FEAT_MOUSE_JSB
12390 "mouse_jsbterm",
12391# endif
12392# ifdef FEAT_MOUSE_NET
12393 "mouse_netterm",
12394# endif
12395# ifdef FEAT_MOUSE_PTERM
12396 "mouse_pterm",
12397# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012398# ifdef FEAT_MOUSE_SGR
12399 "mouse_sgr",
12400# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012401# ifdef FEAT_SYSMOUSE
12402 "mouse_sysmouse",
12403# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012404# ifdef FEAT_MOUSE_URXVT
12405 "mouse_urxvt",
12406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407# ifdef FEAT_MOUSE_XTERM
12408 "mouse_xterm",
12409# endif
12410#endif
12411#ifdef FEAT_MBYTE
12412 "multi_byte",
12413#endif
12414#ifdef FEAT_MBYTE_IME
12415 "multi_byte_ime",
12416#endif
12417#ifdef FEAT_MULTI_LANG
12418 "multi_lang",
12419#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012420#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012421#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012422 "mzscheme",
12423#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425#ifdef FEAT_OLE
12426 "ole",
12427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428#ifdef FEAT_PATH_EXTRA
12429 "path_extra",
12430#endif
12431#ifdef FEAT_PERL
12432#ifndef DYNAMIC_PERL
12433 "perl",
12434#endif
12435#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012436#ifdef FEAT_PERSISTENT_UNDO
12437 "persistent_undo",
12438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef FEAT_PYTHON
12440#ifndef DYNAMIC_PYTHON
12441 "python",
12442#endif
12443#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012444#ifdef FEAT_PYTHON3
12445#ifndef DYNAMIC_PYTHON3
12446 "python3",
12447#endif
12448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#ifdef FEAT_POSTSCRIPT
12450 "postscript",
12451#endif
12452#ifdef FEAT_PRINTER
12453 "printer",
12454#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012455#ifdef FEAT_PROFILE
12456 "profile",
12457#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012458#ifdef FEAT_RELTIME
12459 "reltime",
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef FEAT_QUICKFIX
12462 "quickfix",
12463#endif
12464#ifdef FEAT_RIGHTLEFT
12465 "rightleft",
12466#endif
12467#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12468 "ruby",
12469#endif
12470#ifdef FEAT_SCROLLBIND
12471 "scrollbind",
12472#endif
12473#ifdef FEAT_CMDL_INFO
12474 "showcmd",
12475 "cmdline_info",
12476#endif
12477#ifdef FEAT_SIGNS
12478 "signs",
12479#endif
12480#ifdef FEAT_SMARTINDENT
12481 "smartindent",
12482#endif
12483#ifdef FEAT_SNIFF
12484 "sniff",
12485#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012486#ifdef STARTUPTIME
12487 "startuptime",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_STL_OPT
12490 "statusline",
12491#endif
12492#ifdef FEAT_SUN_WORKSHOP
12493 "sun_workshop",
12494#endif
12495#ifdef FEAT_NETBEANS_INTG
12496 "netbeans_intg",
12497#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012498#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012499 "spell",
12500#endif
12501#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 "syntax",
12503#endif
12504#if defined(USE_SYSTEM) || !defined(UNIX)
12505 "system",
12506#endif
12507#ifdef FEAT_TAG_BINS
12508 "tag_binary",
12509#endif
12510#ifdef FEAT_TAG_OLDSTATIC
12511 "tag_old_static",
12512#endif
12513#ifdef FEAT_TAG_ANYWHITE
12514 "tag_any_white",
12515#endif
12516#ifdef FEAT_TCL
12517# ifndef DYNAMIC_TCL
12518 "tcl",
12519# endif
12520#endif
12521#ifdef TERMINFO
12522 "terminfo",
12523#endif
12524#ifdef FEAT_TERMRESPONSE
12525 "termresponse",
12526#endif
12527#ifdef FEAT_TEXTOBJ
12528 "textobjects",
12529#endif
12530#ifdef HAVE_TGETENT
12531 "tgetent",
12532#endif
12533#ifdef FEAT_TITLE
12534 "title",
12535#endif
12536#ifdef FEAT_TOOLBAR
12537 "toolbar",
12538#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012539#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12540 "unnamedplus",
12541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542#ifdef FEAT_USR_CMDS
12543 "user-commands", /* was accidentally included in 5.4 */
12544 "user_commands",
12545#endif
12546#ifdef FEAT_VIMINFO
12547 "viminfo",
12548#endif
12549#ifdef FEAT_VERTSPLIT
12550 "vertsplit",
12551#endif
12552#ifdef FEAT_VIRTUALEDIT
12553 "virtualedit",
12554#endif
12555#ifdef FEAT_VISUAL
12556 "visual",
12557#endif
12558#ifdef FEAT_VISUALEXTRA
12559 "visualextra",
12560#endif
12561#ifdef FEAT_VREPLACE
12562 "vreplace",
12563#endif
12564#ifdef FEAT_WILDIGN
12565 "wildignore",
12566#endif
12567#ifdef FEAT_WILDMENU
12568 "wildmenu",
12569#endif
12570#ifdef FEAT_WINDOWS
12571 "windows",
12572#endif
12573#ifdef FEAT_WAK
12574 "winaltkeys",
12575#endif
12576#ifdef FEAT_WRITEBACKUP
12577 "writebackup",
12578#endif
12579#ifdef FEAT_XIM
12580 "xim",
12581#endif
12582#ifdef FEAT_XFONTSET
12583 "xfontset",
12584#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012585#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012586 "xpm",
12587 "xpm_w32", /* for backward compatibility */
12588#else
12589# if defined(HAVE_XPM)
12590 "xpm",
12591# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593#ifdef USE_XSMP
12594 "xsmp",
12595#endif
12596#ifdef USE_XSMP_INTERACT
12597 "xsmp_interact",
12598#endif
12599#ifdef FEAT_XCLIPBOARD
12600 "xterm_clipboard",
12601#endif
12602#ifdef FEAT_XTERM_SAVE
12603 "xterm_save",
12604#endif
12605#if defined(UNIX) && defined(FEAT_X11)
12606 "X11",
12607#endif
12608 NULL
12609 };
12610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 for (i = 0; has_list[i] != NULL; ++i)
12613 if (STRICMP(name, has_list[i]) == 0)
12614 {
12615 n = TRUE;
12616 break;
12617 }
12618
12619 if (n == FALSE)
12620 {
12621 if (STRNICMP(name, "patch", 5) == 0)
12622 n = has_patch(atoi((char *)name + 5));
12623 else if (STRICMP(name, "vim_starting") == 0)
12624 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012625#ifdef FEAT_MBYTE
12626 else if (STRICMP(name, "multi_byte_encoding") == 0)
12627 n = has_mbyte;
12628#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012629#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12630 else if (STRICMP(name, "balloon_multiline") == 0)
12631 n = multiline_balloon_available();
12632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633#ifdef DYNAMIC_TCL
12634 else if (STRICMP(name, "tcl") == 0)
12635 n = tcl_enabled(FALSE);
12636#endif
12637#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12638 else if (STRICMP(name, "iconv") == 0)
12639 n = iconv_enabled(FALSE);
12640#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012641#ifdef DYNAMIC_LUA
12642 else if (STRICMP(name, "lua") == 0)
12643 n = lua_enabled(FALSE);
12644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012645#ifdef DYNAMIC_MZSCHEME
12646 else if (STRICMP(name, "mzscheme") == 0)
12647 n = mzscheme_enabled(FALSE);
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef DYNAMIC_RUBY
12650 else if (STRICMP(name, "ruby") == 0)
12651 n = ruby_enabled(FALSE);
12652#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012653#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654#ifdef DYNAMIC_PYTHON
12655 else if (STRICMP(name, "python") == 0)
12656 n = python_enabled(FALSE);
12657#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012658#endif
12659#ifdef FEAT_PYTHON3
12660#ifdef DYNAMIC_PYTHON3
12661 else if (STRICMP(name, "python3") == 0)
12662 n = python3_enabled(FALSE);
12663#endif
12664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665#ifdef DYNAMIC_PERL
12666 else if (STRICMP(name, "perl") == 0)
12667 n = perl_enabled(FALSE);
12668#endif
12669#ifdef FEAT_GUI
12670 else if (STRICMP(name, "gui_running") == 0)
12671 n = (gui.in_use || gui.starting);
12672# ifdef FEAT_GUI_W32
12673 else if (STRICMP(name, "gui_win32s") == 0)
12674 n = gui_is_win32s();
12675# endif
12676# ifdef FEAT_BROWSE
12677 else if (STRICMP(name, "browse") == 0)
12678 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12679# endif
12680#endif
12681#ifdef FEAT_SYN_HL
12682 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012683 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684#endif
12685#if defined(WIN3264)
12686 else if (STRICMP(name, "win95") == 0)
12687 n = mch_windows95();
12688#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012689#ifdef FEAT_NETBEANS_INTG
12690 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012691 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 }
12694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696}
12697
12698/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 * "has_key()" function
12700 */
12701 static void
12702f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 typval_T *argvars;
12704 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012706 if (argvars[0].v_type != VAR_DICT)
12707 {
12708 EMSG(_(e_dictreq));
12709 return;
12710 }
12711 if (argvars[0].vval.v_dict == NULL)
12712 return;
12713
12714 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012715 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012716}
12717
12718/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012719 * "haslocaldir()" function
12720 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012721 static void
12722f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012723 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012724 typval_T *rettv;
12725{
12726 rettv->vval.v_number = (curwin->w_localdir != NULL);
12727}
12728
12729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 * "hasmapto()" function
12731 */
12732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *argvars;
12735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
12737 char_u *name;
12738 char_u *mode;
12739 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012740 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 mode = (char_u *)"nvo";
12745 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012748 if (argvars[2].v_type != VAR_UNKNOWN)
12749 abbr = get_tv_number(&argvars[2]);
12750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaar2c932302006-03-18 21:42:09 +000012752 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758/*
12759 * "histadd()" function
12760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766#ifdef FEAT_CMDHIST
12767 int histype;
12768 char_u *str;
12769 char_u buf[NUMBUFLEN];
12770#endif
12771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 if (check_restricted() || check_secure())
12774 return;
12775#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12777 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 if (histype >= 0)
12779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 if (*str != NUL)
12782 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012783 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 return;
12787 }
12788 }
12789#endif
12790}
12791
12792/*
12793 * "histdel()" function
12794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012797 typval_T *argvars UNUSED;
12798 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
12800#ifdef FEAT_CMDHIST
12801 int n;
12802 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012803 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12806 if (str == NULL)
12807 n = 0;
12808 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012811 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
12816 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012817 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 get_tv_string_buf(&argvars[1], buf));
12819 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820#endif
12821}
12822
12823/*
12824 * "histget()" function
12825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
12831#ifdef FEAT_CMDHIST
12832 int type;
12833 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12837 if (str == NULL)
12838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012840 {
12841 type = get_histtype(str);
12842 if (argvars[1].v_type == VAR_UNKNOWN)
12843 idx = get_history_idx(type);
12844 else
12845 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12846 /* -1 on type error */
12847 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853}
12854
12855/*
12856 * "histnr()" function
12857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
12863 int i;
12864
12865#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 char_u *history = get_tv_string_chk(&argvars[0]);
12867
12868 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 if (i >= HIST_CMD && i < HIST_COUNT)
12870 i = get_history_idx(i);
12871 else
12872#endif
12873 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875}
12876
12877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 * "highlightID(name)" function
12879 */
12880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012882 typval_T *argvars;
12883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889 * "highlight_exists()" function
12890 */
12891 static void
12892f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012895{
12896 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12897}
12898
12899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 * "hostname()" function
12901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
12907 char_u hostname[256];
12908
12909 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->v_type = VAR_STRING;
12911 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912}
12913
12914/*
12915 * iconv() function
12916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921{
12922#ifdef FEAT_MBYTE
12923 char_u buf1[NUMBUFLEN];
12924 char_u buf2[NUMBUFLEN];
12925 char_u *from, *to, *str;
12926 vimconv_T vimconv;
12927#endif
12928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
12930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 str = get_tv_string(&argvars[0]);
12934 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12935 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 vimconv.vc_type = CONV_NONE;
12937 convert_setup(&vimconv, from, to);
12938
12939 /* If the encodings are equal, no conversion needed. */
12940 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
12945 convert_setup(&vimconv, NULL, NULL);
12946 vim_free(from);
12947 vim_free(to);
12948#endif
12949}
12950
12951/*
12952 * "indent()" function
12953 */
12954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *argvars;
12957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
12959 linenr_T lnum;
12960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012968/*
12969 * "index()" function
12970 */
12971 static void
12972f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012973 typval_T *argvars;
12974 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012975{
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 list_T *l;
12977 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012978 long idx = 0;
12979 int ic = FALSE;
12980
12981 rettv->vval.v_number = -1;
12982 if (argvars[0].v_type != VAR_LIST)
12983 {
12984 EMSG(_(e_listreq));
12985 return;
12986 }
12987 l = argvars[0].vval.v_list;
12988 if (l != NULL)
12989 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012990 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 int error = FALSE;
12994
Bram Moolenaar758711c2005-02-02 23:11:38 +000012995 /* Start at specified item. Use the cached index that list_find()
12996 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012997 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012998 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012999 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 ic = get_tv_number_chk(&argvars[3], &error);
13001 if (error)
13002 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013003 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013004
Bram Moolenaar758711c2005-02-02 23:11:38 +000013005 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013006 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013007 {
13008 rettv->vval.v_number = idx;
13009 break;
13010 }
13011 }
13012}
13013
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014static int inputsecret_flag = 0;
13015
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013016static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13017
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013019 * This function is used by f_input() and f_inputdialog() functions. The third
13020 * argument to f_input() specifies the type of completion to use at the
13021 * prompt. The third argument to f_inputdialog() specifies the value to return
13022 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 */
13024 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013025get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013026 typval_T *argvars;
13027 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013028 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 char_u *p = NULL;
13032 int c;
13033 char_u buf[NUMBUFLEN];
13034 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013036 int xp_type = EXPAND_NOTHING;
13037 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013040 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
13042#ifdef NO_CONSOLE_INPUT
13043 /* While starting up, there is no place to enter text. */
13044 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046#endif
13047
13048 cmd_silent = FALSE; /* Want to see the prompt. */
13049 if (prompt != NULL)
13050 {
13051 /* Only the part of the message after the last NL is considered as
13052 * prompt for the command line */
13053 p = vim_strrchr(prompt, '\n');
13054 if (p == NULL)
13055 p = prompt;
13056 else
13057 {
13058 ++p;
13059 c = *p;
13060 *p = NUL;
13061 msg_start();
13062 msg_clr_eos();
13063 msg_puts_attr(prompt, echo_attr);
13064 msg_didout = FALSE;
13065 msg_starthere();
13066 *p = c;
13067 }
13068 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 if (argvars[1].v_type != VAR_UNKNOWN)
13071 {
13072 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13073 if (defstr != NULL)
13074 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013076 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013077 {
13078 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013079 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013080 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013081
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013082 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013083 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013084
Bram Moolenaar4463f292005-09-25 22:20:24 +000013085 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13086 if (xp_name == NULL)
13087 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013089 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013090
Bram Moolenaar4463f292005-09-25 22:20:24 +000013091 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13092 &xp_arg) == FAIL)
13093 return;
13094 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013095 }
13096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013098 {
13099# ifdef FEAT_EX_EXTRA
13100 int save_ex_normal_busy = ex_normal_busy;
13101 ex_normal_busy = 0;
13102# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013104 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13105 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013106# ifdef FEAT_EX_EXTRA
13107 ex_normal_busy = save_ex_normal_busy;
13108# endif
13109 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013110 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013111 && argvars[1].v_type != VAR_UNKNOWN
13112 && argvars[2].v_type != VAR_UNKNOWN)
13113 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13114 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013115
13116 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 /* since the user typed this, no need to wait for return */
13119 need_wait_return = FALSE;
13120 msg_didout = FALSE;
13121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 cmd_silent = cmd_silent_save;
13123}
13124
13125/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013126 * "input()" function
13127 * Also handles inputsecret() when inputsecret is set.
13128 */
13129 static void
13130f_input(argvars, rettv)
13131 typval_T *argvars;
13132 typval_T *rettv;
13133{
13134 get_user_input(argvars, rettv, FALSE);
13135}
13136
13137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 * "inputdialog()" function
13139 */
13140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *argvars;
13143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145#if defined(FEAT_GUI_TEXTDIALOG)
13146 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13147 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13148 {
13149 char_u *message;
13150 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 message = get_tv_string_chk(&argvars[0]);
13154 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013155 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013156 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 else
13158 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 if (message != NULL && defstr != NULL
13160 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013161 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 else
13164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 if (message != NULL && defstr != NULL
13166 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_string = vim_strsave(
13169 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175 else
13176#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013177 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013180/*
13181 * "inputlist()" function
13182 */
13183 static void
13184f_inputlist(argvars, rettv)
13185 typval_T *argvars;
13186 typval_T *rettv;
13187{
13188 listitem_T *li;
13189 int selected;
13190 int mouse_used;
13191
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013192#ifdef NO_CONSOLE_INPUT
13193 /* While starting up, there is no place to enter text. */
13194 if (no_console_input())
13195 return;
13196#endif
13197 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13198 {
13199 EMSG2(_(e_listarg), "inputlist()");
13200 return;
13201 }
13202
13203 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013204 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013205 lines_left = Rows; /* avoid more prompt */
13206 msg_scroll = TRUE;
13207 msg_clr_eos();
13208
13209 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13210 {
13211 msg_puts(get_tv_string(&li->li_tv));
13212 msg_putchar('\n');
13213 }
13214
13215 /* Ask for choice. */
13216 selected = prompt_for_number(&mouse_used);
13217 if (mouse_used)
13218 selected -= lines_left;
13219
13220 rettv->vval.v_number = selected;
13221}
13222
13223
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13225
13226/*
13227 * "inputrestore()" function
13228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013231 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
13234 if (ga_userinput.ga_len > 0)
13235 {
13236 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13238 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013239 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 }
13241 else if (p_verbose > 1)
13242 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013243 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 }
13246}
13247
13248/*
13249 * "inputsave()" function
13250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013256 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 if (ga_grow(&ga_userinput, 1) == OK)
13258 {
13259 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13260 + ga_userinput.ga_len);
13261 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013262 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
13264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266}
13267
13268/*
13269 * "inputsecret()" function
13270 */
13271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
13276 ++cmdline_star;
13277 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 --cmdline_star;
13280 --inputsecret_flag;
13281}
13282
13283/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284 * "insert()" function
13285 */
13286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290{
13291 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 listitem_T *item;
13293 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013295
13296 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013297 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013298 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013299 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013300 {
13301 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 before = get_tv_number_chk(&argvars[2], &error);
13303 if (error)
13304 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013305
Bram Moolenaar758711c2005-02-02 23:11:38 +000013306 if (before == l->lv_len)
13307 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 else
13309 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013310 item = list_find(l, before);
13311 if (item == NULL)
13312 {
13313 EMSGN(_(e_listidx), before);
13314 l = NULL;
13315 }
13316 }
13317 if (l != NULL)
13318 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013319 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013320 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013321 }
13322 }
13323}
13324
13325/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013326 * "invert(expr)" function
13327 */
13328 static void
13329f_invert(argvars, rettv)
13330 typval_T *argvars;
13331 typval_T *rettv;
13332{
13333 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13334}
13335
13336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 * "isdirectory()" function
13338 */
13339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013341 typval_T *argvars;
13342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345}
13346
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 * "islocked()" function
13349 */
13350 static void
13351f_islocked(argvars, rettv)
13352 typval_T *argvars;
13353 typval_T *rettv;
13354{
13355 lval_T lv;
13356 char_u *end;
13357 dictitem_T *di;
13358
13359 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013360 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13361 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013362 if (end != NULL && lv.ll_name != NULL)
13363 {
13364 if (*end != NUL)
13365 EMSG(_(e_trailing));
13366 else
13367 {
13368 if (lv.ll_tv == NULL)
13369 {
13370 if (check_changedtick(lv.ll_name))
13371 rettv->vval.v_number = 1; /* always locked */
13372 else
13373 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013374 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013375 if (di != NULL)
13376 {
13377 /* Consider a variable locked when:
13378 * 1. the variable itself is locked
13379 * 2. the value of the variable is locked.
13380 * 3. the List or Dict value is locked.
13381 */
13382 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13383 || tv_islocked(&di->di_tv));
13384 }
13385 }
13386 }
13387 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013388 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013389 else if (lv.ll_newkey != NULL)
13390 EMSG2(_(e_dictkey), lv.ll_newkey);
13391 else if (lv.ll_list != NULL)
13392 /* List item. */
13393 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13394 else
13395 /* Dictionary item. */
13396 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13397 }
13398 }
13399
13400 clear_lval(&lv);
13401}
13402
Bram Moolenaar33570922005-01-25 22:26:29 +000013403static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013404
13405/*
13406 * Turn a dict into a list:
13407 * "what" == 0: list of keys
13408 * "what" == 1: list of values
13409 * "what" == 2: list of items
13410 */
13411 static void
13412dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415 int what;
13416{
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 list_T *l2;
13418 dictitem_T *di;
13419 hashitem_T *hi;
13420 listitem_T *li;
13421 listitem_T *li2;
13422 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013423 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013424
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425 if (argvars[0].v_type != VAR_DICT)
13426 {
13427 EMSG(_(e_dictreq));
13428 return;
13429 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013430 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431 return;
13432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013436 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013441 --todo;
13442 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013444 li = listitem_alloc();
13445 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013446 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013447 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013448
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013449 if (what == 0)
13450 {
13451 /* keys() */
13452 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013453 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013454 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13455 }
13456 else if (what == 1)
13457 {
13458 /* values() */
13459 copy_tv(&di->di_tv, &li->li_tv);
13460 }
13461 else
13462 {
13463 /* items() */
13464 l2 = list_alloc();
13465 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013466 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013467 li->li_tv.vval.v_list = l2;
13468 if (l2 == NULL)
13469 break;
13470 ++l2->lv_refcount;
13471
13472 li2 = listitem_alloc();
13473 if (li2 == NULL)
13474 break;
13475 list_append(l2, li2);
13476 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013477 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013478 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13479
13480 li2 = listitem_alloc();
13481 if (li2 == NULL)
13482 break;
13483 list_append(l2, li2);
13484 copy_tv(&di->di_tv, &li2->li_tv);
13485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013486 }
13487 }
13488}
13489
13490/*
13491 * "items(dict)" function
13492 */
13493 static void
13494f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T *argvars;
13496 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013497{
13498 dict_list(argvars, rettv, 2);
13499}
13500
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013502 * "join()" function
13503 */
13504 static void
13505f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 typval_T *argvars;
13507 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013508{
13509 garray_T ga;
13510 char_u *sep;
13511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 if (argvars[0].v_type != VAR_LIST)
13513 {
13514 EMSG(_(e_listreq));
13515 return;
13516 }
13517 if (argvars[0].vval.v_list == NULL)
13518 return;
13519 if (argvars[1].v_type == VAR_UNKNOWN)
13520 sep = (char_u *)" ";
13521 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013523
13524 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525
13526 if (sep != NULL)
13527 {
13528 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013529 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 ga_append(&ga, NUL);
13531 rettv->vval.v_string = (char_u *)ga.ga_data;
13532 }
13533 else
13534 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013535}
13536
13537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013538 * "keys()" function
13539 */
13540 static void
13541f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013542 typval_T *argvars;
13543 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013544{
13545 dict_list(argvars, rettv, 0);
13546}
13547
13548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 * "last_buffer_nr()" function.
13550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556 int n = 0;
13557 buf_T *buf;
13558
13559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13560 if (n < buf->b_fnum)
13561 n = buf->b_fnum;
13562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013564}
13565
13566/*
13567 * "len()" function
13568 */
13569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573{
13574 switch (argvars[0].v_type)
13575 {
13576 case VAR_STRING:
13577 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->vval.v_number = (varnumber_T)STRLEN(
13579 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013580 break;
13581 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013584 case VAR_DICT:
13585 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13586 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013587 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013588 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 break;
13590 }
13591}
13592
Bram Moolenaar33570922005-01-25 22:26:29 +000013593static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013594
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013599 int type;
13600{
13601#ifdef FEAT_LIBCALL
13602 char_u *string_in;
13603 char_u **string_result;
13604 int nr_result;
13605#endif
13606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013608 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013610
13611 if (check_restricted() || check_secure())
13612 return;
13613
13614#ifdef FEAT_LIBCALL
13615 /* The first two args must be strings, otherwise its meaningless */
13616 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13617 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013618 string_in = NULL;
13619 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013620 string_in = argvars[2].vval.v_string;
13621 if (type == VAR_NUMBER)
13622 string_result = NULL;
13623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625 if (mch_libcall(argvars[0].vval.v_string,
13626 argvars[1].vval.v_string,
13627 string_in,
13628 argvars[2].vval.v_number,
13629 string_result,
13630 &nr_result) == OK
13631 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013633 }
13634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635}
13636
13637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 * "libcall()" function
13639 */
13640 static void
13641f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *argvars;
13643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644{
13645 libcall_common(argvars, rettv, VAR_STRING);
13646}
13647
13648/*
13649 * "libcallnr()" function
13650 */
13651 static void
13652f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013653 typval_T *argvars;
13654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655{
13656 libcall_common(argvars, rettv, VAR_NUMBER);
13657}
13658
13659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 * "line(string)" function
13661 */
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667 linenr_T lnum = 0;
13668 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013669 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013671 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 if (fp != NULL)
13673 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675}
13676
13677/*
13678 * "line2byte(lnum)" function
13679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013682 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684{
13685#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687#else
13688 linenr_T lnum;
13689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13695 if (rettv->vval.v_number >= 0)
13696 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#endif
13698}
13699
13700/*
13701 * "lispindent(lnum)" function
13702 */
13703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707{
13708#ifdef FEAT_LISP
13709 pos_T pos;
13710 linenr_T lnum;
13711
13712 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13715 {
13716 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 curwin->w_cursor = pos;
13719 }
13720 else
13721#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723}
13724
13725/*
13726 * "localtime()" function
13727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
Bram Moolenaar33570922005-01-25 22:26:29 +000013736static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
13738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
13741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 int exact;
13743{
13744 char_u *keys;
13745 char_u *which;
13746 char_u buf[NUMBUFLEN];
13747 char_u *keys_buf = NULL;
13748 char_u *rhs;
13749 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013750 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013751 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013752 mapblock_T *mp;
13753 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
13755 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 rettv->v_type = VAR_STRING;
13757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 if (*keys == NUL)
13761 return;
13762
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013766 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013767 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013768 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013769 if (argvars[3].v_type != VAR_UNKNOWN)
13770 get_dict = get_tv_number(&argvars[3]);
13771 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 else
13774 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775 if (which == NULL)
13776 return;
13777
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 mode = get_map_mode(&which, 0);
13779
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013780 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013781 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013783
13784 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013786 /* Return a string. */
13787 if (rhs != NULL)
13788 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789
Bram Moolenaarbd743252010-10-20 21:23:33 +020013790 }
13791 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13792 {
13793 /* Return a dictionary. */
13794 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13795 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13796 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797
Bram Moolenaarbd743252010-10-20 21:23:33 +020013798 dict_add_nr_str(dict, "lhs", 0L, lhs);
13799 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13800 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13801 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13802 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13803 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13804 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013805 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013806 dict_add_nr_str(dict, "mode", 0L, mapmode);
13807
13808 vim_free(lhs);
13809 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 }
13811}
13812
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013813#ifdef FEAT_FLOAT
13814/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013815 * "log()" function
13816 */
13817 static void
13818f_log(argvars, rettv)
13819 typval_T *argvars;
13820 typval_T *rettv;
13821{
13822 float_T f;
13823
13824 rettv->v_type = VAR_FLOAT;
13825 if (get_float_arg(argvars, &f) == OK)
13826 rettv->vval.v_float = log(f);
13827 else
13828 rettv->vval.v_float = 0.0;
13829}
13830
13831/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013832 * "log10()" function
13833 */
13834 static void
13835f_log10(argvars, rettv)
13836 typval_T *argvars;
13837 typval_T *rettv;
13838{
13839 float_T f;
13840
13841 rettv->v_type = VAR_FLOAT;
13842 if (get_float_arg(argvars, &f) == OK)
13843 rettv->vval.v_float = log10(f);
13844 else
13845 rettv->vval.v_float = 0.0;
13846}
13847#endif
13848
Bram Moolenaar1dced572012-04-05 16:54:08 +020013849#ifdef FEAT_LUA
13850/*
13851 * "luaeval()" function
13852 */
13853 static void
13854f_luaeval(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 char_u *str;
13859 char_u buf[NUMBUFLEN];
13860
13861 str = get_tv_string_buf(&argvars[0], buf);
13862 do_luaeval(str, argvars + 1, rettv);
13863}
13864#endif
13865
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013867 * "map()" function
13868 */
13869 static void
13870f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013873{
13874 filter_map(argvars, rettv, TRUE);
13875}
13876
13877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 */
13880 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013881f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
13883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886}
13887
13888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013889 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 */
13891 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013892f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *argvars;
13894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013896 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897}
13898
Bram Moolenaar33570922005-01-25 22:26:29 +000013899static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900
13901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 typval_T *argvars;
13904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 int type;
13906{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013907 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013908 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 char_u *pat;
13911 regmatch_T regmatch;
13912 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013913 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 char_u *save_cpo;
13915 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013916 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013917 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013918 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013919 list_T *l = NULL;
13920 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013921 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923
13924 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13925 save_cpo = p_cpo;
13926 p_cpo = (char_u *)"";
13927
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013928 rettv->vval.v_number = -1;
13929 if (type == 3)
13930 {
13931 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013932 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013933 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013934 }
13935 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->v_type = VAR_STRING;
13938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013941 if (argvars[0].v_type == VAR_LIST)
13942 {
13943 if ((l = argvars[0].vval.v_list) == NULL)
13944 goto theend;
13945 li = l->lv_first;
13946 }
13947 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013948 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013949 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013950 len = (long)STRLEN(str);
13951 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013953 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13954 if (pat == NULL)
13955 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013959 int error = FALSE;
13960
13961 start = get_tv_number_chk(&argvars[2], &error);
13962 if (error)
13963 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013964 if (l != NULL)
13965 {
13966 li = list_find(l, start);
13967 if (li == NULL)
13968 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013969 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 }
13971 else
13972 {
13973 if (start < 0)
13974 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013975 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013976 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013977 /* When "count" argument is there ignore matches before "start",
13978 * otherwise skip part of the string. Differs when pattern is "^"
13979 * or "\<". */
13980 if (argvars[3].v_type != VAR_UNKNOWN)
13981 startcol = start;
13982 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013983 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013984 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013985 len -= start;
13986 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013987 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013988
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 nth = get_tv_number_chk(&argvars[3], &error);
13991 if (error)
13992 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 }
13994
13995 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13996 if (regmatch.regprog != NULL)
13997 {
13998 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013999
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014000 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014001 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 if (l != NULL)
14003 {
14004 if (li == NULL)
14005 {
14006 match = FALSE;
14007 break;
14008 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014009 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014010 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014011 if (str == NULL)
14012 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013 }
14014
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014015 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014017 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014018 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019 if (l == NULL && !match)
14020 break;
14021
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014022 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014023 if (l != NULL)
14024 {
14025 li = li->li_next;
14026 ++idx;
14027 }
14028 else
14029 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014030#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014031 startcol = (colnr_T)(regmatch.startp[0]
14032 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014033#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014034 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014035#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014036 if (startcol > (colnr_T)len
14037 || str + startcol <= regmatch.startp[0])
14038 {
14039 match = FALSE;
14040 break;
14041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014042 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014043 }
14044
14045 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014047 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014049 int i;
14050
14051 /* return list with matched string and submatches */
14052 for (i = 0; i < NSUBEXP; ++i)
14053 {
14054 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014055 {
14056 if (list_append_string(rettv->vval.v_list,
14057 (char_u *)"", 0) == FAIL)
14058 break;
14059 }
14060 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014061 regmatch.startp[i],
14062 (int)(regmatch.endp[i] - regmatch.startp[i]))
14063 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014064 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014065 }
14066 }
14067 else if (type == 2)
14068 {
14069 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014070 if (l != NULL)
14071 copy_tv(&li->li_tv, rettv);
14072 else
14073 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014075 }
14076 else if (l != NULL)
14077 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 else
14079 {
14080 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014081 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 (varnumber_T)(regmatch.startp[0] - str);
14083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014086 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 }
14088 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014089 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 }
14091
14092theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014093 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 p_cpo = save_cpo;
14095}
14096
14097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 * "match()" function
14099 */
14100 static void
14101f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *argvars;
14103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104{
14105 find_some_match(argvars, rettv, 1);
14106}
14107
14108/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014109 * "matchadd()" function
14110 */
14111 static void
14112f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014113 typval_T *argvars UNUSED;
14114 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014115{
14116#ifdef FEAT_SEARCH_EXTRA
14117 char_u buf[NUMBUFLEN];
14118 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14119 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14120 int prio = 10; /* default priority */
14121 int id = -1;
14122 int error = FALSE;
14123
14124 rettv->vval.v_number = -1;
14125
14126 if (grp == NULL || pat == NULL)
14127 return;
14128 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014129 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014130 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014131 if (argvars[3].v_type != VAR_UNKNOWN)
14132 id = get_tv_number_chk(&argvars[3], &error);
14133 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014134 if (error == TRUE)
14135 return;
14136 if (id >= 1 && id <= 3)
14137 {
14138 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14139 return;
14140 }
14141
14142 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14143#endif
14144}
14145
14146/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014147 * "matcharg()" function
14148 */
14149 static void
14150f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014151 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014152 typval_T *rettv;
14153{
14154 if (rettv_list_alloc(rettv) == OK)
14155 {
14156#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014157 int id = get_tv_number(&argvars[0]);
14158 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014159
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014160 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014161 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014162 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14163 {
14164 list_append_string(rettv->vval.v_list,
14165 syn_id2name(m->hlg_id), -1);
14166 list_append_string(rettv->vval.v_list, m->pattern, -1);
14167 }
14168 else
14169 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014170 list_append_string(rettv->vval.v_list, NULL, -1);
14171 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014172 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014173 }
14174#endif
14175 }
14176}
14177
14178/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014179 * "matchdelete()" function
14180 */
14181 static void
14182f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014183 typval_T *argvars UNUSED;
14184 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014185{
14186#ifdef FEAT_SEARCH_EXTRA
14187 rettv->vval.v_number = match_delete(curwin,
14188 (int)get_tv_number(&argvars[0]), TRUE);
14189#endif
14190}
14191
14192/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193 * "matchend()" function
14194 */
14195 static void
14196f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014197 typval_T *argvars;
14198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199{
14200 find_some_match(argvars, rettv, 0);
14201}
14202
14203/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014204 * "matchlist()" function
14205 */
14206 static void
14207f_matchlist(argvars, rettv)
14208 typval_T *argvars;
14209 typval_T *rettv;
14210{
14211 find_some_match(argvars, rettv, 3);
14212}
14213
14214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215 * "matchstr()" function
14216 */
14217 static void
14218f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221{
14222 find_some_match(argvars, rettv, 2);
14223}
14224
Bram Moolenaar33570922005-01-25 22:26:29 +000014225static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014226
14227 static void
14228max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014231 int domax;
14232{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014233 long n = 0;
14234 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014236
14237 if (argvars[0].v_type == VAR_LIST)
14238 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 list_T *l;
14240 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014241
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014242 l = argvars[0].vval.v_list;
14243 if (l != NULL)
14244 {
14245 li = l->lv_first;
14246 if (li != NULL)
14247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014249 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014250 {
14251 li = li->li_next;
14252 if (li == NULL)
14253 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014255 if (domax ? i > n : i < n)
14256 n = i;
14257 }
14258 }
14259 }
14260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261 else if (argvars[0].v_type == VAR_DICT)
14262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014263 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014264 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014266 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014267
14268 d = argvars[0].vval.v_dict;
14269 if (d != NULL)
14270 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014271 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014273 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014274 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014276 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014278 if (first)
14279 {
14280 n = i;
14281 first = FALSE;
14282 }
14283 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014284 n = i;
14285 }
14286 }
14287 }
14288 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014289 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014290 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014292}
14293
14294/*
14295 * "max()" function
14296 */
14297 static void
14298f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014299 typval_T *argvars;
14300 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014301{
14302 max_min(argvars, rettv, TRUE);
14303}
14304
14305/*
14306 * "min()" function
14307 */
14308 static void
14309f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 typval_T *argvars;
14311 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014312{
14313 max_min(argvars, rettv, FALSE);
14314}
14315
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014316static int mkdir_recurse __ARGS((char_u *dir, int prot));
14317
14318/*
14319 * Create the directory in which "dir" is located, and higher levels when
14320 * needed.
14321 */
14322 static int
14323mkdir_recurse(dir, prot)
14324 char_u *dir;
14325 int prot;
14326{
14327 char_u *p;
14328 char_u *updir;
14329 int r = FAIL;
14330
14331 /* Get end of directory name in "dir".
14332 * We're done when it's "/" or "c:/". */
14333 p = gettail_sep(dir);
14334 if (p <= get_past_head(dir))
14335 return OK;
14336
14337 /* If the directory exists we're done. Otherwise: create it.*/
14338 updir = vim_strnsave(dir, (int)(p - dir));
14339 if (updir == NULL)
14340 return FAIL;
14341 if (mch_isdir(updir))
14342 r = OK;
14343 else if (mkdir_recurse(updir, prot) == OK)
14344 r = vim_mkdir_emsg(updir, prot);
14345 vim_free(updir);
14346 return r;
14347}
14348
14349#ifdef vim_mkdir
14350/*
14351 * "mkdir()" function
14352 */
14353 static void
14354f_mkdir(argvars, rettv)
14355 typval_T *argvars;
14356 typval_T *rettv;
14357{
14358 char_u *dir;
14359 char_u buf[NUMBUFLEN];
14360 int prot = 0755;
14361
14362 rettv->vval.v_number = FAIL;
14363 if (check_restricted() || check_secure())
14364 return;
14365
14366 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014367 if (*dir == NUL)
14368 rettv->vval.v_number = FAIL;
14369 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014370 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014371 if (*gettail(dir) == NUL)
14372 /* remove trailing slashes */
14373 *gettail_sep(dir) = NUL;
14374
14375 if (argvars[1].v_type != VAR_UNKNOWN)
14376 {
14377 if (argvars[2].v_type != VAR_UNKNOWN)
14378 prot = get_tv_number_chk(&argvars[2], NULL);
14379 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14380 mkdir_recurse(dir, prot);
14381 }
14382 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014383 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014384}
14385#endif
14386
Bram Moolenaar0d660222005-01-07 21:51:51 +000014387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 * "mode()" function
14389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 typval_T *argvars;
14393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395 char_u buf[3];
14396
14397 buf[1] = NUL;
14398 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
14400#ifdef FEAT_VISUAL
14401 if (VIsual_active)
14402 {
14403 if (VIsual_select)
14404 buf[0] = VIsual_mode + 's' - 'v';
14405 else
14406 buf[0] = VIsual_mode;
14407 }
14408 else
14409#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014410 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14411 || State == CONFIRM)
14412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014414 if (State == ASKMORE)
14415 buf[1] = 'm';
14416 else if (State == CONFIRM)
14417 buf[1] = '?';
14418 }
14419 else if (State == EXTERNCMD)
14420 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 else if (State & INSERT)
14422 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014423#ifdef FEAT_VREPLACE
14424 if (State & VREPLACE_FLAG)
14425 {
14426 buf[0] = 'R';
14427 buf[1] = 'v';
14428 }
14429 else
14430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 if (State & REPLACE_FLAG)
14432 buf[0] = 'R';
14433 else
14434 buf[0] = 'i';
14435 }
14436 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014439 if (exmode_active)
14440 buf[1] = 'v';
14441 }
14442 else if (exmode_active)
14443 {
14444 buf[0] = 'c';
14445 buf[1] = 'e';
14446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014450 if (finish_op)
14451 buf[1] = 'o';
14452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014454 /* Clear out the minor mode when the argument is not a non-zero number or
14455 * non-empty string. */
14456 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014457 buf[1] = NUL;
14458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->vval.v_string = vim_strsave(buf);
14460 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461}
14462
Bram Moolenaar429fa852013-04-15 12:27:36 +020014463#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014464/*
14465 * "mzeval()" function
14466 */
14467 static void
14468f_mzeval(argvars, rettv)
14469 typval_T *argvars;
14470 typval_T *rettv;
14471{
14472 char_u *str;
14473 char_u buf[NUMBUFLEN];
14474
14475 str = get_tv_string_buf(&argvars[0], buf);
14476 do_mzeval(str, rettv);
14477}
Bram Moolenaar75676462013-01-30 14:55:42 +010014478
14479 void
14480mzscheme_call_vim(name, args, rettv)
14481 char_u *name;
14482 typval_T *args;
14483 typval_T *rettv;
14484{
14485 typval_T argvars[3];
14486
14487 argvars[0].v_type = VAR_STRING;
14488 argvars[0].vval.v_string = name;
14489 copy_tv(args, &argvars[1]);
14490 argvars[2].v_type = VAR_UNKNOWN;
14491 f_call(argvars, rettv);
14492 clear_tv(&argvars[1]);
14493}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014494#endif
14495
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497 * "nextnonblank()" function
14498 */
14499 static void
14500f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503{
14504 linenr_T lnum;
14505
14506 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014508 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014509 {
14510 lnum = 0;
14511 break;
14512 }
14513 if (*skipwhite(ml_get(lnum)) != NUL)
14514 break;
14515 }
14516 rettv->vval.v_number = lnum;
14517}
14518
14519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 * "nr2char()" function
14521 */
14522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
14527 char_u buf[NUMBUFLEN];
14528
14529#ifdef FEAT_MBYTE
14530 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014531 {
14532 int utf8 = 0;
14533
14534 if (argvars[1].v_type != VAR_UNKNOWN)
14535 utf8 = get_tv_number_chk(&argvars[1], NULL);
14536 if (utf8)
14537 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14538 else
14539 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 else
14542#endif
14543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 buf[1] = NUL;
14546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->v_type = VAR_STRING;
14548 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014549}
14550
14551/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014552 * "or(expr, expr)" function
14553 */
14554 static void
14555f_or(argvars, rettv)
14556 typval_T *argvars;
14557 typval_T *rettv;
14558{
14559 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14560 | get_tv_number_chk(&argvars[1], NULL);
14561}
14562
14563/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014564 * "pathshorten()" function
14565 */
14566 static void
14567f_pathshorten(argvars, rettv)
14568 typval_T *argvars;
14569 typval_T *rettv;
14570{
14571 char_u *p;
14572
14573 rettv->v_type = VAR_STRING;
14574 p = get_tv_string_chk(&argvars[0]);
14575 if (p == NULL)
14576 rettv->vval.v_string = NULL;
14577 else
14578 {
14579 p = vim_strsave(p);
14580 rettv->vval.v_string = p;
14581 if (p != NULL)
14582 shorten_dir(p);
14583 }
14584}
14585
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014586#ifdef FEAT_FLOAT
14587/*
14588 * "pow()" function
14589 */
14590 static void
14591f_pow(argvars, rettv)
14592 typval_T *argvars;
14593 typval_T *rettv;
14594{
14595 float_T fx, fy;
14596
14597 rettv->v_type = VAR_FLOAT;
14598 if (get_float_arg(argvars, &fx) == OK
14599 && get_float_arg(&argvars[1], &fy) == OK)
14600 rettv->vval.v_float = pow(fx, fy);
14601 else
14602 rettv->vval.v_float = 0.0;
14603}
14604#endif
14605
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014606/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607 * "prevnonblank()" function
14608 */
14609 static void
14610f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 typval_T *argvars;
14612 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613{
14614 linenr_T lnum;
14615
14616 lnum = get_tv_lnum(argvars);
14617 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14618 lnum = 0;
14619 else
14620 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14621 --lnum;
14622 rettv->vval.v_number = lnum;
14623}
14624
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014625#ifdef HAVE_STDARG_H
14626/* This dummy va_list is here because:
14627 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14628 * - locally in the function results in a "used before set" warning
14629 * - using va_start() to initialize it gives "function with fixed args" error */
14630static va_list ap;
14631#endif
14632
Bram Moolenaar8c711452005-01-14 21:53:12 +000014633/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014634 * "printf()" function
14635 */
14636 static void
14637f_printf(argvars, rettv)
14638 typval_T *argvars;
14639 typval_T *rettv;
14640{
14641 rettv->v_type = VAR_STRING;
14642 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014643#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014644 {
14645 char_u buf[NUMBUFLEN];
14646 int len;
14647 char_u *s;
14648 int saved_did_emsg = did_emsg;
14649 char *fmt;
14650
14651 /* Get the required length, allocate the buffer and do it for real. */
14652 did_emsg = FALSE;
14653 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014654 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014655 if (!did_emsg)
14656 {
14657 s = alloc(len + 1);
14658 if (s != NULL)
14659 {
14660 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014661 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014662 }
14663 }
14664 did_emsg |= saved_did_emsg;
14665 }
14666#endif
14667}
14668
14669/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014670 * "pumvisible()" function
14671 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014672 static void
14673f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014674 typval_T *argvars UNUSED;
14675 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014676{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014677#ifdef FEAT_INS_EXPAND
14678 if (pum_visible())
14679 rettv->vval.v_number = 1;
14680#endif
14681}
14682
Bram Moolenaardb913952012-06-29 12:54:53 +020014683#ifdef FEAT_PYTHON3
14684/*
14685 * "py3eval()" function
14686 */
14687 static void
14688f_py3eval(argvars, rettv)
14689 typval_T *argvars;
14690 typval_T *rettv;
14691{
14692 char_u *str;
14693 char_u buf[NUMBUFLEN];
14694
14695 str = get_tv_string_buf(&argvars[0], buf);
14696 do_py3eval(str, rettv);
14697}
14698#endif
14699
14700#ifdef FEAT_PYTHON
14701/*
14702 * "pyeval()" function
14703 */
14704 static void
14705f_pyeval(argvars, rettv)
14706 typval_T *argvars;
14707 typval_T *rettv;
14708{
14709 char_u *str;
14710 char_u buf[NUMBUFLEN];
14711
14712 str = get_tv_string_buf(&argvars[0], buf);
14713 do_pyeval(str, rettv);
14714}
14715#endif
14716
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014717/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014718 * "range()" function
14719 */
14720 static void
14721f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 typval_T *argvars;
14723 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014724{
14725 long start;
14726 long end;
14727 long stride = 1;
14728 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014729 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014731 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014732 if (argvars[1].v_type == VAR_UNKNOWN)
14733 {
14734 end = start - 1;
14735 start = 0;
14736 }
14737 else
14738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014740 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014742 }
14743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 if (error)
14745 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014746 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014747 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014748 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014749 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014750 else
14751 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014752 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014754 if (list_append_number(rettv->vval.v_list,
14755 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014756 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014757 }
14758}
14759
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014760/*
14761 * "readfile()" function
14762 */
14763 static void
14764f_readfile(argvars, rettv)
14765 typval_T *argvars;
14766 typval_T *rettv;
14767{
14768 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014769 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770 char_u *fname;
14771 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14773 int io_size = sizeof(buf);
14774 int readlen; /* size of last fread() */
14775 char_u *prev = NULL; /* previously read bytes, if any */
14776 long prevlen = 0; /* length of data in prev */
14777 long prevsize = 0; /* size of prev buffer */
14778 long maxline = MAXLNUM;
14779 long cnt = 0;
14780 char_u *p; /* position in buf */
14781 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014782
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014783 if (argvars[1].v_type != VAR_UNKNOWN)
14784 {
14785 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14786 binary = TRUE;
14787 if (argvars[2].v_type != VAR_UNKNOWN)
14788 maxline = get_tv_number(&argvars[2]);
14789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014791 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014793
14794 /* Always open the file in binary mode, library functions have a mind of
14795 * their own about CR-LF conversion. */
14796 fname = get_tv_string(&argvars[0]);
14797 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14798 {
14799 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14800 return;
14801 }
14802
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014803 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014804 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014805 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014806
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014807 /* This for loop processes what was read, but is also entered at end
14808 * of file so that either:
14809 * - an incomplete line gets written
14810 * - a "binary" file gets an empty line at the end if it ends in a
14811 * newline. */
14812 for (p = buf, start = buf;
14813 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14814 ++p)
14815 {
14816 if (*p == '\n' || readlen <= 0)
14817 {
14818 listitem_T *li;
14819 char_u *s = NULL;
14820 long_u len = p - start;
14821
14822 /* Finished a line. Remove CRs before NL. */
14823 if (readlen > 0 && !binary)
14824 {
14825 while (len > 0 && start[len - 1] == '\r')
14826 --len;
14827 /* removal may cross back to the "prev" string */
14828 if (len == 0)
14829 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14830 --prevlen;
14831 }
14832 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014833 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014834 else
14835 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 /* Change "prev" buffer to be the right size. This way
14837 * the bytes are only copied once, and very long lines are
14838 * allocated only once. */
14839 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014841 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014842 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014843 prev = NULL; /* the list will own the string */
14844 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014845 }
14846 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014847 if (s == NULL)
14848 {
14849 do_outofmem_msg((long_u) prevlen + len + 1);
14850 failed = TRUE;
14851 break;
14852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014853
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014854 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 {
14856 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014857 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 break;
14859 }
14860 li->li_tv.v_type = VAR_STRING;
14861 li->li_tv.v_lock = 0;
14862 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014863 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014865 start = p + 1; /* step over newline */
14866 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014867 break;
14868 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 else if (*p == NUL)
14870 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014871#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014872 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14873 * when finding the BF and check the previous two bytes. */
14874 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014875 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014876 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14877 * + 1, these may be in the "prev" string. */
14878 char_u back1 = p >= buf + 1 ? p[-1]
14879 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14880 char_u back2 = p >= buf + 2 ? p[-2]
14881 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14882 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014883
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014884 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014885 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014886 char_u *dest = p - 2;
14887
14888 /* Usually a BOM is at the beginning of a file, and so at
14889 * the beginning of a line; then we can just step over it.
14890 */
14891 if (start == dest)
14892 start = p + 1;
14893 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014894 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014895 /* have to shuffle buf to close gap */
14896 int adjust_prevlen = 0;
14897
14898 if (dest < buf)
14899 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014900 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 dest = buf;
14902 }
14903 if (readlen > p - buf + 1)
14904 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14905 readlen -= 3 - adjust_prevlen;
14906 prevlen -= adjust_prevlen;
14907 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014908 }
14909 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911#endif
14912 } /* for */
14913
14914 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14915 break;
14916 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014917 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014918 /* There's part of a line in buf, store it in "prev". */
14919 if (p - start + prevlen >= prevsize)
14920 {
14921 /* need bigger "prev" buffer */
14922 char_u *newprev;
14923
14924 /* A common use case is ordinary text files and "prev" gets a
14925 * fragment of a line, so the first allocation is made
14926 * small, to avoid repeatedly 'allocing' large and
14927 * 'reallocing' small. */
14928 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014929 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 else
14931 {
14932 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014933 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 prevsize = grow50pc > growmin ? grow50pc : growmin;
14935 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014936 newprev = prev == NULL ? alloc(prevsize)
14937 : vim_realloc(prev, prevsize);
14938 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014939 {
14940 do_outofmem_msg((long_u)prevsize);
14941 failed = TRUE;
14942 break;
14943 }
14944 prev = newprev;
14945 }
14946 /* Add the line part to end of "prev". */
14947 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014948 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014949 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014950 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014951
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014952 /*
14953 * For a negative line count use only the lines at the end of the file,
14954 * free the rest.
14955 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014956 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014957 while (cnt > -maxline)
14958 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014959 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014960 --cnt;
14961 }
14962
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014963 if (failed)
14964 {
14965 list_free(rettv->vval.v_list, TRUE);
14966 /* readfile doc says an empty list is returned on error */
14967 rettv->vval.v_list = list_alloc();
14968 }
14969
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014970 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014971 fclose(fd);
14972}
14973
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014974#if defined(FEAT_RELTIME)
14975static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14976
14977/*
14978 * Convert a List to proftime_T.
14979 * Return FAIL when there is something wrong.
14980 */
14981 static int
14982list2proftime(arg, tm)
14983 typval_T *arg;
14984 proftime_T *tm;
14985{
14986 long n1, n2;
14987 int error = FALSE;
14988
14989 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14990 || arg->vval.v_list->lv_len != 2)
14991 return FAIL;
14992 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14993 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14994# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014995 tm->HighPart = n1;
14996 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014997# else
14998 tm->tv_sec = n1;
14999 tm->tv_usec = n2;
15000# endif
15001 return error ? FAIL : OK;
15002}
15003#endif /* FEAT_RELTIME */
15004
15005/*
15006 * "reltime()" function
15007 */
15008 static void
15009f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015010 typval_T *argvars UNUSED;
15011 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015012{
15013#ifdef FEAT_RELTIME
15014 proftime_T res;
15015 proftime_T start;
15016
15017 if (argvars[0].v_type == VAR_UNKNOWN)
15018 {
15019 /* No arguments: get current time. */
15020 profile_start(&res);
15021 }
15022 else if (argvars[1].v_type == VAR_UNKNOWN)
15023 {
15024 if (list2proftime(&argvars[0], &res) == FAIL)
15025 return;
15026 profile_end(&res);
15027 }
15028 else
15029 {
15030 /* Two arguments: compute the difference. */
15031 if (list2proftime(&argvars[0], &start) == FAIL
15032 || list2proftime(&argvars[1], &res) == FAIL)
15033 return;
15034 profile_sub(&res, &start);
15035 }
15036
15037 if (rettv_list_alloc(rettv) == OK)
15038 {
15039 long n1, n2;
15040
15041# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015042 n1 = res.HighPart;
15043 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015044# else
15045 n1 = res.tv_sec;
15046 n2 = res.tv_usec;
15047# endif
15048 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15049 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15050 }
15051#endif
15052}
15053
15054/*
15055 * "reltimestr()" function
15056 */
15057 static void
15058f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015059 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015060 typval_T *rettv;
15061{
15062#ifdef FEAT_RELTIME
15063 proftime_T tm;
15064#endif
15065
15066 rettv->v_type = VAR_STRING;
15067 rettv->vval.v_string = NULL;
15068#ifdef FEAT_RELTIME
15069 if (list2proftime(&argvars[0], &tm) == OK)
15070 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15071#endif
15072}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015073
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15075static void make_connection __ARGS((void));
15076static int check_connection __ARGS((void));
15077
15078 static void
15079make_connection()
15080{
15081 if (X_DISPLAY == NULL
15082# ifdef FEAT_GUI
15083 && !gui.in_use
15084# endif
15085 )
15086 {
15087 x_force_connect = TRUE;
15088 setup_term_clip();
15089 x_force_connect = FALSE;
15090 }
15091}
15092
15093 static int
15094check_connection()
15095{
15096 make_connection();
15097 if (X_DISPLAY == NULL)
15098 {
15099 EMSG(_("E240: No connection to Vim server"));
15100 return FAIL;
15101 }
15102 return OK;
15103}
15104#endif
15105
15106#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015107static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108
15109 static void
15110remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 typval_T *argvars;
15112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 int expr;
15114{
15115 char_u *server_name;
15116 char_u *keys;
15117 char_u *r = NULL;
15118 char_u buf[NUMBUFLEN];
15119# ifdef WIN32
15120 HWND w;
15121# else
15122 Window w;
15123# endif
15124
15125 if (check_restricted() || check_secure())
15126 return;
15127
15128# ifdef FEAT_X11
15129 if (check_connection() == FAIL)
15130 return;
15131# endif
15132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015133 server_name = get_tv_string_chk(&argvars[0]);
15134 if (server_name == NULL)
15135 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 keys = get_tv_string_buf(&argvars[1], buf);
15137# ifdef WIN32
15138 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15139# else
15140 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15141 < 0)
15142# endif
15143 {
15144 if (r != NULL)
15145 EMSG(r); /* sending worked but evaluation failed */
15146 else
15147 EMSG2(_("E241: Unable to send to %s"), server_name);
15148 return;
15149 }
15150
15151 rettv->vval.v_string = r;
15152
15153 if (argvars[2].v_type != VAR_UNKNOWN)
15154 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015156 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015159 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 v.di_tv.v_type = VAR_STRING;
15161 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015162 idvar = get_tv_string_chk(&argvars[2]);
15163 if (idvar != NULL)
15164 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 }
15167}
15168#endif
15169
15170/*
15171 * "remote_expr()" function
15172 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 static void
15174f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177{
15178 rettv->v_type = VAR_STRING;
15179 rettv->vval.v_string = NULL;
15180#ifdef FEAT_CLIENTSERVER
15181 remote_common(argvars, rettv, TRUE);
15182#endif
15183}
15184
15185/*
15186 * "remote_foreground()" function
15187 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188 static void
15189f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015190 typval_T *argvars UNUSED;
15191 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193#ifdef FEAT_CLIENTSERVER
15194# ifdef WIN32
15195 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 {
15197 char_u *server_name = get_tv_string_chk(&argvars[0]);
15198
15199 if (server_name != NULL)
15200 serverForeground(server_name);
15201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202# else
15203 /* Send a foreground() expression to the server. */
15204 argvars[1].v_type = VAR_STRING;
15205 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15206 argvars[2].v_type = VAR_UNKNOWN;
15207 remote_common(argvars, rettv, TRUE);
15208 vim_free(argvars[1].vval.v_string);
15209# endif
15210#endif
15211}
15212
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 static void
15214f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015215 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217{
15218#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220 char_u *s = NULL;
15221# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015222 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015225
15226 if (check_restricted() || check_secure())
15227 {
15228 rettv->vval.v_number = -1;
15229 return;
15230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015231 serverid = get_tv_string_chk(&argvars[0]);
15232 if (serverid == NULL)
15233 {
15234 rettv->vval.v_number = -1;
15235 return; /* type error; errmsg already given */
15236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015238 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239 if (n == 0)
15240 rettv->vval.v_number = -1;
15241 else
15242 {
15243 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15244 rettv->vval.v_number = (s != NULL);
15245 }
15246# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247 if (check_connection() == FAIL)
15248 return;
15249
15250 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015251 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252# endif
15253
15254 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015256 char_u *retvar;
15257
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 v.di_tv.v_type = VAR_STRING;
15259 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 retvar = get_tv_string_chk(&argvars[1]);
15261 if (retvar != NULL)
15262 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015264 }
15265#else
15266 rettv->vval.v_number = -1;
15267#endif
15268}
15269
Bram Moolenaar0d660222005-01-07 21:51:51 +000015270 static void
15271f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274{
15275 char_u *r = NULL;
15276
15277#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 char_u *serverid = get_tv_string_chk(&argvars[0]);
15279
15280 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 {
15282# ifdef WIN32
15283 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015284 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015286 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287 if (n != 0)
15288 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15289 if (r == NULL)
15290# else
15291 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293# endif
15294 EMSG(_("E277: Unable to read a server reply"));
15295 }
15296#endif
15297 rettv->v_type = VAR_STRING;
15298 rettv->vval.v_string = r;
15299}
15300
15301/*
15302 * "remote_send()" function
15303 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015304 static void
15305f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015306 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015308{
15309 rettv->v_type = VAR_STRING;
15310 rettv->vval.v_string = NULL;
15311#ifdef FEAT_CLIENTSERVER
15312 remote_common(argvars, rettv, FALSE);
15313#endif
15314}
15315
15316/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015317 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015318 */
15319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015320f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015321 typval_T *argvars;
15322 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015323{
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 list_T *l;
15325 listitem_T *item, *item2;
15326 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015327 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015329 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015330 dict_T *d;
15331 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015332 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015333
Bram Moolenaar8c711452005-01-14 21:53:12 +000015334 if (argvars[0].v_type == VAR_DICT)
15335 {
15336 if (argvars[2].v_type != VAR_UNKNOWN)
15337 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015338 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015339 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015341 key = get_tv_string_chk(&argvars[1]);
15342 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015344 di = dict_find(d, key, -1);
15345 if (di == NULL)
15346 EMSG2(_(e_dictkey), key);
15347 else
15348 {
15349 *rettv = di->di_tv;
15350 init_tv(&di->di_tv);
15351 dictitem_remove(d, di);
15352 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015354 }
15355 }
15356 else if (argvars[0].v_type != VAR_LIST)
15357 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015358 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015359 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 int error = FALSE;
15362
15363 idx = get_tv_number_chk(&argvars[1], &error);
15364 if (error)
15365 ; /* type error: do nothing, errmsg already given */
15366 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015367 EMSGN(_(e_listidx), idx);
15368 else
15369 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015370 if (argvars[2].v_type == VAR_UNKNOWN)
15371 {
15372 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015373 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015374 *rettv = item->li_tv;
15375 vim_free(item);
15376 }
15377 else
15378 {
15379 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015380 end = get_tv_number_chk(&argvars[2], &error);
15381 if (error)
15382 ; /* type error: do nothing */
15383 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015384 EMSGN(_(e_listidx), end);
15385 else
15386 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015387 int cnt = 0;
15388
15389 for (li = item; li != NULL; li = li->li_next)
15390 {
15391 ++cnt;
15392 if (li == item2)
15393 break;
15394 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015395 if (li == NULL) /* didn't find "item2" after "item" */
15396 EMSG(_(e_invrange));
15397 else
15398 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015399 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015400 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015401 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015402 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015403 l->lv_first = item;
15404 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015405 item->li_prev = NULL;
15406 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015407 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015408 }
15409 }
15410 }
15411 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015412 }
15413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414}
15415
15416/*
15417 * "rename({from}, {to})" function
15418 */
15419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
15422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u buf[NUMBUFLEN];
15425
15426 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15430 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431}
15432
15433/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015434 * "repeat()" function
15435 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015436 static void
15437f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 typval_T *argvars;
15439 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015440{
15441 char_u *p;
15442 int n;
15443 int slen;
15444 int len;
15445 char_u *r;
15446 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015447
15448 n = get_tv_number(&argvars[1]);
15449 if (argvars[0].v_type == VAR_LIST)
15450 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015451 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015452 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015453 if (list_extend(rettv->vval.v_list,
15454 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015455 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015456 }
15457 else
15458 {
15459 p = get_tv_string(&argvars[0]);
15460 rettv->v_type = VAR_STRING;
15461 rettv->vval.v_string = NULL;
15462
15463 slen = (int)STRLEN(p);
15464 len = slen * n;
15465 if (len <= 0)
15466 return;
15467
15468 r = alloc(len + 1);
15469 if (r != NULL)
15470 {
15471 for (i = 0; i < n; i++)
15472 mch_memmove(r + i * slen, p, (size_t)slen);
15473 r[len] = NUL;
15474 }
15475
15476 rettv->vval.v_string = r;
15477 }
15478}
15479
15480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 * "resolve()" function
15482 */
15483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015485 typval_T *argvars;
15486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487{
15488 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015489#ifdef HAVE_READLINK
15490 char_u *buf = NULL;
15491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494#ifdef FEAT_SHORTCUT
15495 {
15496 char_u *v = NULL;
15497
15498 v = mch_resolve_shortcut(p);
15499 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015500 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015502 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 }
15504#else
15505# ifdef HAVE_READLINK
15506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 char_u *cpy;
15508 int len;
15509 char_u *remain = NULL;
15510 char_u *q;
15511 int is_relative_to_current = FALSE;
15512 int has_trailing_pathsep = FALSE;
15513 int limit = 100;
15514
15515 p = vim_strsave(p);
15516
15517 if (p[0] == '.' && (vim_ispathsep(p[1])
15518 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15519 is_relative_to_current = TRUE;
15520
15521 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015522 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015525 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527
15528 q = getnextcomp(p);
15529 if (*q != NUL)
15530 {
15531 /* Separate the first path component in "p", and keep the
15532 * remainder (beginning with the path separator). */
15533 remain = vim_strsave(q - 1);
15534 q[-1] = NUL;
15535 }
15536
Bram Moolenaard9462e32011-04-11 21:35:11 +020015537 buf = alloc(MAXPATHL + 1);
15538 if (buf == NULL)
15539 goto fail;
15540
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 for (;;)
15542 {
15543 for (;;)
15544 {
15545 len = readlink((char *)p, (char *)buf, MAXPATHL);
15546 if (len <= 0)
15547 break;
15548 buf[len] = NUL;
15549
15550 if (limit-- == 0)
15551 {
15552 vim_free(p);
15553 vim_free(remain);
15554 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 goto fail;
15557 }
15558
15559 /* Ensure that the result will have a trailing path separator
15560 * if the argument has one. */
15561 if (remain == NULL && has_trailing_pathsep)
15562 add_pathsep(buf);
15563
15564 /* Separate the first path component in the link value and
15565 * concatenate the remainders. */
15566 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15567 if (*q != NUL)
15568 {
15569 if (remain == NULL)
15570 remain = vim_strsave(q - 1);
15571 else
15572 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015573 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 if (cpy != NULL)
15575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 vim_free(remain);
15577 remain = cpy;
15578 }
15579 }
15580 q[-1] = NUL;
15581 }
15582
15583 q = gettail(p);
15584 if (q > p && *q == NUL)
15585 {
15586 /* Ignore trailing path separator. */
15587 q[-1] = NUL;
15588 q = gettail(p);
15589 }
15590 if (q > p && !mch_isFullName(buf))
15591 {
15592 /* symlink is relative to directory of argument */
15593 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15594 if (cpy != NULL)
15595 {
15596 STRCPY(cpy, p);
15597 STRCPY(gettail(cpy), buf);
15598 vim_free(p);
15599 p = cpy;
15600 }
15601 }
15602 else
15603 {
15604 vim_free(p);
15605 p = vim_strsave(buf);
15606 }
15607 }
15608
15609 if (remain == NULL)
15610 break;
15611
15612 /* Append the first path component of "remain" to "p". */
15613 q = getnextcomp(remain + 1);
15614 len = q - remain - (*q != NUL);
15615 cpy = vim_strnsave(p, STRLEN(p) + len);
15616 if (cpy != NULL)
15617 {
15618 STRNCAT(cpy, remain, len);
15619 vim_free(p);
15620 p = cpy;
15621 }
15622 /* Shorten "remain". */
15623 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015624 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 else
15626 {
15627 vim_free(remain);
15628 remain = NULL;
15629 }
15630 }
15631
15632 /* If the result is a relative path name, make it explicitly relative to
15633 * the current directory if and only if the argument had this form. */
15634 if (!vim_ispathsep(*p))
15635 {
15636 if (is_relative_to_current
15637 && *p != NUL
15638 && !(p[0] == '.'
15639 && (p[1] == NUL
15640 || vim_ispathsep(p[1])
15641 || (p[1] == '.'
15642 && (p[2] == NUL
15643 || vim_ispathsep(p[2]))))))
15644 {
15645 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015646 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 if (cpy != NULL)
15648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 vim_free(p);
15650 p = cpy;
15651 }
15652 }
15653 else if (!is_relative_to_current)
15654 {
15655 /* Strip leading "./". */
15656 q = p;
15657 while (q[0] == '.' && vim_ispathsep(q[1]))
15658 q += 2;
15659 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015660 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 }
15662 }
15663
15664 /* Ensure that the result will have no trailing path separator
15665 * if the argument had none. But keep "/" or "//". */
15666 if (!has_trailing_pathsep)
15667 {
15668 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015669 if (after_pathsep(p, q))
15670 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 }
15672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
15675# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015676 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677# endif
15678#endif
15679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
15682#ifdef HAVE_READLINK
15683fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015684 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687}
15688
15689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 */
15692 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 typval_T *argvars;
15695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696{
Bram Moolenaar33570922005-01-25 22:26:29 +000015697 list_T *l;
15698 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700 if (argvars[0].v_type != VAR_LIST)
15701 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015702 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015703 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704 {
15705 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015706 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015707 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015708 while (li != NULL)
15709 {
15710 ni = li->li_prev;
15711 list_append(l, li);
15712 li = ni;
15713 }
15714 rettv->vval.v_list = l;
15715 rettv->v_type = VAR_LIST;
15716 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015717 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719}
15720
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015721#define SP_NOMOVE 0x01 /* don't move cursor */
15722#define SP_REPEAT 0x02 /* repeat to find outer pair */
15723#define SP_RETCOUNT 0x04 /* return matchcount */
15724#define SP_SETPCMARK 0x08 /* set previous context mark */
15725#define SP_START 0x10 /* accept match at start position */
15726#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15727#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015728
Bram Moolenaar33570922005-01-25 22:26:29 +000015729static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730
15731/*
15732 * Get flags for a search function.
15733 * Possibly sets "p_ws".
15734 * Returns BACKWARD, FORWARD or zero (for an error).
15735 */
15736 static int
15737get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 int *flagsp;
15740{
15741 int dir = FORWARD;
15742 char_u *flags;
15743 char_u nbuf[NUMBUFLEN];
15744 int mask;
15745
15746 if (varp->v_type != VAR_UNKNOWN)
15747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015748 flags = get_tv_string_buf_chk(varp, nbuf);
15749 if (flags == NULL)
15750 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751 while (*flags != NUL)
15752 {
15753 switch (*flags)
15754 {
15755 case 'b': dir = BACKWARD; break;
15756 case 'w': p_ws = TRUE; break;
15757 case 'W': p_ws = FALSE; break;
15758 default: mask = 0;
15759 if (flagsp != NULL)
15760 switch (*flags)
15761 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015762 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015763 case 'e': mask = SP_END; break;
15764 case 'm': mask = SP_RETCOUNT; break;
15765 case 'n': mask = SP_NOMOVE; break;
15766 case 'p': mask = SP_SUBPAT; break;
15767 case 'r': mask = SP_REPEAT; break;
15768 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769 }
15770 if (mask == 0)
15771 {
15772 EMSG2(_(e_invarg2), flags);
15773 dir = 0;
15774 }
15775 else
15776 *flagsp |= mask;
15777 }
15778 if (dir == 0)
15779 break;
15780 ++flags;
15781 }
15782 }
15783 return dir;
15784}
15785
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015787 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015789 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015790search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015792 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015793 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015795 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 char_u *pat;
15797 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015798 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 int save_p_ws = p_ws;
15800 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015801 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015802 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015803 proftime_T tm;
15804#ifdef FEAT_RELTIME
15805 long time_limit = 0;
15806#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015807 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015808 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015810 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015811 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015812 if (dir == 0)
15813 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015814 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015815 if (flags & SP_START)
15816 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015817 if (flags & SP_END)
15818 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015819
Bram Moolenaar76929292008-01-06 19:07:36 +000015820 /* Optional arguments: line number to stop searching and timeout. */
15821 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015822 {
15823 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15824 if (lnum_stop < 0)
15825 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015826#ifdef FEAT_RELTIME
15827 if (argvars[3].v_type != VAR_UNKNOWN)
15828 {
15829 time_limit = get_tv_number_chk(&argvars[3], NULL);
15830 if (time_limit < 0)
15831 goto theend;
15832 }
15833#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015834 }
15835
Bram Moolenaar76929292008-01-06 19:07:36 +000015836#ifdef FEAT_RELTIME
15837 /* Set the time limit, if there is one. */
15838 profile_setlimit(time_limit, &tm);
15839#endif
15840
Bram Moolenaar231334e2005-07-25 20:46:57 +000015841 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015842 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015843 * Check to make sure only those flags are set.
15844 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15845 * flags cannot be set. Check for that condition also.
15846 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015847 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015848 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015850 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015851 goto theend;
15852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015854 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015855 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015856 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015857 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015859 if (flags & SP_SUBPAT)
15860 retval = subpatnum;
15861 else
15862 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015863 if (flags & SP_SETPCMARK)
15864 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015866 if (match_pos != NULL)
15867 {
15868 /* Store the match cursor position */
15869 match_pos->lnum = pos.lnum;
15870 match_pos->col = pos.col + 1;
15871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 /* "/$" will put the cursor after the end of the line, may need to
15873 * correct that here */
15874 check_cursor();
15875 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015876
15877 /* If 'n' flag is used: restore cursor position. */
15878 if (flags & SP_NOMOVE)
15879 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015880 else
15881 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015882theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015884
15885 return retval;
15886}
15887
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015888#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015889
15890/*
15891 * round() is not in C90, use ceil() or floor() instead.
15892 */
15893 float_T
15894vim_round(f)
15895 float_T f;
15896{
15897 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15898}
15899
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015900/*
15901 * "round({float})" function
15902 */
15903 static void
15904f_round(argvars, rettv)
15905 typval_T *argvars;
15906 typval_T *rettv;
15907{
15908 float_T f;
15909
15910 rettv->v_type = VAR_FLOAT;
15911 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015912 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015913 else
15914 rettv->vval.v_float = 0.0;
15915}
15916#endif
15917
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015918/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015919 * "screenattr()" function
15920 */
15921 static void
15922f_screenattr(argvars, rettv)
15923 typval_T *argvars UNUSED;
15924 typval_T *rettv;
15925{
15926 int row;
15927 int col;
15928 int c;
15929
15930 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15931 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15932 if (row < 0 || row >= screen_Rows
15933 || col < 0 || col >= screen_Columns)
15934 c = -1;
15935 else
15936 c = ScreenAttrs[LineOffset[row] + col];
15937 rettv->vval.v_number = c;
15938}
15939
15940/*
15941 * "screenchar()" function
15942 */
15943 static void
15944f_screenchar(argvars, rettv)
15945 typval_T *argvars UNUSED;
15946 typval_T *rettv;
15947{
15948 int row;
15949 int col;
15950 int off;
15951 int c;
15952
15953 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15954 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15955 if (row < 0 || row >= screen_Rows
15956 || col < 0 || col >= screen_Columns)
15957 c = -1;
15958 else
15959 {
15960 off = LineOffset[row] + col;
15961#ifdef FEAT_MBYTE
15962 if (enc_utf8 && ScreenLinesUC[off] != 0)
15963 c = ScreenLinesUC[off];
15964 else
15965#endif
15966 c = ScreenLines[off];
15967 }
15968 rettv->vval.v_number = c;
15969}
15970
15971/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015972 * "screencol()" function
15973 *
15974 * First column is 1 to be consistent with virtcol().
15975 */
15976 static void
15977f_screencol(argvars, rettv)
15978 typval_T *argvars UNUSED;
15979 typval_T *rettv;
15980{
15981 rettv->vval.v_number = screen_screencol() + 1;
15982}
15983
15984/*
15985 * "screenrow()" function
15986 */
15987 static void
15988f_screenrow(argvars, rettv)
15989 typval_T *argvars UNUSED;
15990 typval_T *rettv;
15991{
15992 rettv->vval.v_number = screen_screenrow() + 1;
15993}
15994
15995/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015996 * "search()" function
15997 */
15998 static void
15999f_search(argvars, rettv)
16000 typval_T *argvars;
16001 typval_T *rettv;
16002{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016003 int flags = 0;
16004
16005 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006}
16007
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016009 * "searchdecl()" function
16010 */
16011 static void
16012f_searchdecl(argvars, rettv)
16013 typval_T *argvars;
16014 typval_T *rettv;
16015{
16016 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016017 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016018 int error = FALSE;
16019 char_u *name;
16020
16021 rettv->vval.v_number = 1; /* default: FAIL */
16022
16023 name = get_tv_string_chk(&argvars[0]);
16024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016025 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016026 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016027 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16028 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16029 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016030 if (!error && name != NULL)
16031 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016032 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016033}
16034
16035/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016036 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016038 static int
16039searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016040 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016041 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042{
16043 char_u *spat, *mpat, *epat;
16044 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 int dir;
16047 int flags = 0;
16048 char_u nbuf1[NUMBUFLEN];
16049 char_u nbuf2[NUMBUFLEN];
16050 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016051 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016052 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016053 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016056 spat = get_tv_string_chk(&argvars[0]);
16057 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16058 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16059 if (spat == NULL || mpat == NULL || epat == NULL)
16060 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 /* Handle the optional fourth argument: flags */
16063 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016064 if (dir == 0)
16065 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016066
16067 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016068 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16069 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016070 if ((flags & (SP_END | SP_SUBPAT)) != 0
16071 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016072 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016073 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016074 goto theend;
16075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016077 /* Using 'r' implies 'W', otherwise it doesn't work. */
16078 if (flags & SP_REPEAT)
16079 p_ws = FALSE;
16080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016081 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016082 if (argvars[3].v_type == VAR_UNKNOWN
16083 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 skip = (char_u *)"";
16085 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016087 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016088 if (argvars[5].v_type != VAR_UNKNOWN)
16089 {
16090 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16091 if (lnum_stop < 0)
16092 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016093#ifdef FEAT_RELTIME
16094 if (argvars[6].v_type != VAR_UNKNOWN)
16095 {
16096 time_limit = get_tv_number_chk(&argvars[6], NULL);
16097 if (time_limit < 0)
16098 goto theend;
16099 }
16100#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016101 }
16102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 if (skip == NULL)
16104 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016106 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016107 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016108
16109theend:
16110 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016111
16112 return retval;
16113}
16114
16115/*
16116 * "searchpair()" function
16117 */
16118 static void
16119f_searchpair(argvars, rettv)
16120 typval_T *argvars;
16121 typval_T *rettv;
16122{
16123 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16124}
16125
16126/*
16127 * "searchpairpos()" function
16128 */
16129 static void
16130f_searchpairpos(argvars, rettv)
16131 typval_T *argvars;
16132 typval_T *rettv;
16133{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016134 pos_T match_pos;
16135 int lnum = 0;
16136 int col = 0;
16137
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016140
16141 if (searchpair_cmn(argvars, &match_pos) > 0)
16142 {
16143 lnum = match_pos.lnum;
16144 col = match_pos.col;
16145 }
16146
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016147 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16148 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016149}
16150
16151/*
16152 * Search for a start/middle/end thing.
16153 * Used by searchpair(), see its documentation for the details.
16154 * Returns 0 or -1 for no match,
16155 */
16156 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016157do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16158 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016159 char_u *spat; /* start pattern */
16160 char_u *mpat; /* middle pattern */
16161 char_u *epat; /* end pattern */
16162 int dir; /* BACKWARD or FORWARD */
16163 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016164 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016165 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016167 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016168{
16169 char_u *save_cpo;
16170 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16171 long retval = 0;
16172 pos_T pos;
16173 pos_T firstpos;
16174 pos_T foundpos;
16175 pos_T save_cursor;
16176 pos_T save_pos;
16177 int n;
16178 int r;
16179 int nest = 1;
16180 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016181 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016182 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016183
16184 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16185 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016186 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016187
Bram Moolenaar76929292008-01-06 19:07:36 +000016188#ifdef FEAT_RELTIME
16189 /* Set the time limit, if there is one. */
16190 profile_setlimit(time_limit, &tm);
16191#endif
16192
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016193 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16194 * start/middle/end (pat3, for the top pair). */
16195 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16196 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16197 if (pat2 == NULL || pat3 == NULL)
16198 goto theend;
16199 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16200 if (*mpat == NUL)
16201 STRCPY(pat3, pat2);
16202 else
16203 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16204 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016205 if (flags & SP_START)
16206 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016207
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 save_cursor = curwin->w_cursor;
16209 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016210 clearpos(&firstpos);
16211 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 pat = pat3;
16213 for (;;)
16214 {
16215 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016216 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16218 /* didn't find it or found the first match again: FAIL */
16219 break;
16220
16221 if (firstpos.lnum == 0)
16222 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016223 if (equalpos(pos, foundpos))
16224 {
16225 /* Found the same position again. Can happen with a pattern that
16226 * has "\zs" at the end and searching backwards. Advance one
16227 * character and try again. */
16228 if (dir == BACKWARD)
16229 decl(&pos);
16230 else
16231 incl(&pos);
16232 }
16233 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016235 /* clear the start flag to avoid getting stuck here */
16236 options &= ~SEARCH_START;
16237
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 /* If the skip pattern matches, ignore this match. */
16239 if (*skip != NUL)
16240 {
16241 save_pos = curwin->w_cursor;
16242 curwin->w_cursor = pos;
16243 r = eval_to_bool(skip, &err, NULL, FALSE);
16244 curwin->w_cursor = save_pos;
16245 if (err)
16246 {
16247 /* Evaluating {skip} caused an error, break here. */
16248 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016249 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 break;
16251 }
16252 if (r)
16253 continue;
16254 }
16255
16256 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16257 {
16258 /* Found end when searching backwards or start when searching
16259 * forward: nested pair. */
16260 ++nest;
16261 pat = pat2; /* nested, don't search for middle */
16262 }
16263 else
16264 {
16265 /* Found end when searching forward or start when searching
16266 * backward: end of (nested) pair; or found middle in outer pair. */
16267 if (--nest == 1)
16268 pat = pat3; /* outer level, search for middle */
16269 }
16270
16271 if (nest == 0)
16272 {
16273 /* Found the match: return matchcount or line number. */
16274 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016275 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016277 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016278 if (flags & SP_SETPCMARK)
16279 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 curwin->w_cursor = pos;
16281 if (!(flags & SP_REPEAT))
16282 break;
16283 nest = 1; /* search for next unmatched */
16284 }
16285 }
16286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016287 if (match_pos != NULL)
16288 {
16289 /* Store the match cursor position */
16290 match_pos->lnum = curwin->w_cursor.lnum;
16291 match_pos->col = curwin->w_cursor.col + 1;
16292 }
16293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016295 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 curwin->w_cursor = save_cursor;
16297
16298theend:
16299 vim_free(pat2);
16300 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016301 if (p_cpo == empty_option)
16302 p_cpo = save_cpo;
16303 else
16304 /* Darn, evaluating the {skip} expression changed the value. */
16305 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016306
16307 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308}
16309
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016310/*
16311 * "searchpos()" function
16312 */
16313 static void
16314f_searchpos(argvars, rettv)
16315 typval_T *argvars;
16316 typval_T *rettv;
16317{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016318 pos_T match_pos;
16319 int lnum = 0;
16320 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016321 int n;
16322 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016323
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016324 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016326
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016327 n = search_cmn(argvars, &match_pos, &flags);
16328 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016329 {
16330 lnum = match_pos.lnum;
16331 col = match_pos.col;
16332 }
16333
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016334 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16335 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016336 if (flags & SP_SUBPAT)
16337 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016338}
16339
16340
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 static void
16342f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346#ifdef FEAT_CLIENTSERVER
16347 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016348 char_u *server = get_tv_string_chk(&argvars[0]);
16349 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016352 if (server == NULL || reply == NULL)
16353 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 if (check_restricted() || check_secure())
16355 return;
16356# ifdef FEAT_X11
16357 if (check_connection() == FAIL)
16358 return;
16359# endif
16360
16361 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 EMSG(_("E258: Unable to send to client"));
16364 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366 rettv->vval.v_number = 0;
16367#else
16368 rettv->vval.v_number = -1;
16369#endif
16370}
16371
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 static void
16373f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016374 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376{
16377 char_u *r = NULL;
16378
16379#ifdef FEAT_CLIENTSERVER
16380# ifdef WIN32
16381 r = serverGetVimNames();
16382# else
16383 make_connection();
16384 if (X_DISPLAY != NULL)
16385 r = serverGetVimNames(X_DISPLAY);
16386# endif
16387#endif
16388 rettv->v_type = VAR_STRING;
16389 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390}
16391
16392/*
16393 * "setbufvar()" function
16394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016397 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016398 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399{
16400 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 char_u nbuf[NUMBUFLEN];
16405
16406 if (check_restricted() || check_secure())
16407 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016408 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16409 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016410 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 varp = &argvars[2];
16412
16413 if (buf != NULL && varname != NULL && varp != NULL)
16414 {
16415 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417
16418 if (*varname == '&')
16419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016420 long numval;
16421 char_u *strval;
16422 int error = FALSE;
16423
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 numval = get_tv_number_chk(varp, &error);
16426 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 if (!error && strval != NULL)
16428 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
16430 else
16431 {
16432 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16433 if (bufvarname != NULL)
16434 {
16435 STRCPY(bufvarname, "b:");
16436 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016437 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 vim_free(bufvarname);
16439 }
16440 }
16441
16442 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * "setcmdpos()" function
16449 */
16450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 typval_T *argvars;
16453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016455 int pos = (int)get_tv_number(&argvars[0]) - 1;
16456
16457 if (pos >= 0)
16458 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459}
16460
16461/*
16462 * "setline()" function
16463 */
16464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016465f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 typval_T *argvars;
16467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468{
16469 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016470 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016471 list_T *l = NULL;
16472 listitem_T *li = NULL;
16473 long added = 0;
16474 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016476 lnum = get_tv_lnum(&argvars[0]);
16477 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016479 l = argvars[1].vval.v_list;
16480 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016482 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016484
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016485 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016486 for (;;)
16487 {
16488 if (l != NULL)
16489 {
16490 /* list argument, get next string */
16491 if (li == NULL)
16492 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016493 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016494 li = li->li_next;
16495 }
16496
16497 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016498 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016499 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016500
16501 /* When coming here from Insert mode, sync undo, so that this can be
16502 * undone separately from what was previously inserted. */
16503 if (u_sync_once == 2)
16504 {
16505 u_sync_once = 1; /* notify that u_sync() was called */
16506 u_sync(TRUE);
16507 }
16508
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016509 if (lnum <= curbuf->b_ml.ml_line_count)
16510 {
16511 /* existing line, replace it */
16512 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16513 {
16514 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016515 if (lnum == curwin->w_cursor.lnum)
16516 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016517 rettv->vval.v_number = 0; /* OK */
16518 }
16519 }
16520 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16521 {
16522 /* lnum is one past the last line, append the line */
16523 ++added;
16524 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16525 rettv->vval.v_number = 0; /* OK */
16526 }
16527
16528 if (l == NULL) /* only one string argument */
16529 break;
16530 ++lnum;
16531 }
16532
16533 if (added > 0)
16534 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535}
16536
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016537static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16538
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016540 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016541 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016542 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016543set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016544 win_T *wp UNUSED;
16545 typval_T *list_arg UNUSED;
16546 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016547 typval_T *rettv;
16548{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016549#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016550 char_u *act;
16551 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016552#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016553
Bram Moolenaar2641f772005-03-25 21:58:17 +000016554 rettv->vval.v_number = -1;
16555
16556#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016557 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016558 EMSG(_(e_listreq));
16559 else
16560 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016561 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016562
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016563 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016564 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016565 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 if (act == NULL)
16567 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016568 if (*act == 'a' || *act == 'r')
16569 action = *act;
16570 }
16571
Bram Moolenaar81484f42012-12-05 15:16:47 +010016572 if (l != NULL && set_errorlist(wp, l, action,
16573 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016574 rettv->vval.v_number = 0;
16575 }
16576#endif
16577}
16578
16579/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016580 * "setloclist()" function
16581 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016582 static void
16583f_setloclist(argvars, rettv)
16584 typval_T *argvars;
16585 typval_T *rettv;
16586{
16587 win_T *win;
16588
16589 rettv->vval.v_number = -1;
16590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016591 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016592 if (win != NULL)
16593 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16594}
16595
16596/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016597 * "setmatches()" function
16598 */
16599 static void
16600f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016601 typval_T *argvars UNUSED;
16602 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016603{
16604#ifdef FEAT_SEARCH_EXTRA
16605 list_T *l;
16606 listitem_T *li;
16607 dict_T *d;
16608
16609 rettv->vval.v_number = -1;
16610 if (argvars[0].v_type != VAR_LIST)
16611 {
16612 EMSG(_(e_listreq));
16613 return;
16614 }
16615 if ((l = argvars[0].vval.v_list) != NULL)
16616 {
16617
16618 /* To some extent make sure that we are dealing with a list from
16619 * "getmatches()". */
16620 li = l->lv_first;
16621 while (li != NULL)
16622 {
16623 if (li->li_tv.v_type != VAR_DICT
16624 || (d = li->li_tv.vval.v_dict) == NULL)
16625 {
16626 EMSG(_(e_invarg));
16627 return;
16628 }
16629 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16630 && dict_find(d, (char_u *)"pattern", -1) != NULL
16631 && dict_find(d, (char_u *)"priority", -1) != NULL
16632 && dict_find(d, (char_u *)"id", -1) != NULL))
16633 {
16634 EMSG(_(e_invarg));
16635 return;
16636 }
16637 li = li->li_next;
16638 }
16639
16640 clear_matches(curwin);
16641 li = l->lv_first;
16642 while (li != NULL)
16643 {
16644 d = li->li_tv.vval.v_dict;
16645 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16646 get_dict_string(d, (char_u *)"pattern", FALSE),
16647 (int)get_dict_number(d, (char_u *)"priority"),
16648 (int)get_dict_number(d, (char_u *)"id"));
16649 li = li->li_next;
16650 }
16651 rettv->vval.v_number = 0;
16652 }
16653#endif
16654}
16655
16656/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016657 * "setpos()" function
16658 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016659 static void
16660f_setpos(argvars, rettv)
16661 typval_T *argvars;
16662 typval_T *rettv;
16663{
16664 pos_T pos;
16665 int fnum;
16666 char_u *name;
16667
Bram Moolenaar08250432008-02-13 11:42:46 +000016668 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016669 name = get_tv_string_chk(argvars);
16670 if (name != NULL)
16671 {
16672 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16673 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016674 if (--pos.col < 0)
16675 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016676 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016677 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016678 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016679 if (fnum == curbuf->b_fnum)
16680 {
16681 curwin->w_cursor = pos;
16682 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016683 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016684 }
16685 else
16686 EMSG(_(e_invarg));
16687 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016688 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16689 {
16690 /* set mark */
16691 if (setmark_pos(name[1], &pos, fnum) == OK)
16692 rettv->vval.v_number = 0;
16693 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016694 else
16695 EMSG(_(e_invarg));
16696 }
16697 }
16698}
16699
16700/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016701 * "setqflist()" function
16702 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016703 static void
16704f_setqflist(argvars, rettv)
16705 typval_T *argvars;
16706 typval_T *rettv;
16707{
16708 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16709}
16710
16711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 * "setreg()" function
16713 */
16714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 typval_T *argvars;
16717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718{
16719 int regname;
16720 char_u *strregname;
16721 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016722 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 int append;
16724 char_u yank_type;
16725 long block_len;
16726
16727 block_len = -1;
16728 yank_type = MAUTO;
16729 append = FALSE;
16730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016732 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016734 if (strregname == NULL)
16735 return; /* type error; errmsg already given */
16736 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 if (regname == 0 || regname == '@')
16738 regname = '"';
16739 else if (regname == '=')
16740 return;
16741
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016742 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016744 stropt = get_tv_string_chk(&argvars[2]);
16745 if (stropt == NULL)
16746 return; /* type error */
16747 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 switch (*stropt)
16749 {
16750 case 'a': case 'A': /* append */
16751 append = TRUE;
16752 break;
16753 case 'v': case 'c': /* character-wise selection */
16754 yank_type = MCHAR;
16755 break;
16756 case 'V': case 'l': /* line-wise selection */
16757 yank_type = MLINE;
16758 break;
16759#ifdef FEAT_VISUAL
16760 case 'b': case Ctrl_V: /* block-wise selection */
16761 yank_type = MBLOCK;
16762 if (VIM_ISDIGIT(stropt[1]))
16763 {
16764 ++stropt;
16765 block_len = getdigits(&stropt) - 1;
16766 --stropt;
16767 }
16768 break;
16769#endif
16770 }
16771 }
16772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016773 strval = get_tv_string_chk(&argvars[1]);
16774 if (strval != NULL)
16775 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778}
16779
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016780/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016781 * "settabvar()" function
16782 */
16783 static void
16784f_settabvar(argvars, rettv)
16785 typval_T *argvars;
16786 typval_T *rettv;
16787{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016788#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016789 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016790 tabpage_T *tp;
16791#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016792 char_u *varname, *tabvarname;
16793 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016794
16795 rettv->vval.v_number = 0;
16796
16797 if (check_restricted() || check_secure())
16798 return;
16799
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016800#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016801 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016802#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016803 varname = get_tv_string_chk(&argvars[1]);
16804 varp = &argvars[2];
16805
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016806 if (varname != NULL && varp != NULL
16807#ifdef FEAT_WINDOWS
16808 && tp != NULL
16809#endif
16810 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016811 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016812#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016813 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016814 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016815#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016816
16817 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16818 if (tabvarname != NULL)
16819 {
16820 STRCPY(tabvarname, "t:");
16821 STRCPY(tabvarname + 2, varname);
16822 set_var(tabvarname, varp, TRUE);
16823 vim_free(tabvarname);
16824 }
16825
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016826#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016827 /* Restore current tabpage */
16828 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016829 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016830#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016831 }
16832}
16833
16834/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016835 * "settabwinvar()" function
16836 */
16837 static void
16838f_settabwinvar(argvars, rettv)
16839 typval_T *argvars;
16840 typval_T *rettv;
16841{
16842 setwinvar(argvars, rettv, 1);
16843}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844
16845/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016846 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016850 typval_T *argvars;
16851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016853 setwinvar(argvars, rettv, 0);
16854}
16855
16856/*
16857 * "setwinvar()" and "settabwinvar()" functions
16858 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016859
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016860 static void
16861setwinvar(argvars, rettv, off)
16862 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016863 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016864 int off;
16865{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 win_T *win;
16867#ifdef FEAT_WINDOWS
16868 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016869 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870#endif
16871 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016874 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875
16876 if (check_restricted() || check_secure())
16877 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016878
16879#ifdef FEAT_WINDOWS
16880 if (off == 1)
16881 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16882 else
16883 tp = curtab;
16884#endif
16885 win = find_win_by_nr(&argvars[off], tp);
16886 varname = get_tv_string_chk(&argvars[off + 1]);
16887 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888
16889 if (win != NULL && varname != NULL && varp != NULL)
16890 {
16891#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016892 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016893 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894#endif
16895
16896 if (*varname == '&')
16897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016898 long numval;
16899 char_u *strval;
16900 int error = FALSE;
16901
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016903 numval = get_tv_number_chk(varp, &error);
16904 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 if (!error && strval != NULL)
16906 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 }
16908 else
16909 {
16910 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16911 if (winvarname != NULL)
16912 {
16913 STRCPY(winvarname, "w:");
16914 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016915 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 vim_free(winvarname);
16917 }
16918 }
16919
16920#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016921 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922#endif
16923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924}
16925
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016926#ifdef FEAT_CRYPT
16927/*
16928 * "sha256({string})" function
16929 */
16930 static void
16931f_sha256(argvars, rettv)
16932 typval_T *argvars;
16933 typval_T *rettv;
16934{
16935 char_u *p;
16936
16937 p = get_tv_string(&argvars[0]);
16938 rettv->vval.v_string = vim_strsave(
16939 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16940 rettv->v_type = VAR_STRING;
16941}
16942#endif /* FEAT_CRYPT */
16943
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016945 * "shellescape({string})" function
16946 */
16947 static void
16948f_shellescape(argvars, rettv)
16949 typval_T *argvars;
16950 typval_T *rettv;
16951{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016952 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010016953 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016954 rettv->v_type = VAR_STRING;
16955}
16956
16957/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016958 * shiftwidth() function
16959 */
16960 static void
16961f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016962 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016963 typval_T *rettv;
16964{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016965 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016966}
16967
16968/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 */
16971 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 typval_T *argvars;
16974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 p = get_tv_string(&argvars[0]);
16979 rettv->vval.v_string = vim_strsave(p);
16980 simplify_filename(rettv->vval.v_string); /* simplify in place */
16981 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982}
16983
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016984#ifdef FEAT_FLOAT
16985/*
16986 * "sin()" function
16987 */
16988 static void
16989f_sin(argvars, rettv)
16990 typval_T *argvars;
16991 typval_T *rettv;
16992{
16993 float_T f;
16994
16995 rettv->v_type = VAR_FLOAT;
16996 if (get_float_arg(argvars, &f) == OK)
16997 rettv->vval.v_float = sin(f);
16998 else
16999 rettv->vval.v_float = 0.0;
17000}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017001
17002/*
17003 * "sinh()" function
17004 */
17005 static void
17006f_sinh(argvars, rettv)
17007 typval_T *argvars;
17008 typval_T *rettv;
17009{
17010 float_T f;
17011
17012 rettv->v_type = VAR_FLOAT;
17013 if (get_float_arg(argvars, &f) == OK)
17014 rettv->vval.v_float = sinh(f);
17015 else
17016 rettv->vval.v_float = 0.0;
17017}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017018#endif
17019
Bram Moolenaar0d660222005-01-07 21:51:51 +000017020static int
17021#ifdef __BORLANDC__
17022 _RTLENTRYF
17023#endif
17024 item_compare __ARGS((const void *s1, const void *s2));
17025static int
17026#ifdef __BORLANDC__
17027 _RTLENTRYF
17028#endif
17029 item_compare2 __ARGS((const void *s1, const void *s2));
17030
17031static int item_compare_ic;
17032static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017033static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017034static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035#define ITEM_COMPARE_FAIL 999
17036
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 static int
17041#ifdef __BORLANDC__
17042_RTLENTRYF
17043#endif
17044item_compare(s1, s2)
17045 const void *s1;
17046 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048 char_u *p1, *p2;
17049 char_u *tofree1, *tofree2;
17050 int res;
17051 char_u numbuf1[NUMBUFLEN];
17052 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017054 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17055 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017056 if (p1 == NULL)
17057 p1 = (char_u *)"";
17058 if (p2 == NULL)
17059 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 if (item_compare_ic)
17061 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 res = STRCMP(p1, p2);
17064 vim_free(tofree1);
17065 vim_free(tofree2);
17066 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067}
17068
17069 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070#ifdef __BORLANDC__
17071_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073item_compare2(s1, s2)
17074 const void *s1;
17075 const void *s2;
17076{
17077 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017078 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017079 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 /* shortcut after failure in previous call; compare all items equal */
17083 if (item_compare_func_err)
17084 return 0;
17085
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017086 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17087 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17089 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090
17091 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017092 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017093 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17094 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 clear_tv(&argv[0]);
17096 clear_tv(&argv[1]);
17097
17098 if (res == FAIL)
17099 res = ITEM_COMPARE_FAIL;
17100 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17102 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017103 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 clear_tv(&rettv);
17105 return res;
17106}
17107
17108/*
17109 * "sort({list})" function
17110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 typval_T *argvars;
17114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115{
Bram Moolenaar33570922005-01-25 22:26:29 +000017116 list_T *l;
17117 listitem_T *li;
17118 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119 long len;
17120 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 if (argvars[0].v_type != VAR_LIST)
17123 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 else
17125 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017127 if (l == NULL || tv_check_lock(l->lv_lock,
17128 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 return;
17130 rettv->vval.v_list = l;
17131 rettv->v_type = VAR_LIST;
17132 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134 len = list_len(l);
17135 if (len <= 1)
17136 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 item_compare_ic = FALSE;
17139 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017140 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 if (argvars[1].v_type != VAR_UNKNOWN)
17142 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017143 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 else
17147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 int error = FALSE;
17149
17150 i = get_tv_number_chk(&argvars[1], &error);
17151 if (error)
17152 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 if (i == 1)
17154 item_compare_ic = TRUE;
17155 else
17156 item_compare_func = get_tv_string(&argvars[1]);
17157 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017158
17159 if (argvars[2].v_type != VAR_UNKNOWN)
17160 {
17161 /* optional third argument: {dict} */
17162 if (argvars[2].v_type != VAR_DICT)
17163 {
17164 EMSG(_(e_dictreq));
17165 return;
17166 }
17167 item_compare_selfdict = argvars[2].vval.v_dict;
17168 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 if (ptrs == NULL)
17174 return;
17175 i = 0;
17176 for (li = l->lv_first; li != NULL; li = li->li_next)
17177 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 /* test the compare function */
17181 if (item_compare_func != NULL
17182 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17183 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017184 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 {
17187 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 item_compare_func == NULL ? item_compare : item_compare2);
17190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017191 if (!item_compare_func_err)
17192 {
17193 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017194 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017195 l->lv_len = 0;
17196 for (i = 0; i < len; ++i)
17197 list_append(l, ptrs[i]);
17198 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017199 }
17200
17201 vim_free(ptrs);
17202 }
17203}
17204
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017205/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017206 * "soundfold({word})" function
17207 */
17208 static void
17209f_soundfold(argvars, rettv)
17210 typval_T *argvars;
17211 typval_T *rettv;
17212{
17213 char_u *s;
17214
17215 rettv->v_type = VAR_STRING;
17216 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017217#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017218 rettv->vval.v_string = eval_soundfold(s);
17219#else
17220 rettv->vval.v_string = vim_strsave(s);
17221#endif
17222}
17223
17224/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017225 * "spellbadword()" function
17226 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017227 static void
17228f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017229 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017230 typval_T *rettv;
17231{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017232 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017233 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017234 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017235
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017236 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017237 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017238
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017239#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017240 if (argvars[0].v_type == VAR_UNKNOWN)
17241 {
17242 /* Find the start and length of the badly spelled word. */
17243 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17244 if (len != 0)
17245 word = ml_get_cursor();
17246 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017247 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017248 {
17249 char_u *str = get_tv_string_chk(&argvars[0]);
17250 int capcol = -1;
17251
17252 if (str != NULL)
17253 {
17254 /* Check the argument for spelling. */
17255 while (*str != NUL)
17256 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017257 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017258 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017259 {
17260 word = str;
17261 break;
17262 }
17263 str += len;
17264 }
17265 }
17266 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017267#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017268
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017269 list_append_string(rettv->vval.v_list, word, len);
17270 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017271 attr == HLF_SPB ? "bad" :
17272 attr == HLF_SPR ? "rare" :
17273 attr == HLF_SPL ? "local" :
17274 attr == HLF_SPC ? "caps" :
17275 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017276}
17277
17278/*
17279 * "spellsuggest()" function
17280 */
17281 static void
17282f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017283 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017284 typval_T *rettv;
17285{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017286#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017287 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017288 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017289 int maxcount;
17290 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017291 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017292 listitem_T *li;
17293 int need_capital = FALSE;
17294#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017295
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017296 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017297 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017298
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017299#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017300 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017301 {
17302 str = get_tv_string(&argvars[0]);
17303 if (argvars[1].v_type != VAR_UNKNOWN)
17304 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017305 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017306 if (maxcount <= 0)
17307 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017308 if (argvars[2].v_type != VAR_UNKNOWN)
17309 {
17310 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17311 if (typeerr)
17312 return;
17313 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017314 }
17315 else
17316 maxcount = 25;
17317
Bram Moolenaar4770d092006-01-12 23:22:24 +000017318 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017319
17320 for (i = 0; i < ga.ga_len; ++i)
17321 {
17322 str = ((char_u **)ga.ga_data)[i];
17323
17324 li = listitem_alloc();
17325 if (li == NULL)
17326 vim_free(str);
17327 else
17328 {
17329 li->li_tv.v_type = VAR_STRING;
17330 li->li_tv.v_lock = 0;
17331 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017332 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017333 }
17334 }
17335 ga_clear(&ga);
17336 }
17337#endif
17338}
17339
Bram Moolenaar0d660222005-01-07 21:51:51 +000017340 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017341f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017342 typval_T *argvars;
17343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017344{
17345 char_u *str;
17346 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017347 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348 regmatch_T regmatch;
17349 char_u patbuf[NUMBUFLEN];
17350 char_u *save_cpo;
17351 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017352 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017353 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017355
17356 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17357 save_cpo = p_cpo;
17358 p_cpo = (char_u *)"";
17359
17360 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017361 if (argvars[1].v_type != VAR_UNKNOWN)
17362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17364 if (pat == NULL)
17365 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017368 }
17369 if (pat == NULL || *pat == NUL)
17370 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017372 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017374 if (typeerr)
17375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376
Bram Moolenaar0d660222005-01-07 21:51:51 +000017377 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17378 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017380 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017381 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017383 if (*str == NUL)
17384 match = FALSE; /* empty item at the end */
17385 else
17386 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 if (match)
17388 end = regmatch.startp[0];
17389 else
17390 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017391 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17392 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017393 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017394 if (list_append_string(rettv->vval.v_list, str,
17395 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017396 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017397 }
17398 if (!match)
17399 break;
17400 /* Advance to just after the match. */
17401 if (regmatch.endp[0] > str)
17402 col = 0;
17403 else
17404 {
17405 /* Don't get stuck at the same match. */
17406#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017407 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408#else
17409 col = 1;
17410#endif
17411 }
17412 str = regmatch.endp[0];
17413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414
Bram Moolenaar473de612013-06-08 18:19:48 +020017415 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417
Bram Moolenaar0d660222005-01-07 21:51:51 +000017418 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419}
17420
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017421#ifdef FEAT_FLOAT
17422/*
17423 * "sqrt()" function
17424 */
17425 static void
17426f_sqrt(argvars, rettv)
17427 typval_T *argvars;
17428 typval_T *rettv;
17429{
17430 float_T f;
17431
17432 rettv->v_type = VAR_FLOAT;
17433 if (get_float_arg(argvars, &f) == OK)
17434 rettv->vval.v_float = sqrt(f);
17435 else
17436 rettv->vval.v_float = 0.0;
17437}
17438
17439/*
17440 * "str2float()" function
17441 */
17442 static void
17443f_str2float(argvars, rettv)
17444 typval_T *argvars;
17445 typval_T *rettv;
17446{
17447 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17448
17449 if (*p == '+')
17450 p = skipwhite(p + 1);
17451 (void)string2float(p, &rettv->vval.v_float);
17452 rettv->v_type = VAR_FLOAT;
17453}
17454#endif
17455
Bram Moolenaar2c932302006-03-18 21:42:09 +000017456/*
17457 * "str2nr()" function
17458 */
17459 static void
17460f_str2nr(argvars, rettv)
17461 typval_T *argvars;
17462 typval_T *rettv;
17463{
17464 int base = 10;
17465 char_u *p;
17466 long n;
17467
17468 if (argvars[1].v_type != VAR_UNKNOWN)
17469 {
17470 base = get_tv_number(&argvars[1]);
17471 if (base != 8 && base != 10 && base != 16)
17472 {
17473 EMSG(_(e_invarg));
17474 return;
17475 }
17476 }
17477
17478 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017479 if (*p == '+')
17480 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017481 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17482 rettv->vval.v_number = n;
17483}
17484
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485#ifdef HAVE_STRFTIME
17486/*
17487 * "strftime({format}[, {time}])" function
17488 */
17489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *argvars;
17492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493{
17494 char_u result_buf[256];
17495 struct tm *curtime;
17496 time_t seconds;
17497 char_u *p;
17498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017502 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 seconds = time(NULL);
17504 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 curtime = localtime(&seconds);
17507 /* MSVC returns NULL for an invalid value of seconds. */
17508 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 else
17511 {
17512# ifdef FEAT_MBYTE
17513 vimconv_T conv;
17514 char_u *enc;
17515
17516 conv.vc_type = CONV_NONE;
17517 enc = enc_locale();
17518 convert_setup(&conv, p_enc, enc);
17519 if (conv.vc_type != CONV_NONE)
17520 p = string_convert(&conv, p, NULL);
17521# endif
17522 if (p != NULL)
17523 (void)strftime((char *)result_buf, sizeof(result_buf),
17524 (char *)p, curtime);
17525 else
17526 result_buf[0] = NUL;
17527
17528# ifdef FEAT_MBYTE
17529 if (conv.vc_type != CONV_NONE)
17530 vim_free(p);
17531 convert_setup(&conv, enc, p_enc);
17532 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017533 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 else
17535# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537
17538# ifdef FEAT_MBYTE
17539 /* Release conversion descriptors */
17540 convert_setup(&conv, NULL, NULL);
17541 vim_free(enc);
17542# endif
17543 }
17544}
17545#endif
17546
17547/*
17548 * "stridx()" function
17549 */
17550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017551f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 typval_T *argvars;
17553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554{
17555 char_u buf[NUMBUFLEN];
17556 char_u *needle;
17557 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017562 needle = get_tv_string_chk(&argvars[1]);
17563 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017565 if (needle == NULL || haystack == NULL)
17566 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 if (argvars[2].v_type != VAR_UNKNOWN)
17569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017570 int error = FALSE;
17571
17572 start_idx = get_tv_number_chk(&argvars[2], &error);
17573 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017574 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017575 if (start_idx >= 0)
17576 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 }
17578
17579 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17580 if (pos != NULL)
17581 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582}
17583
17584/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017585 * "string()" function
17586 */
17587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017588f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017589 typval_T *argvars;
17590 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017591{
17592 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017593 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017596 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017597 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017598 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600}
17601
17602/*
17603 * "strlen()" function
17604 */
17605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 typval_T *argvars;
17608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610 rettv->vval.v_number = (varnumber_T)(STRLEN(
17611 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612}
17613
17614/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017615 * "strchars()" function
17616 */
17617 static void
17618f_strchars(argvars, rettv)
17619 typval_T *argvars;
17620 typval_T *rettv;
17621{
17622 char_u *s = get_tv_string(&argvars[0]);
17623#ifdef FEAT_MBYTE
17624 varnumber_T len = 0;
17625
17626 while (*s != NUL)
17627 {
17628 mb_cptr2char_adv(&s);
17629 ++len;
17630 }
17631 rettv->vval.v_number = len;
17632#else
17633 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17634#endif
17635}
17636
17637/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017638 * "strdisplaywidth()" function
17639 */
17640 static void
17641f_strdisplaywidth(argvars, rettv)
17642 typval_T *argvars;
17643 typval_T *rettv;
17644{
17645 char_u *s = get_tv_string(&argvars[0]);
17646 int col = 0;
17647
17648 if (argvars[1].v_type != VAR_UNKNOWN)
17649 col = get_tv_number(&argvars[1]);
17650
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017651 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017652}
17653
17654/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017655 * "strwidth()" function
17656 */
17657 static void
17658f_strwidth(argvars, rettv)
17659 typval_T *argvars;
17660 typval_T *rettv;
17661{
17662 char_u *s = get_tv_string(&argvars[0]);
17663
17664 rettv->vval.v_number = (varnumber_T)(
17665#ifdef FEAT_MBYTE
17666 mb_string2cells(s, -1)
17667#else
17668 STRLEN(s)
17669#endif
17670 );
17671}
17672
17673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 * "strpart()" function
17675 */
17676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 typval_T *argvars;
17679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680{
17681 char_u *p;
17682 int n;
17683 int len;
17684 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017685 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 slen = (int)STRLEN(p);
17689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017690 n = get_tv_number_chk(&argvars[1], &error);
17691 if (error)
17692 len = 0;
17693 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017694 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 else
17696 len = slen - n; /* default len: all bytes that are available. */
17697
17698 /*
17699 * Only return the overlap between the specified part and the actual
17700 * string.
17701 */
17702 if (n < 0)
17703 {
17704 len += n;
17705 n = 0;
17706 }
17707 else if (n > slen)
17708 n = slen;
17709 if (len < 0)
17710 len = 0;
17711 else if (n + len > slen)
17712 len = slen - n;
17713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 rettv->v_type = VAR_STRING;
17715 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716}
17717
17718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719 * "strridx()" function
17720 */
17721 static void
17722f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 typval_T *argvars;
17724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725{
17726 char_u buf[NUMBUFLEN];
17727 char_u *needle;
17728 char_u *haystack;
17729 char_u *rest;
17730 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017731 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 needle = get_tv_string_chk(&argvars[1]);
17734 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735
17736 rettv->vval.v_number = -1;
17737 if (needle == NULL || haystack == NULL)
17738 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017739
17740 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017741 if (argvars[2].v_type != VAR_UNKNOWN)
17742 {
17743 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017744 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017745 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017747 }
17748 else
17749 end_idx = haystack_len;
17750
Bram Moolenaar0d660222005-01-07 21:51:51 +000017751 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017754 lastmatch = haystack + end_idx;
17755 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017757 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758 for (rest = haystack; *rest != '\0'; ++rest)
17759 {
17760 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017761 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017762 break;
17763 lastmatch = rest;
17764 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017765 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766
17767 if (lastmatch == NULL)
17768 rettv->vval.v_number = -1;
17769 else
17770 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17771}
17772
17773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 * "strtrans()" function
17775 */
17776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 typval_T *argvars;
17779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017781 rettv->v_type = VAR_STRING;
17782 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783}
17784
17785/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786 * "submatch()" function
17787 */
17788 static void
17789f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 typval_T *argvars;
17791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792{
17793 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 rettv->vval.v_string =
17795 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796}
17797
17798/*
17799 * "substitute()" function
17800 */
17801 static void
17802f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 typval_T *argvars;
17804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017805{
17806 char_u patbuf[NUMBUFLEN];
17807 char_u subbuf[NUMBUFLEN];
17808 char_u flagsbuf[NUMBUFLEN];
17809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017810 char_u *str = get_tv_string_chk(&argvars[0]);
17811 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17812 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17813 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17814
Bram Moolenaar0d660222005-01-07 21:51:51 +000017815 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17817 rettv->vval.v_string = NULL;
17818 else
17819 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017820}
17821
17822/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017823 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829{
17830 int id = 0;
17831#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017832 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 long col;
17834 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017835 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017837 lnum = get_tv_lnum(argvars); /* -1 on type error */
17838 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17839 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017841 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017842 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017843 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844#endif
17845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847}
17848
17849/*
17850 * "synIDattr(id, what [, mode])" function
17851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
17857 char_u *p = NULL;
17858#ifdef FEAT_SYN_HL
17859 int id;
17860 char_u *what;
17861 char_u *mode;
17862 char_u modebuf[NUMBUFLEN];
17863 int modec;
17864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017865 id = get_tv_number(&argvars[0]);
17866 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017871 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 modec = 0; /* replace invalid with current */
17873 }
17874 else
17875 {
17876#ifdef FEAT_GUI
17877 if (gui.in_use)
17878 modec = 'g';
17879 else
17880#endif
17881 if (t_colors > 1)
17882 modec = 'c';
17883 else
17884 modec = 't';
17885 }
17886
17887
17888 switch (TOLOWER_ASC(what[0]))
17889 {
17890 case 'b':
17891 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17892 p = highlight_color(id, what, modec);
17893 else /* bold */
17894 p = highlight_has_attr(id, HL_BOLD, modec);
17895 break;
17896
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017897 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 p = highlight_color(id, what, modec);
17899 break;
17900
17901 case 'i':
17902 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17903 p = highlight_has_attr(id, HL_INVERSE, modec);
17904 else /* italic */
17905 p = highlight_has_attr(id, HL_ITALIC, modec);
17906 break;
17907
17908 case 'n': /* name */
17909 p = get_highlight_name(NULL, id - 1);
17910 break;
17911
17912 case 'r': /* reverse */
17913 p = highlight_has_attr(id, HL_INVERSE, modec);
17914 break;
17915
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017916 case 's':
17917 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17918 p = highlight_color(id, what, modec);
17919 else /* standout */
17920 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 break;
17922
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017923 case 'u':
17924 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17925 /* underline */
17926 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17927 else
17928 /* undercurl */
17929 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 break;
17931 }
17932
17933 if (p != NULL)
17934 p = vim_strsave(p);
17935#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 rettv->v_type = VAR_STRING;
17937 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938}
17939
17940/*
17941 * "synIDtrans(id)" function
17942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017944f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017945 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947{
17948 int id;
17949
17950#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017951 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952
17953 if (id > 0)
17954 id = syn_get_final_id(id);
17955 else
17956#endif
17957 id = 0;
17958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960}
17961
17962/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017963 * "synconcealed(lnum, col)" function
17964 */
17965 static void
17966f_synconcealed(argvars, rettv)
17967 typval_T *argvars UNUSED;
17968 typval_T *rettv;
17969{
17970#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17971 long lnum;
17972 long col;
17973 int syntax_flags = 0;
17974 int cchar;
17975 int matchid = 0;
17976 char_u str[NUMBUFLEN];
17977#endif
17978
17979 rettv->v_type = VAR_LIST;
17980 rettv->vval.v_list = NULL;
17981
17982#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17983 lnum = get_tv_lnum(argvars); /* -1 on type error */
17984 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17985
17986 vim_memset(str, NUL, sizeof(str));
17987
17988 if (rettv_list_alloc(rettv) != FAIL)
17989 {
17990 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17991 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17992 && curwin->w_p_cole > 0)
17993 {
17994 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17995 syntax_flags = get_syntax_info(&matchid);
17996
17997 /* get the conceal character */
17998 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17999 {
18000 cchar = syn_get_sub_char();
18001 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18002 cchar = lcs_conceal;
18003 if (cchar != NUL)
18004 {
18005# ifdef FEAT_MBYTE
18006 if (has_mbyte)
18007 (*mb_char2bytes)(cchar, str);
18008 else
18009# endif
18010 str[0] = cchar;
18011 }
18012 }
18013 }
18014
18015 list_append_number(rettv->vval.v_list,
18016 (syntax_flags & HL_CONCEAL) != 0);
18017 /* -1 to auto-determine strlen */
18018 list_append_string(rettv->vval.v_list, str, -1);
18019 list_append_number(rettv->vval.v_list, matchid);
18020 }
18021#endif
18022}
18023
18024/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018025 * "synstack(lnum, col)" function
18026 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018027 static void
18028f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018029 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018030 typval_T *rettv;
18031{
18032#ifdef FEAT_SYN_HL
18033 long lnum;
18034 long col;
18035 int i;
18036 int id;
18037#endif
18038
18039 rettv->v_type = VAR_LIST;
18040 rettv->vval.v_list = NULL;
18041
18042#ifdef FEAT_SYN_HL
18043 lnum = get_tv_lnum(argvars); /* -1 on type error */
18044 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18045
18046 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018047 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018048 && rettv_list_alloc(rettv) != FAIL)
18049 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018050 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018051 for (i = 0; ; ++i)
18052 {
18053 id = syn_get_stack_item(i);
18054 if (id < 0)
18055 break;
18056 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18057 break;
18058 }
18059 }
18060#endif
18061}
18062
18063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 * "system()" function
18065 */
18066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018068 typval_T *argvars;
18069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018071 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018073 char_u *infile = NULL;
18074 char_u buf[NUMBUFLEN];
18075 int err = FALSE;
18076 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018078 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018079 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018080
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018081 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018082 {
18083 /*
18084 * Write the string to a temp file, to be used for input of the shell
18085 * command.
18086 */
18087 if ((infile = vim_tempname('i')) == NULL)
18088 {
18089 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018090 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018091 }
18092
18093 fd = mch_fopen((char *)infile, WRITEBIN);
18094 if (fd == NULL)
18095 {
18096 EMSG2(_(e_notopen), infile);
18097 goto done;
18098 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018099 p = get_tv_string_buf_chk(&argvars[1], buf);
18100 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018101 {
18102 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018104 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018105 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18106 err = TRUE;
18107 if (fclose(fd) != 0)
18108 err = TRUE;
18109 if (err)
18110 {
18111 EMSG(_("E677: Error writing temp file"));
18112 goto done;
18113 }
18114 }
18115
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018116 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18117 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018118
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119#ifdef USE_CR
18120 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018121 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 {
18123 char_u *s;
18124
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018125 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 {
18127 if (*s == CAR)
18128 *s = NL;
18129 }
18130 }
18131#else
18132# ifdef USE_CRNL
18133 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018134 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 {
18136 char_u *s, *d;
18137
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018138 d = res;
18139 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 {
18141 if (s[0] == CAR && s[1] == NL)
18142 ++s;
18143 *d++ = *s;
18144 }
18145 *d = NUL;
18146 }
18147# endif
18148#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018149
18150done:
18151 if (infile != NULL)
18152 {
18153 mch_remove(infile);
18154 vim_free(infile);
18155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156 rettv->v_type = VAR_STRING;
18157 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158}
18159
18160/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018161 * "tabpagebuflist()" function
18162 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018163 static void
18164f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018165 typval_T *argvars UNUSED;
18166 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018167{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018168#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018169 tabpage_T *tp;
18170 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018171
18172 if (argvars[0].v_type == VAR_UNKNOWN)
18173 wp = firstwin;
18174 else
18175 {
18176 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18177 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018178 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018179 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018180 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018181 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018182 for (; wp != NULL; wp = wp->w_next)
18183 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018184 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018185 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018186 }
18187#endif
18188}
18189
18190
18191/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018192 * "tabpagenr()" function
18193 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018194 static void
18195f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018196 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018197 typval_T *rettv;
18198{
18199 int nr = 1;
18200#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018201 char_u *arg;
18202
18203 if (argvars[0].v_type != VAR_UNKNOWN)
18204 {
18205 arg = get_tv_string_chk(&argvars[0]);
18206 nr = 0;
18207 if (arg != NULL)
18208 {
18209 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018210 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018211 else
18212 EMSG2(_(e_invexpr2), arg);
18213 }
18214 }
18215 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018216 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018217#endif
18218 rettv->vval.v_number = nr;
18219}
18220
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018221
18222#ifdef FEAT_WINDOWS
18223static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18224
18225/*
18226 * Common code for tabpagewinnr() and winnr().
18227 */
18228 static int
18229get_winnr(tp, argvar)
18230 tabpage_T *tp;
18231 typval_T *argvar;
18232{
18233 win_T *twin;
18234 int nr = 1;
18235 win_T *wp;
18236 char_u *arg;
18237
18238 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18239 if (argvar->v_type != VAR_UNKNOWN)
18240 {
18241 arg = get_tv_string_chk(argvar);
18242 if (arg == NULL)
18243 nr = 0; /* type error; errmsg already given */
18244 else if (STRCMP(arg, "$") == 0)
18245 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18246 else if (STRCMP(arg, "#") == 0)
18247 {
18248 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18249 if (twin == NULL)
18250 nr = 0;
18251 }
18252 else
18253 {
18254 EMSG2(_(e_invexpr2), arg);
18255 nr = 0;
18256 }
18257 }
18258
18259 if (nr > 0)
18260 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18261 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018262 {
18263 if (wp == NULL)
18264 {
18265 /* didn't find it in this tabpage */
18266 nr = 0;
18267 break;
18268 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018269 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018270 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018271 return nr;
18272}
18273#endif
18274
18275/*
18276 * "tabpagewinnr()" function
18277 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018278 static void
18279f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018280 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018281 typval_T *rettv;
18282{
18283 int nr = 1;
18284#ifdef FEAT_WINDOWS
18285 tabpage_T *tp;
18286
18287 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18288 if (tp == NULL)
18289 nr = 0;
18290 else
18291 nr = get_winnr(tp, &argvars[1]);
18292#endif
18293 rettv->vval.v_number = nr;
18294}
18295
18296
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018297/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018298 * "tagfiles()" function
18299 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018300 static void
18301f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018302 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018303 typval_T *rettv;
18304{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018305 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018306 tagname_T tn;
18307 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018309 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018310 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018311 fname = alloc(MAXPATHL);
18312 if (fname == NULL)
18313 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018315 for (first = TRUE; ; first = FALSE)
18316 if (get_tagfname(&tn, first, fname) == FAIL
18317 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018318 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018319 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018320 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018321}
18322
18323/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018324 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018325 */
18326 static void
18327f_taglist(argvars, rettv)
18328 typval_T *argvars;
18329 typval_T *rettv;
18330{
18331 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018332
18333 tag_pattern = get_tv_string(&argvars[0]);
18334
18335 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018336 if (*tag_pattern == NUL)
18337 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018338
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018339 if (rettv_list_alloc(rettv) == OK)
18340 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018341}
18342
18343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 * "tempname()" function
18345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018347f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350{
18351 static int x = 'A';
18352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353 rettv->v_type = VAR_STRING;
18354 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355
18356 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18357 * names. Skip 'I' and 'O', they are used for shell redirection. */
18358 do
18359 {
18360 if (x == 'Z')
18361 x = '0';
18362 else if (x == '9')
18363 x = 'A';
18364 else
18365 {
18366#ifdef EBCDIC
18367 if (x == 'I')
18368 x = 'J';
18369 else if (x == 'R')
18370 x = 'S';
18371 else
18372#endif
18373 ++x;
18374 }
18375 } while (x == 'I' || x == 'O');
18376}
18377
18378/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018379 * "test(list)" function: Just checking the walls...
18380 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018381 static void
18382f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018383 typval_T *argvars UNUSED;
18384 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018385{
18386 /* Used for unit testing. Change the code below to your liking. */
18387#if 0
18388 listitem_T *li;
18389 list_T *l;
18390 char_u *bad, *good;
18391
18392 if (argvars[0].v_type != VAR_LIST)
18393 return;
18394 l = argvars[0].vval.v_list;
18395 if (l == NULL)
18396 return;
18397 li = l->lv_first;
18398 if (li == NULL)
18399 return;
18400 bad = get_tv_string(&li->li_tv);
18401 li = li->li_next;
18402 if (li == NULL)
18403 return;
18404 good = get_tv_string(&li->li_tv);
18405 rettv->vval.v_number = test_edit_score(bad, good);
18406#endif
18407}
18408
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018409#ifdef FEAT_FLOAT
18410/*
18411 * "tan()" function
18412 */
18413 static void
18414f_tan(argvars, rettv)
18415 typval_T *argvars;
18416 typval_T *rettv;
18417{
18418 float_T f;
18419
18420 rettv->v_type = VAR_FLOAT;
18421 if (get_float_arg(argvars, &f) == OK)
18422 rettv->vval.v_float = tan(f);
18423 else
18424 rettv->vval.v_float = 0.0;
18425}
18426
18427/*
18428 * "tanh()" function
18429 */
18430 static void
18431f_tanh(argvars, rettv)
18432 typval_T *argvars;
18433 typval_T *rettv;
18434{
18435 float_T f;
18436
18437 rettv->v_type = VAR_FLOAT;
18438 if (get_float_arg(argvars, &f) == OK)
18439 rettv->vval.v_float = tanh(f);
18440 else
18441 rettv->vval.v_float = 0.0;
18442}
18443#endif
18444
Bram Moolenaard52d9742005-08-21 22:20:28 +000018445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 * "tolower(string)" function
18447 */
18448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 typval_T *argvars;
18451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
18453 char_u *p;
18454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018455 p = vim_strsave(get_tv_string(&argvars[0]));
18456 rettv->v_type = VAR_STRING;
18457 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
18459 if (p != NULL)
18460 while (*p != NUL)
18461 {
18462#ifdef FEAT_MBYTE
18463 int l;
18464
18465 if (enc_utf8)
18466 {
18467 int c, lc;
18468
18469 c = utf_ptr2char(p);
18470 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018471 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 /* TODO: reallocate string when byte count changes. */
18473 if (utf_char2len(lc) == l)
18474 utf_char2bytes(lc, p);
18475 p += l;
18476 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018477 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 p += l; /* skip multi-byte character */
18479 else
18480#endif
18481 {
18482 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18483 ++p;
18484 }
18485 }
18486}
18487
18488/*
18489 * "toupper(string)" function
18490 */
18491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018492f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018493 typval_T *argvars;
18494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018497 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498}
18499
18500/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018501 * "tr(string, fromstr, tostr)" function
18502 */
18503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018505 typval_T *argvars;
18506 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018507{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018508 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018509 char_u *fromstr;
18510 char_u *tostr;
18511 char_u *p;
18512#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018513 int inlen;
18514 int fromlen;
18515 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018516 int idx;
18517 char_u *cpstr;
18518 int cplen;
18519 int first = TRUE;
18520#endif
18521 char_u buf[NUMBUFLEN];
18522 char_u buf2[NUMBUFLEN];
18523 garray_T ga;
18524
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018525 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18527 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018528
18529 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530 rettv->v_type = VAR_STRING;
18531 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018532 if (fromstr == NULL || tostr == NULL)
18533 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018534 ga_init2(&ga, (int)sizeof(char), 80);
18535
18536#ifdef FEAT_MBYTE
18537 if (!has_mbyte)
18538#endif
18539 /* not multi-byte: fromstr and tostr must be the same length */
18540 if (STRLEN(fromstr) != STRLEN(tostr))
18541 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018542#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018543error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018544#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545 EMSG2(_(e_invarg2), fromstr);
18546 ga_clear(&ga);
18547 return;
18548 }
18549
18550 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018551 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018552 {
18553#ifdef FEAT_MBYTE
18554 if (has_mbyte)
18555 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018556 inlen = (*mb_ptr2len)(in_str);
18557 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018558 cplen = inlen;
18559 idx = 0;
18560 for (p = fromstr; *p != NUL; p += fromlen)
18561 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018562 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018563 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018564 {
18565 for (p = tostr; *p != NUL; p += tolen)
18566 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018567 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018568 if (idx-- == 0)
18569 {
18570 cplen = tolen;
18571 cpstr = p;
18572 break;
18573 }
18574 }
18575 if (*p == NUL) /* tostr is shorter than fromstr */
18576 goto error;
18577 break;
18578 }
18579 ++idx;
18580 }
18581
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018582 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018583 {
18584 /* Check that fromstr and tostr have the same number of
18585 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018586 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018587 first = FALSE;
18588 for (p = tostr; *p != NUL; p += tolen)
18589 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018590 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018591 --idx;
18592 }
18593 if (idx != 0)
18594 goto error;
18595 }
18596
18597 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018598 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018599 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018600
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018601 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018602 }
18603 else
18604#endif
18605 {
18606 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018607 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018608 if (p != NULL)
18609 ga_append(&ga, tostr[p - fromstr]);
18610 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018611 ga_append(&ga, *in_str);
18612 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018613 }
18614 }
18615
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018616 /* add a terminating NUL */
18617 ga_grow(&ga, 1);
18618 ga_append(&ga, NUL);
18619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018620 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018621}
18622
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018623#ifdef FEAT_FLOAT
18624/*
18625 * "trunc({float})" function
18626 */
18627 static void
18628f_trunc(argvars, rettv)
18629 typval_T *argvars;
18630 typval_T *rettv;
18631{
18632 float_T f;
18633
18634 rettv->v_type = VAR_FLOAT;
18635 if (get_float_arg(argvars, &f) == OK)
18636 /* trunc() is not in C90, use floor() or ceil() instead. */
18637 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18638 else
18639 rettv->vval.v_float = 0.0;
18640}
18641#endif
18642
Bram Moolenaar8299df92004-07-10 09:47:34 +000018643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 * "type(expr)" function
18645 */
18646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018648 typval_T *argvars;
18649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018651 int n;
18652
18653 switch (argvars[0].v_type)
18654 {
18655 case VAR_NUMBER: n = 0; break;
18656 case VAR_STRING: n = 1; break;
18657 case VAR_FUNC: n = 2; break;
18658 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018659 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018660#ifdef FEAT_FLOAT
18661 case VAR_FLOAT: n = 5; break;
18662#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018663 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18664 }
18665 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666}
18667
18668/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018669 * "undofile(name)" function
18670 */
18671 static void
18672f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018673 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018674 typval_T *rettv;
18675{
18676 rettv->v_type = VAR_STRING;
18677#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018678 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018679 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018680
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018681 if (*fname == NUL)
18682 {
18683 /* If there is no file name there will be no undo file. */
18684 rettv->vval.v_string = NULL;
18685 }
18686 else
18687 {
18688 char_u *ffname = FullName_save(fname, FALSE);
18689
18690 if (ffname != NULL)
18691 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18692 vim_free(ffname);
18693 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018694 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018695#else
18696 rettv->vval.v_string = NULL;
18697#endif
18698}
18699
18700/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018701 * "undotree()" function
18702 */
18703 static void
18704f_undotree(argvars, rettv)
18705 typval_T *argvars UNUSED;
18706 typval_T *rettv;
18707{
18708 if (rettv_dict_alloc(rettv) == OK)
18709 {
18710 dict_T *dict = rettv->vval.v_dict;
18711 list_T *list;
18712
Bram Moolenaar730cde92010-06-27 05:18:54 +020018713 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018714 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018715 dict_add_nr_str(dict, "save_last",
18716 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018717 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18718 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018719 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018720
18721 list = list_alloc();
18722 if (list != NULL)
18723 {
18724 u_eval_tree(curbuf->b_u_oldhead, list);
18725 dict_add_list(dict, "entries", list);
18726 }
18727 }
18728}
18729
18730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018731 * "values(dict)" function
18732 */
18733 static void
18734f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018735 typval_T *argvars;
18736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018737{
18738 dict_list(argvars, rettv, 1);
18739}
18740
18741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 * "virtcol(string)" function
18743 */
18744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018746 typval_T *argvars;
18747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748{
18749 colnr_T vcol = 0;
18750 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018751 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018753 fp = var2fpos(&argvars[0], FALSE, &fnum);
18754 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18755 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 {
18757 getvvcol(curwin, fp, NULL, NULL, &vcol);
18758 ++vcol;
18759 }
18760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018761 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762}
18763
18764/*
18765 * "visualmode()" function
18766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018769 typval_T *argvars UNUSED;
18770 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771{
18772#ifdef FEAT_VISUAL
18773 char_u str[2];
18774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 str[0] = curbuf->b_visual_mode_eval;
18777 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779
18780 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018781 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783#endif
18784}
18785
18786/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018787 * "wildmenumode()" function
18788 */
18789 static void
18790f_wildmenumode(argvars, rettv)
18791 typval_T *argvars UNUSED;
18792 typval_T *rettv UNUSED;
18793{
18794#ifdef FEAT_WILDMENU
18795 if (wild_menu_showing)
18796 rettv->vval.v_number = 1;
18797#endif
18798}
18799
18800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 * "winbufnr(nr)" function
18802 */
18803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 typval_T *argvars;
18806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807{
18808 win_T *wp;
18809
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018810 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018814 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815}
18816
18817/*
18818 * "wincol()" function
18819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018821f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824{
18825 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827}
18828
18829/*
18830 * "winheight(nr)" function
18831 */
18832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018833f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 typval_T *argvars;
18835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836{
18837 win_T *wp;
18838
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018839 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
18847 * "winline()" function
18848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018850f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853{
18854 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018855 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856}
18857
18858/*
18859 * "winnr()" function
18860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018862f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865{
18866 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018867
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018869 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872}
18873
18874/*
18875 * "winrestcmd()" function
18876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018879 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881{
18882#ifdef FEAT_WINDOWS
18883 win_T *wp;
18884 int winnr = 1;
18885 garray_T ga;
18886 char_u buf[50];
18887
18888 ga_init2(&ga, (int)sizeof(char), 70);
18889 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18890 {
18891 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18892 ga_concat(&ga, buf);
18893# ifdef FEAT_VERTSPLIT
18894 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18895 ga_concat(&ga, buf);
18896# endif
18897 ++winnr;
18898 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018899 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906}
18907
18908/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018909 * "winrestview()" function
18910 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018911 static void
18912f_winrestview(argvars, rettv)
18913 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018914 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018915{
18916 dict_T *dict;
18917
18918 if (argvars[0].v_type != VAR_DICT
18919 || (dict = argvars[0].vval.v_dict) == NULL)
18920 EMSG(_(e_invarg));
18921 else
18922 {
18923 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18924 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18925#ifdef FEAT_VIRTUALEDIT
18926 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18927#endif
18928 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018929 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018930
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018931 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018932#ifdef FEAT_DIFF
18933 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18934#endif
18935 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18936 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18937
18938 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018939 win_new_height(curwin, curwin->w_height);
18940# ifdef FEAT_VERTSPLIT
18941 win_new_width(curwin, W_WIDTH(curwin));
18942# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018943 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018944
18945 if (curwin->w_topline == 0)
18946 curwin->w_topline = 1;
18947 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18948 curwin->w_topline = curbuf->b_ml.ml_line_count;
18949#ifdef FEAT_DIFF
18950 check_topfill(curwin, TRUE);
18951#endif
18952 }
18953}
18954
18955/*
18956 * "winsaveview()" function
18957 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018958 static void
18959f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018960 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018961 typval_T *rettv;
18962{
18963 dict_T *dict;
18964
Bram Moolenaara800b422010-06-27 01:15:55 +020018965 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018966 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018967 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018968
18969 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18970 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18971#ifdef FEAT_VIRTUALEDIT
18972 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18973#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018974 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018975 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18976
18977 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18978#ifdef FEAT_DIFF
18979 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18980#endif
18981 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18982 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18983}
18984
18985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 * "winwidth(nr)" function
18987 */
18988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 typval_T *argvars;
18991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992{
18993 win_T *wp;
18994
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018995 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 else
18999#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019000 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003#endif
19004}
19005
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019007 * "writefile()" function
19008 */
19009 static void
19010f_writefile(argvars, rettv)
19011 typval_T *argvars;
19012 typval_T *rettv;
19013{
19014 int binary = FALSE;
19015 char_u *fname;
19016 FILE *fd;
19017 listitem_T *li;
19018 char_u *s;
19019 int ret = 0;
19020 int c;
19021
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019022 if (check_restricted() || check_secure())
19023 return;
19024
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019025 if (argvars[0].v_type != VAR_LIST)
19026 {
19027 EMSG2(_(e_listarg), "writefile()");
19028 return;
19029 }
19030 if (argvars[0].vval.v_list == NULL)
19031 return;
19032
19033 if (argvars[2].v_type != VAR_UNKNOWN
19034 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19035 binary = TRUE;
19036
19037 /* Always open the file in binary mode, library functions have a mind of
19038 * their own about CR-LF conversion. */
19039 fname = get_tv_string(&argvars[1]);
19040 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19041 {
19042 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19043 ret = -1;
19044 }
19045 else
19046 {
19047 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19048 li = li->li_next)
19049 {
19050 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19051 {
19052 if (*s == '\n')
19053 c = putc(NUL, fd);
19054 else
19055 c = putc(*s, fd);
19056 if (c == EOF)
19057 {
19058 ret = -1;
19059 break;
19060 }
19061 }
19062 if (!binary || li->li_next != NULL)
19063 if (putc('\n', fd) == EOF)
19064 {
19065 ret = -1;
19066 break;
19067 }
19068 if (ret < 0)
19069 {
19070 EMSG(_(e_write));
19071 break;
19072 }
19073 }
19074 fclose(fd);
19075 }
19076
19077 rettv->vval.v_number = ret;
19078}
19079
19080/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019081 * "xor(expr, expr)" function
19082 */
19083 static void
19084f_xor(argvars, rettv)
19085 typval_T *argvars;
19086 typval_T *rettv;
19087{
19088 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19089 ^ get_tv_number_chk(&argvars[1], NULL);
19090}
19091
19092
19093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019095 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 */
19097 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019098var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019100 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019101 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019105 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106
Bram Moolenaara5525202006-03-02 22:52:09 +000019107 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019108 if (varp->v_type == VAR_LIST)
19109 {
19110 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019111 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019112 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019113 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019114
19115 l = varp->vval.v_list;
19116 if (l == NULL)
19117 return NULL;
19118
19119 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019120 pos.lnum = list_find_nr(l, 0L, &error);
19121 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019122 return NULL; /* invalid line number */
19123
19124 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019125 pos.col = list_find_nr(l, 1L, &error);
19126 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019127 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019128 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019129
19130 /* We accept "$" for the column number: last column. */
19131 li = list_find(l, 1L);
19132 if (li != NULL && li->li_tv.v_type == VAR_STRING
19133 && li->li_tv.vval.v_string != NULL
19134 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19135 pos.col = len + 1;
19136
Bram Moolenaara5525202006-03-02 22:52:09 +000019137 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019138 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019139 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019140 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019141
Bram Moolenaara5525202006-03-02 22:52:09 +000019142#ifdef FEAT_VIRTUALEDIT
19143 /* Get the virtual offset. Defaults to zero. */
19144 pos.coladd = list_find_nr(l, 2L, &error);
19145 if (error)
19146 pos.coladd = 0;
19147#endif
19148
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019149 return &pos;
19150 }
19151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019152 name = get_tv_string_chk(varp);
19153 if (name == NULL)
19154 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019155 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019157#ifdef FEAT_VISUAL
19158 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19159 {
19160 if (VIsual_active)
19161 return &VIsual;
19162 return &curwin->w_cursor;
19163 }
19164#endif
19165 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019167 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19169 return NULL;
19170 return pp;
19171 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019172
19173#ifdef FEAT_VIRTUALEDIT
19174 pos.coladd = 0;
19175#endif
19176
Bram Moolenaar477933c2007-07-17 14:32:23 +000019177 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019178 {
19179 pos.col = 0;
19180 if (name[1] == '0') /* "w0": first visible line */
19181 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019182 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019183 pos.lnum = curwin->w_topline;
19184 return &pos;
19185 }
19186 else if (name[1] == '$') /* "w$": last visible line */
19187 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019188 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019189 pos.lnum = curwin->w_botline - 1;
19190 return &pos;
19191 }
19192 }
19193 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019195 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 {
19197 pos.lnum = curbuf->b_ml.ml_line_count;
19198 pos.col = 0;
19199 }
19200 else
19201 {
19202 pos.lnum = curwin->w_cursor.lnum;
19203 pos.col = (colnr_T)STRLEN(ml_get_curline());
19204 }
19205 return &pos;
19206 }
19207 return NULL;
19208}
19209
19210/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019211 * Convert list in "arg" into a position and optional file number.
19212 * When "fnump" is NULL there is no file number, only 3 items.
19213 * Note that the column is passed on as-is, the caller may want to decrement
19214 * it to use 1 for the first column.
19215 * Return FAIL when conversion is not possible, doesn't check the position for
19216 * validity.
19217 */
19218 static int
19219list2fpos(arg, posp, fnump)
19220 typval_T *arg;
19221 pos_T *posp;
19222 int *fnump;
19223{
19224 list_T *l = arg->vval.v_list;
19225 long i = 0;
19226 long n;
19227
Bram Moolenaarbde35262006-07-23 20:12:24 +000019228 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19229 * when "fnump" isn't NULL and "coladd" is optional. */
19230 if (arg->v_type != VAR_LIST
19231 || l == NULL
19232 || l->lv_len < (fnump == NULL ? 2 : 3)
19233 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019234 return FAIL;
19235
19236 if (fnump != NULL)
19237 {
19238 n = list_find_nr(l, i++, NULL); /* fnum */
19239 if (n < 0)
19240 return FAIL;
19241 if (n == 0)
19242 n = curbuf->b_fnum; /* current buffer */
19243 *fnump = n;
19244 }
19245
19246 n = list_find_nr(l, i++, NULL); /* lnum */
19247 if (n < 0)
19248 return FAIL;
19249 posp->lnum = n;
19250
19251 n = list_find_nr(l, i++, NULL); /* col */
19252 if (n < 0)
19253 return FAIL;
19254 posp->col = n;
19255
19256#ifdef FEAT_VIRTUALEDIT
19257 n = list_find_nr(l, i, NULL);
19258 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019259 posp->coladd = 0;
19260 else
19261 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019262#endif
19263
19264 return OK;
19265}
19266
19267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 * Get the length of an environment variable name.
19269 * Advance "arg" to the first character after the name.
19270 * Return 0 for error.
19271 */
19272 static int
19273get_env_len(arg)
19274 char_u **arg;
19275{
19276 char_u *p;
19277 int len;
19278
19279 for (p = *arg; vim_isIDc(*p); ++p)
19280 ;
19281 if (p == *arg) /* no name found */
19282 return 0;
19283
19284 len = (int)(p - *arg);
19285 *arg = p;
19286 return len;
19287}
19288
19289/*
19290 * Get the length of the name of a function or internal variable.
19291 * "arg" is advanced to the first non-white character after the name.
19292 * Return 0 if something is wrong.
19293 */
19294 static int
19295get_id_len(arg)
19296 char_u **arg;
19297{
19298 char_u *p;
19299 int len;
19300
19301 /* Find the end of the name. */
19302 for (p = *arg; eval_isnamec(*p); ++p)
19303 ;
19304 if (p == *arg) /* no name found */
19305 return 0;
19306
19307 len = (int)(p - *arg);
19308 *arg = skipwhite(p);
19309
19310 return len;
19311}
19312
19313/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019314 * Get the length of the name of a variable or function.
19315 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019317 * Return -1 if curly braces expansion failed.
19318 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 * If the name contains 'magic' {}'s, expand them and return the
19320 * expanded name in an allocated string via 'alias' - caller must free.
19321 */
19322 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019323get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 char_u **arg;
19325 char_u **alias;
19326 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019327 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328{
19329 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 char_u *p;
19331 char_u *expr_start;
19332 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333
19334 *alias = NULL; /* default to no alias */
19335
19336 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19337 && (*arg)[2] == (int)KE_SNR)
19338 {
19339 /* hard coded <SNR>, already translated */
19340 *arg += 3;
19341 return get_id_len(arg) + 3;
19342 }
19343 len = eval_fname_script(*arg);
19344 if (len > 0)
19345 {
19346 /* literal "<SID>", "s:" or "<SNR>" */
19347 *arg += len;
19348 }
19349
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019353 p = find_name_end(*arg, &expr_start, &expr_end,
19354 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 if (expr_start != NULL)
19356 {
19357 char_u *temp_string;
19358
19359 if (!evaluate)
19360 {
19361 len += (int)(p - *arg);
19362 *arg = skipwhite(p);
19363 return len;
19364 }
19365
19366 /*
19367 * Include any <SID> etc in the expanded string:
19368 * Thus the -len here.
19369 */
19370 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19371 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019372 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 *alias = temp_string;
19374 *arg = skipwhite(p);
19375 return (int)STRLEN(temp_string);
19376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377
19378 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019379 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 EMSG2(_(e_invexpr2), *arg);
19381
19382 return len;
19383}
19384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385/*
19386 * Find the end of a variable or function name, taking care of magic braces.
19387 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19388 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019389 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019390 * Return a pointer to just after the name. Equal to "arg" if there is no
19391 * valid name.
19392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019394find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395 char_u *arg;
19396 char_u **expr_start;
19397 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019398 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019400 int mb_nest = 0;
19401 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 char_u *p;
19403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019404 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 *expr_start = NULL;
19407 *expr_end = NULL;
19408 }
19409
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019410 /* Quick check for valid starting character. */
19411 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19412 return arg;
19413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 for (p = arg; *p != NUL
19415 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019416 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019417 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019419 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019420 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019421 if (*p == '\'')
19422 {
19423 /* skip over 'string' to avoid counting [ and ] inside it. */
19424 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19425 ;
19426 if (*p == NUL)
19427 break;
19428 }
19429 else if (*p == '"')
19430 {
19431 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19432 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19433 if (*p == '\\' && p[1] != NUL)
19434 ++p;
19435 if (*p == NUL)
19436 break;
19437 }
19438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019439 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019441 if (*p == '[')
19442 ++br_nest;
19443 else if (*p == ']')
19444 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019447 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019449 if (*p == '{')
19450 {
19451 mb_nest++;
19452 if (expr_start != NULL && *expr_start == NULL)
19453 *expr_start = p;
19454 }
19455 else if (*p == '}')
19456 {
19457 mb_nest--;
19458 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19459 *expr_end = p;
19460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 }
19463
19464 return p;
19465}
19466
19467/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019468 * Expands out the 'magic' {}'s in a variable/function name.
19469 * Note that this can call itself recursively, to deal with
19470 * constructs like foo{bar}{baz}{bam}
19471 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19472 * "in_start" ^
19473 * "expr_start" ^
19474 * "expr_end" ^
19475 * "in_end" ^
19476 *
19477 * Returns a new allocated string, which the caller must free.
19478 * Returns NULL for failure.
19479 */
19480 static char_u *
19481make_expanded_name(in_start, expr_start, expr_end, in_end)
19482 char_u *in_start;
19483 char_u *expr_start;
19484 char_u *expr_end;
19485 char_u *in_end;
19486{
19487 char_u c1;
19488 char_u *retval = NULL;
19489 char_u *temp_result;
19490 char_u *nextcmd = NULL;
19491
19492 if (expr_end == NULL || in_end == NULL)
19493 return NULL;
19494 *expr_start = NUL;
19495 *expr_end = NUL;
19496 c1 = *in_end;
19497 *in_end = NUL;
19498
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019499 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019500 if (temp_result != NULL && nextcmd == NULL)
19501 {
19502 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19503 + (in_end - expr_end) + 1));
19504 if (retval != NULL)
19505 {
19506 STRCPY(retval, in_start);
19507 STRCAT(retval, temp_result);
19508 STRCAT(retval, expr_end + 1);
19509 }
19510 }
19511 vim_free(temp_result);
19512
19513 *in_end = c1; /* put char back for error messages */
19514 *expr_start = '{';
19515 *expr_end = '}';
19516
19517 if (retval != NULL)
19518 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019519 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019520 if (expr_start != NULL)
19521 {
19522 /* Further expansion! */
19523 temp_result = make_expanded_name(retval, expr_start,
19524 expr_end, temp_result);
19525 vim_free(retval);
19526 retval = temp_result;
19527 }
19528 }
19529
19530 return retval;
19531}
19532
19533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019535 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 */
19537 static int
19538eval_isnamec(c)
19539 int c;
19540{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019541 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19542}
19543
19544/*
19545 * Return TRUE if character "c" can be used as the first character in a
19546 * variable or function name (excluding '{' and '}').
19547 */
19548 static int
19549eval_isnamec1(c)
19550 int c;
19551{
19552 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553}
19554
19555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 * Set number v: variable to "val".
19557 */
19558 void
19559set_vim_var_nr(idx, val)
19560 int idx;
19561 long val;
19562{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019563 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564}
19565
19566/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019567 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 */
19569 long
19570get_vim_var_nr(idx)
19571 int idx;
19572{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019573 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574}
19575
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019576/*
19577 * Get string v: variable value. Uses a static buffer, can only be used once.
19578 */
19579 char_u *
19580get_vim_var_str(idx)
19581 int idx;
19582{
19583 return get_tv_string(&vimvars[idx].vv_tv);
19584}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019585
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019587 * Get List v: variable value. Caller must take care of reference count when
19588 * needed.
19589 */
19590 list_T *
19591get_vim_var_list(idx)
19592 int idx;
19593{
19594 return vimvars[idx].vv_list;
19595}
19596
19597/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019598 * Set v:char to character "c".
19599 */
19600 void
19601set_vim_var_char(c)
19602 int c;
19603{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019604 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019605
19606#ifdef FEAT_MBYTE
19607 if (has_mbyte)
19608 buf[(*mb_char2bytes)(c, buf)] = NUL;
19609 else
19610#endif
19611 {
19612 buf[0] = c;
19613 buf[1] = NUL;
19614 }
19615 set_vim_var_string(VV_CHAR, buf, -1);
19616}
19617
19618/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019619 * Set v:count to "count" and v:count1 to "count1".
19620 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 */
19622 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019623set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 long count;
19625 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019626 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019628 if (set_prevcount)
19629 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019630 vimvars[VV_COUNT].vv_nr = count;
19631 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632}
19633
19634/*
19635 * Set string v: variable to a copy of "val".
19636 */
19637 void
19638set_vim_var_string(idx, val, len)
19639 int idx;
19640 char_u *val;
19641 int len; /* length of "val" to use or -1 (whole string) */
19642{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019643 /* Need to do this (at least) once, since we can't initialize a union.
19644 * Will always be invoked when "v:progname" is set. */
19645 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19646
Bram Moolenaare9a41262005-01-15 22:18:47 +000019647 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019649 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019651 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019653 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654}
19655
19656/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019657 * Set List v: variable to "val".
19658 */
19659 void
19660set_vim_var_list(idx, val)
19661 int idx;
19662 list_T *val;
19663{
19664 list_unref(vimvars[idx].vv_list);
19665 vimvars[idx].vv_list = val;
19666 if (val != NULL)
19667 ++val->lv_refcount;
19668}
19669
19670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 * Set v:register if needed.
19672 */
19673 void
19674set_reg_var(c)
19675 int c;
19676{
19677 char_u regname;
19678
19679 if (c == 0 || c == ' ')
19680 regname = '"';
19681 else
19682 regname = c;
19683 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019684 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 set_vim_var_string(VV_REG, &regname, 1);
19686}
19687
19688/*
19689 * Get or set v:exception. If "oldval" == NULL, return the current value.
19690 * Otherwise, restore the value to "oldval" and return NULL.
19691 * Must always be called in pairs to save and restore v:exception! Does not
19692 * take care of memory allocations.
19693 */
19694 char_u *
19695v_exception(oldval)
19696 char_u *oldval;
19697{
19698 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019699 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700
Bram Moolenaare9a41262005-01-15 22:18:47 +000019701 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 return NULL;
19703}
19704
19705/*
19706 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19707 * Otherwise, restore the value to "oldval" and return NULL.
19708 * Must always be called in pairs to save and restore v:throwpoint! Does not
19709 * take care of memory allocations.
19710 */
19711 char_u *
19712v_throwpoint(oldval)
19713 char_u *oldval;
19714{
19715 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019716 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717
Bram Moolenaare9a41262005-01-15 22:18:47 +000019718 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 return NULL;
19720}
19721
19722#if defined(FEAT_AUTOCMD) || defined(PROTO)
19723/*
19724 * Set v:cmdarg.
19725 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19726 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19727 * Must always be called in pairs!
19728 */
19729 char_u *
19730set_cmdarg(eap, oldarg)
19731 exarg_T *eap;
19732 char_u *oldarg;
19733{
19734 char_u *oldval;
19735 char_u *newval;
19736 unsigned len;
19737
Bram Moolenaare9a41262005-01-15 22:18:47 +000019738 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019739 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019741 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019742 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019743 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 }
19745
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019746 if (eap->force_bin == FORCE_BIN)
19747 len = 6;
19748 else if (eap->force_bin == FORCE_NOBIN)
19749 len = 8;
19750 else
19751 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019752
19753 if (eap->read_edit)
19754 len += 7;
19755
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019756 if (eap->force_ff != 0)
19757 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19758# ifdef FEAT_MBYTE
19759 if (eap->force_enc != 0)
19760 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019761 if (eap->bad_char != 0)
19762 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019763# endif
19764
19765 newval = alloc(len + 1);
19766 if (newval == NULL)
19767 return NULL;
19768
19769 if (eap->force_bin == FORCE_BIN)
19770 sprintf((char *)newval, " ++bin");
19771 else if (eap->force_bin == FORCE_NOBIN)
19772 sprintf((char *)newval, " ++nobin");
19773 else
19774 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019775
19776 if (eap->read_edit)
19777 STRCAT(newval, " ++edit");
19778
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019779 if (eap->force_ff != 0)
19780 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19781 eap->cmd + eap->force_ff);
19782# ifdef FEAT_MBYTE
19783 if (eap->force_enc != 0)
19784 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19785 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019786 if (eap->bad_char == BAD_KEEP)
19787 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19788 else if (eap->bad_char == BAD_DROP)
19789 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19790 else if (eap->bad_char != 0)
19791 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019792# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019793 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019794 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795}
19796#endif
19797
19798/*
19799 * Get the value of internal variable "name".
19800 * Return OK or FAIL.
19801 */
19802 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019803get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 char_u *name;
19805 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019807 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019808 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809{
19810 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019811 typval_T *tv = NULL;
19812 typval_T atv;
19813 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
19816 /* truncate the name, so that we can use strcmp() */
19817 cc = name[len];
19818 name[len] = NUL;
19819
19820 /*
19821 * Check for "b:changedtick".
19822 */
19823 if (STRCMP(name, "b:changedtick") == 0)
19824 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019825 atv.v_type = VAR_NUMBER;
19826 atv.vval.v_number = curbuf->b_changedtick;
19827 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 }
19829
19830 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 * Check for user-defined variables.
19832 */
19833 else
19834 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019835 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019837 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 }
19839
Bram Moolenaare9a41262005-01-15 22:18:47 +000019840 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019842 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 ret = FAIL;
19845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019847 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848
19849 name[len] = cc;
19850
19851 return ret;
19852}
19853
19854/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019855 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19856 * Also handle function call with Funcref variable: func(expr)
19857 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19858 */
19859 static int
19860handle_subscript(arg, rettv, evaluate, verbose)
19861 char_u **arg;
19862 typval_T *rettv;
19863 int evaluate; /* do more than finding the end */
19864 int verbose; /* give error messages */
19865{
19866 int ret = OK;
19867 dict_T *selfdict = NULL;
19868 char_u *s;
19869 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019870 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019871
19872 while (ret == OK
19873 && (**arg == '['
19874 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019875 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019876 && !vim_iswhite(*(*arg - 1)))
19877 {
19878 if (**arg == '(')
19879 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019880 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019881 if (evaluate)
19882 {
19883 functv = *rettv;
19884 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019886 /* Invoke the function. Recursive! */
19887 s = functv.vval.v_string;
19888 }
19889 else
19890 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019891 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019892 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19893 &len, evaluate, selfdict);
19894
19895 /* Clear the funcref afterwards, so that deleting it while
19896 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019897 if (evaluate)
19898 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019899
19900 /* Stop the expression evaluation when immediately aborting on
19901 * error, or when an interrupt occurred or an exception was thrown
19902 * but not caught. */
19903 if (aborting())
19904 {
19905 if (ret == OK)
19906 clear_tv(rettv);
19907 ret = FAIL;
19908 }
19909 dict_unref(selfdict);
19910 selfdict = NULL;
19911 }
19912 else /* **arg == '[' || **arg == '.' */
19913 {
19914 dict_unref(selfdict);
19915 if (rettv->v_type == VAR_DICT)
19916 {
19917 selfdict = rettv->vval.v_dict;
19918 if (selfdict != NULL)
19919 ++selfdict->dv_refcount;
19920 }
19921 else
19922 selfdict = NULL;
19923 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19924 {
19925 clear_tv(rettv);
19926 ret = FAIL;
19927 }
19928 }
19929 }
19930 dict_unref(selfdict);
19931 return ret;
19932}
19933
19934/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019935 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019936 * value).
19937 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019940{
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019942}
19943
19944/*
19945 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 * The string "s" must have been allocated, it is consumed.
19947 * Return NULL for out of memory, the variable otherwise.
19948 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 char_u *s;
19952{
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 rettv = alloc_tv();
19956 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 rettv->v_type = VAR_STRING;
19959 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 }
19961 else
19962 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964}
19965
19966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019967 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019969 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972{
19973 if (varp != NULL)
19974 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 switch (varp->v_type)
19976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019978 func_unref(varp->vval.v_string);
19979 /*FALLTHROUGH*/
19980 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019981 vim_free(varp->vval.v_string);
19982 break;
19983 case VAR_LIST:
19984 list_unref(varp->vval.v_list);
19985 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019986 case VAR_DICT:
19987 dict_unref(varp->vval.v_dict);
19988 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019989 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019990#ifdef FEAT_FLOAT
19991 case VAR_FLOAT:
19992#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019993 case VAR_UNKNOWN:
19994 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019996 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019997 break;
19998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 vim_free(varp);
20000 }
20001}
20002
20003/*
20004 * Free the memory for a variable value and set the value to NULL or 0.
20005 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020006 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009{
20010 if (varp != NULL)
20011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020012 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020014 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020015 func_unref(varp->vval.v_string);
20016 /*FALLTHROUGH*/
20017 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 vim_free(varp->vval.v_string);
20019 varp->vval.v_string = NULL;
20020 break;
20021 case VAR_LIST:
20022 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020023 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020025 case VAR_DICT:
20026 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020027 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020028 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020030 varp->vval.v_number = 0;
20031 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020032#ifdef FEAT_FLOAT
20033 case VAR_FLOAT:
20034 varp->vval.v_float = 0.0;
20035 break;
20036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 case VAR_UNKNOWN:
20038 break;
20039 default:
20040 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020042 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 }
20044}
20045
20046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 * Set the value of a variable to NULL without freeing items.
20048 */
20049 static void
20050init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052{
20053 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055}
20056
20057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 * Get the number value of a variable.
20059 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020060 * For incompatible types, return 0.
20061 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20062 * caller of incompatible types: it sets *denote to TRUE if "denote"
20063 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 */
20065 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020069 int error = FALSE;
20070
20071 return get_tv_number_chk(varp, &error); /* return 0L on error */
20072}
20073
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020074 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020075get_tv_number_chk(varp, denote)
20076 typval_T *varp;
20077 int *denote;
20078{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020081 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020083 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020084 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020085#ifdef FEAT_FLOAT
20086 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020087 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020088 break;
20089#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020091 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020092 break;
20093 case VAR_STRING:
20094 if (varp->vval.v_string != NULL)
20095 vim_str2nr(varp->vval.v_string, NULL, NULL,
20096 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020097 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020098 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020099 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020100 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020101 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020102 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020103 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020104 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020105 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020106 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020108 if (denote == NULL) /* useful for values that must be unsigned */
20109 n = -1;
20110 else
20111 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020112 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113}
20114
20115/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020116 * Get the lnum from the first argument.
20117 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020118 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 */
20120 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123{
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 linenr_T lnum;
20126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020127 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 if (lnum == 0) /* no valid number, try using line() */
20129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130 rettv.v_type = VAR_NUMBER;
20131 f_line(argvars, &rettv);
20132 lnum = rettv.vval.v_number;
20133 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 }
20135 return lnum;
20136}
20137
20138/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020139 * Get the lnum from the first argument.
20140 * Also accepts "$", then "buf" is used.
20141 * Returns 0 on error.
20142 */
20143 static linenr_T
20144get_tv_lnum_buf(argvars, buf)
20145 typval_T *argvars;
20146 buf_T *buf;
20147{
20148 if (argvars[0].v_type == VAR_STRING
20149 && argvars[0].vval.v_string != NULL
20150 && argvars[0].vval.v_string[0] == '$'
20151 && buf != NULL)
20152 return buf->b_ml.ml_line_count;
20153 return get_tv_number_chk(&argvars[0], NULL);
20154}
20155
20156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 * Get the string value of a variable.
20158 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020159 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20160 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 * If the String variable has never been set, return an empty string.
20162 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020163 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20164 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 */
20166 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169{
20170 static char_u mybuf[NUMBUFLEN];
20171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173}
20174
20175 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020178 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020180 char_u *res = get_tv_string_buf_chk(varp, buf);
20181
20182 return res != NULL ? res : (char_u *)"";
20183}
20184
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020185 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020186get_tv_string_chk(varp)
20187 typval_T *varp;
20188{
20189 static char_u mybuf[NUMBUFLEN];
20190
20191 return get_tv_string_buf_chk(varp, mybuf);
20192}
20193
20194 static char_u *
20195get_tv_string_buf_chk(varp, buf)
20196 typval_T *varp;
20197 char_u *buf;
20198{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020199 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020201 case VAR_NUMBER:
20202 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20203 return buf;
20204 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020205 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020206 break;
20207 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020208 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020209 break;
20210 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020211 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020212 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020213#ifdef FEAT_FLOAT
20214 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020215 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020216 break;
20217#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020218 case VAR_STRING:
20219 if (varp->vval.v_string != NULL)
20220 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020221 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020222 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020226 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227}
20228
20229/*
20230 * Find variable "name" in the list of variables.
20231 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020232 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020233 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020237find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020239 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020240 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244
Bram Moolenaara7043832005-01-21 11:56:39 +000020245 ht = find_var_ht(name, &varname);
20246 if (htp != NULL)
20247 *htp = ht;
20248 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020250 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251}
20252
20253/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020254 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020255 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020258find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020260 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020261 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020262 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020263{
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 hashitem_T *hi;
20265
20266 if (*varname == NUL)
20267 {
20268 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020269 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020271 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020272 case 'g': return &globvars_var;
20273 case 'v': return &vimvars_var;
20274 case 'b': return &curbuf->b_bufvar;
20275 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020276#ifdef FEAT_WINDOWS
20277 case 't': return &curtab->tp_winvar;
20278#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020279 case 'l': return current_funccal == NULL
20280 ? NULL : &current_funccal->l_vars_var;
20281 case 'a': return current_funccal == NULL
20282 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 }
20284 return NULL;
20285 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020286
20287 hi = hash_find(ht, varname);
20288 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 {
20290 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020291 * worked find the variable again. Don't auto-load a script if it was
20292 * loaded already, otherwise it would be loaded every time when
20293 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020294 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020295 {
20296 /* Note: script_autoload() may make "hi" invalid. It must either
20297 * be obtained again or not used. */
20298 if (!script_autoload(varname, FALSE) || aborting())
20299 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020300 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020301 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020302 if (HASHITEM_EMPTY(hi))
20303 return NULL;
20304 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020306}
20307
20308/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020310 * Set "varname" to the start of name without ':'.
20311 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020313find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 char_u *name;
20315 char_u **varname;
20316{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020317 hashitem_T *hi;
20318
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (name[1] != ':')
20320 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020321 /* The name must not start with a colon or #. */
20322 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 return NULL;
20324 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020325
20326 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020327 hi = hash_find(&compat_hashtab, name);
20328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020329 return &compat_hashtab;
20330
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 return &globvarht; /* global variable */
20333 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 }
20335 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020336 if (*name == 'g') /* global variable */
20337 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020338 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20339 */
20340 if (vim_strchr(name + 2, ':') != NULL
20341 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020342 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020344 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020346 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020347#ifdef FEAT_WINDOWS
20348 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020349 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020350#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 if (*name == 'v') /* v: variable */
20352 return &vimvarht;
20353 if (*name == 'a' && current_funccal != NULL) /* function argument */
20354 return &current_funccal->l_avars.dv_hashtab;
20355 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20356 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 if (*name == 's' /* script variable */
20358 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20359 return &SCRIPT_VARS(current_SID);
20360 return NULL;
20361}
20362
20363/*
20364 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020365 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 * Returns NULL when it doesn't exist.
20367 */
20368 char_u *
20369get_var_value(name)
20370 char_u *name;
20371{
Bram Moolenaar33570922005-01-25 22:26:29 +000020372 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020374 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 if (v == NULL)
20376 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020377 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378}
20379
20380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 * sourcing this script and when executing functions defined in the script.
20383 */
20384 void
20385new_script_vars(id)
20386 scid_T id;
20387{
Bram Moolenaara7043832005-01-21 11:56:39 +000020388 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 hashtab_T *ht;
20390 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020391
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020394 /* Re-allocating ga_data means that an ht_array pointing to
20395 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020396 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020397 for (i = 1; i <= ga_scripts.ga_len; ++i)
20398 {
20399 ht = &SCRIPT_VARS(i);
20400 if (ht->ht_mask == HT_INIT_SIZE - 1)
20401 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020402 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020404 }
20405
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 while (ga_scripts.ga_len < id)
20407 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020408 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020409 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020410 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 }
20413 }
20414}
20415
20416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20418 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 */
20420 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020421init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020422 dict_T *dict;
20423 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020424 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425{
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020427 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020428 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020429 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020430 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 dict_var->di_tv.vval.v_dict = dict;
20432 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020433 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20435 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436}
20437
20438/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020439 * Unreference a dictionary initialized by init_var_dict().
20440 */
20441 void
20442unref_var_dict(dict)
20443 dict_T *dict;
20444{
20445 /* Now the dict needs to be freed if no one else is using it, go back to
20446 * normal reference counting. */
20447 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20448 dict_unref(dict);
20449}
20450
20451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020453 * Frees all allocated variables and the value they contain.
20454 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 */
20456 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020457vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 hashtab_T *ht;
20459{
20460 vars_clear_ext(ht, TRUE);
20461}
20462
20463/*
20464 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20465 */
20466 static void
20467vars_clear_ext(ht, free_val)
20468 hashtab_T *ht;
20469 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470{
Bram Moolenaara7043832005-01-21 11:56:39 +000020471 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 hashitem_T *hi;
20473 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474
Bram Moolenaar33570922005-01-25 22:26:29 +000020475 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020476 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020477 for (hi = ht->ht_array; todo > 0; ++hi)
20478 {
20479 if (!HASHITEM_EMPTY(hi))
20480 {
20481 --todo;
20482
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020484 * ht_array might change then. hash_clear() takes care of it
20485 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 v = HI2DI(hi);
20487 if (free_val)
20488 clear_tv(&v->di_tv);
20489 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20490 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020491 }
20492 }
20493 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020494 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495}
20496
Bram Moolenaara7043832005-01-21 11:56:39 +000020497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020498 * Delete a variable from hashtab "ht" at item "hi".
20499 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020502delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 hashtab_T *ht;
20504 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505{
Bram Moolenaar33570922005-01-25 22:26:29 +000020506 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020507
20508 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020509 clear_tv(&di->di_tv);
20510 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511}
20512
20513/*
20514 * List the value of one internal variable.
20515 */
20516 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020517list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020518 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020520 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020522 char_u *tofree;
20523 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020524 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020525
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020526 current_copyID += COPYID_INC;
20527 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020528 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020529 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020530 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531}
20532
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020534list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 char_u *prefix;
20536 char_u *name;
20537 int type;
20538 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020539 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540{
Bram Moolenaar31859182007-08-14 20:41:13 +000020541 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20542 msg_start();
20543 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 if (name != NULL) /* "a:" vars don't have a name stored */
20545 msg_puts(name);
20546 msg_putchar(' ');
20547 msg_advance(22);
20548 if (type == VAR_NUMBER)
20549 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020550 else if (type == VAR_FUNC)
20551 msg_putchar('*');
20552 else if (type == VAR_LIST)
20553 {
20554 msg_putchar('[');
20555 if (*string == '[')
20556 ++string;
20557 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020558 else if (type == VAR_DICT)
20559 {
20560 msg_putchar('{');
20561 if (*string == '{')
20562 ++string;
20563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 else
20565 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020566
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020568
20569 if (type == VAR_FUNC)
20570 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020571 if (*first)
20572 {
20573 msg_clr_eos();
20574 *first = FALSE;
20575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576}
20577
20578/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020579 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 * If the variable already exists, the value is updated.
20581 * Otherwise the variable is created.
20582 */
20583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020584set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020587 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588{
Bram Moolenaar33570922005-01-25 22:26:29 +000020589 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020593 ht = find_var_ht(name, &varname);
20594 if (ht == NULL || *varname == NUL)
20595 {
20596 EMSG2(_(e_illvar), name);
20597 return;
20598 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020599 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020600
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020601 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20602 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020606 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020607 if (var_check_ro(v->di_flags, name)
20608 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 return;
20610 if (v->di_tv.v_type != tv->v_type
20611 && !((v->di_tv.v_type == VAR_STRING
20612 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020613 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020614 || tv->v_type == VAR_NUMBER))
20615#ifdef FEAT_FLOAT
20616 && !((v->di_tv.v_type == VAR_NUMBER
20617 || v->di_tv.v_type == VAR_FLOAT)
20618 && (tv->v_type == VAR_NUMBER
20619 || tv->v_type == VAR_FLOAT))
20620#endif
20621 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020622 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020623 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020624 return;
20625 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020626
20627 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020628 * Handle setting internal v: variables separately: we don't change
20629 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020630 */
20631 if (ht == &vimvarht)
20632 {
20633 if (v->di_tv.v_type == VAR_STRING)
20634 {
20635 vim_free(v->di_tv.vval.v_string);
20636 if (copy || tv->v_type != VAR_STRING)
20637 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20638 else
20639 {
20640 /* Take over the string to avoid an extra alloc/free. */
20641 v->di_tv.vval.v_string = tv->vval.v_string;
20642 tv->vval.v_string = NULL;
20643 }
20644 }
20645 else if (v->di_tv.v_type != VAR_NUMBER)
20646 EMSG2(_(e_intern2), "set_var()");
20647 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020648 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020650 if (STRCMP(varname, "searchforward") == 0)
20651 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020652#ifdef FEAT_SEARCH_EXTRA
20653 else if (STRCMP(varname, "hlsearch") == 0)
20654 {
20655 no_hlsearch = !v->di_tv.vval.v_number;
20656 redraw_all_later(SOME_VALID);
20657 }
20658#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020659 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020660 return;
20661 }
20662
20663 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 }
20665 else /* add a new variable */
20666 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020667 /* Can't add "v:" variable. */
20668 if (ht == &vimvarht)
20669 {
20670 EMSG2(_(e_illvar), name);
20671 return;
20672 }
20673
Bram Moolenaar92124a32005-06-17 22:03:40 +000020674 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020675 if (!valid_varname(varname))
20676 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020678 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20679 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020680 if (v == NULL)
20681 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020683 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020685 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 return;
20687 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020688 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020690
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020691 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020693 else
20694 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020695 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020696 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020697 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699}
20700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020701/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020702 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020703 * Also give an error message.
20704 */
20705 static int
20706var_check_ro(flags, name)
20707 int flags;
20708 char_u *name;
20709{
20710 if (flags & DI_FLAGS_RO)
20711 {
20712 EMSG2(_(e_readonlyvar), name);
20713 return TRUE;
20714 }
20715 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20716 {
20717 EMSG2(_(e_readonlysbx), name);
20718 return TRUE;
20719 }
20720 return FALSE;
20721}
20722
20723/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020724 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20725 * Also give an error message.
20726 */
20727 static int
20728var_check_fixed(flags, name)
20729 int flags;
20730 char_u *name;
20731{
20732 if (flags & DI_FLAGS_FIX)
20733 {
20734 EMSG2(_("E795: Cannot delete variable %s"), name);
20735 return TRUE;
20736 }
20737 return FALSE;
20738}
20739
20740/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020741 * Check if a funcref is assigned to a valid variable name.
20742 * Return TRUE and give an error if not.
20743 */
20744 static int
20745var_check_func_name(name, new_var)
20746 char_u *name; /* points to start of variable name */
20747 int new_var; /* TRUE when creating the variable */
20748{
20749 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20750 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20751 ? name[2] : name[0]))
20752 {
20753 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20754 name);
20755 return TRUE;
20756 }
20757 /* Don't allow hiding a function. When "v" is not NULL we might be
20758 * assigning another function to the same var, the type is checked
20759 * below. */
20760 if (new_var && function_exists(name))
20761 {
20762 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20763 name);
20764 return TRUE;
20765 }
20766 return FALSE;
20767}
20768
20769/*
20770 * Check if a variable name is valid.
20771 * Return FALSE and give an error if not.
20772 */
20773 static int
20774valid_varname(varname)
20775 char_u *varname;
20776{
20777 char_u *p;
20778
20779 for (p = varname; *p != NUL; ++p)
20780 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20781 && *p != AUTOLOAD_CHAR)
20782 {
20783 EMSG2(_(e_illvar), varname);
20784 return FALSE;
20785 }
20786 return TRUE;
20787}
20788
20789/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020790 * Return TRUE if typeval "tv" is set to be locked (immutable).
20791 * Also give an error message, using "name".
20792 */
20793 static int
20794tv_check_lock(lock, name)
20795 int lock;
20796 char_u *name;
20797{
20798 if (lock & VAR_LOCKED)
20799 {
20800 EMSG2(_("E741: Value is locked: %s"),
20801 name == NULL ? (char_u *)_("Unknown") : name);
20802 return TRUE;
20803 }
20804 if (lock & VAR_FIXED)
20805 {
20806 EMSG2(_("E742: Cannot change value of %s"),
20807 name == NULL ? (char_u *)_("Unknown") : name);
20808 return TRUE;
20809 }
20810 return FALSE;
20811}
20812
20813/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020814 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020815 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020816 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020817 * It is OK for "from" and "to" to point to the same item. This is used to
20818 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020819 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020820 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020821copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 typval_T *from;
20823 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020825 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020826 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020827 switch (from->v_type)
20828 {
20829 case VAR_NUMBER:
20830 to->vval.v_number = from->vval.v_number;
20831 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020832#ifdef FEAT_FLOAT
20833 case VAR_FLOAT:
20834 to->vval.v_float = from->vval.v_float;
20835 break;
20836#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020837 case VAR_STRING:
20838 case VAR_FUNC:
20839 if (from->vval.v_string == NULL)
20840 to->vval.v_string = NULL;
20841 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020843 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020844 if (from->v_type == VAR_FUNC)
20845 func_ref(to->vval.v_string);
20846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020847 break;
20848 case VAR_LIST:
20849 if (from->vval.v_list == NULL)
20850 to->vval.v_list = NULL;
20851 else
20852 {
20853 to->vval.v_list = from->vval.v_list;
20854 ++to->vval.v_list->lv_refcount;
20855 }
20856 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020857 case VAR_DICT:
20858 if (from->vval.v_dict == NULL)
20859 to->vval.v_dict = NULL;
20860 else
20861 {
20862 to->vval.v_dict = from->vval.v_dict;
20863 ++to->vval.v_dict->dv_refcount;
20864 }
20865 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020866 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020867 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020868 break;
20869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020873 * Make a copy of an item.
20874 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020875 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20876 * reference to an already copied list/dict can be used.
20877 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020878 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020879 static int
20880item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020881 typval_T *from;
20882 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020883 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020884 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020885{
20886 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020887 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020888
Bram Moolenaar33570922005-01-25 22:26:29 +000020889 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020890 {
20891 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020892 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020893 }
20894 ++recurse;
20895
20896 switch (from->v_type)
20897 {
20898 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020899#ifdef FEAT_FLOAT
20900 case VAR_FLOAT:
20901#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020902 case VAR_STRING:
20903 case VAR_FUNC:
20904 copy_tv(from, to);
20905 break;
20906 case VAR_LIST:
20907 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020908 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020909 if (from->vval.v_list == NULL)
20910 to->vval.v_list = NULL;
20911 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20912 {
20913 /* use the copy made earlier */
20914 to->vval.v_list = from->vval.v_list->lv_copylist;
20915 ++to->vval.v_list->lv_refcount;
20916 }
20917 else
20918 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20919 if (to->vval.v_list == NULL)
20920 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020921 break;
20922 case VAR_DICT:
20923 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020924 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020925 if (from->vval.v_dict == NULL)
20926 to->vval.v_dict = NULL;
20927 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20928 {
20929 /* use the copy made earlier */
20930 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20931 ++to->vval.v_dict->dv_refcount;
20932 }
20933 else
20934 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20935 if (to->vval.v_dict == NULL)
20936 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020937 break;
20938 default:
20939 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020940 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020941 }
20942 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020943 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020944}
20945
20946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 * ":echo expr1 ..." print each argument separated with a space, add a
20948 * newline at the end.
20949 * ":echon expr1 ..." print each argument plain.
20950 */
20951 void
20952ex_echo(eap)
20953 exarg_T *eap;
20954{
20955 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020957 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 char_u *p;
20959 int needclr = TRUE;
20960 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020961 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962
20963 if (eap->skip)
20964 ++emsg_skip;
20965 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20966 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020967 /* If eval1() causes an error message the text from the command may
20968 * still need to be cleared. E.g., "echo 22,44". */
20969 need_clr_eos = needclr;
20970
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020972 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 {
20974 /*
20975 * Report the invalid expression unless the expression evaluation
20976 * has been cancelled due to an aborting error, an interrupt, or an
20977 * exception.
20978 */
20979 if (!aborting())
20980 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020981 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 break;
20983 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020984 need_clr_eos = FALSE;
20985
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 if (!eap->skip)
20987 {
20988 if (atstart)
20989 {
20990 atstart = FALSE;
20991 /* Call msg_start() after eval1(), evaluating the expression
20992 * may cause a message to appear. */
20993 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020994 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020995 /* Mark the saved text as finishing the line, so that what
20996 * follows is displayed on a new line when scrolling back
20997 * at the more prompt. */
20998 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 }
21002 else if (eap->cmdidx == CMD_echo)
21003 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021004 current_copyID += COPYID_INC;
21005 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021006 if (p != NULL)
21007 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021009 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021011 if (*p != TAB && needclr)
21012 {
21013 /* remove any text still there from the command */
21014 msg_clr_eos();
21015 needclr = FALSE;
21016 }
21017 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 }
21019 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021020 {
21021#ifdef FEAT_MBYTE
21022 if (has_mbyte)
21023 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021024 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021025
21026 (void)msg_outtrans_len_attr(p, i, echo_attr);
21027 p += i - 1;
21028 }
21029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021031 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021036 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 arg = skipwhite(arg);
21038 }
21039 eap->nextcmd = check_nextcmd(arg);
21040
21041 if (eap->skip)
21042 --emsg_skip;
21043 else
21044 {
21045 /* remove text that may still be there from the command */
21046 if (needclr)
21047 msg_clr_eos();
21048 if (eap->cmdidx == CMD_echo)
21049 msg_end();
21050 }
21051}
21052
21053/*
21054 * ":echohl {name}".
21055 */
21056 void
21057ex_echohl(eap)
21058 exarg_T *eap;
21059{
21060 int id;
21061
21062 id = syn_name2id(eap->arg);
21063 if (id == 0)
21064 echo_attr = 0;
21065 else
21066 echo_attr = syn_id2attr(id);
21067}
21068
21069/*
21070 * ":execute expr1 ..." execute the result of an expression.
21071 * ":echomsg expr1 ..." Print a message
21072 * ":echoerr expr1 ..." Print an error
21073 * Each gets spaces around each argument and a newline at the end for
21074 * echo commands
21075 */
21076 void
21077ex_execute(eap)
21078 exarg_T *eap;
21079{
21080 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 int ret = OK;
21083 char_u *p;
21084 garray_T ga;
21085 int len;
21086 int save_did_emsg;
21087
21088 ga_init2(&ga, 1, 80);
21089
21090 if (eap->skip)
21091 ++emsg_skip;
21092 while (*arg != NUL && *arg != '|' && *arg != '\n')
21093 {
21094 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021095 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 {
21097 /*
21098 * Report the invalid expression unless the expression evaluation
21099 * has been cancelled due to an aborting error, an interrupt, or an
21100 * exception.
21101 */
21102 if (!aborting())
21103 EMSG2(_(e_invexpr2), p);
21104 ret = FAIL;
21105 break;
21106 }
21107
21108 if (!eap->skip)
21109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021110 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 len = (int)STRLEN(p);
21112 if (ga_grow(&ga, len + 2) == FAIL)
21113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 ret = FAIL;
21116 break;
21117 }
21118 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 ga.ga_len += len;
21122 }
21123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021124 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 arg = skipwhite(arg);
21126 }
21127
21128 if (ret != FAIL && ga.ga_data != NULL)
21129 {
21130 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021133 out_flush();
21134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 else if (eap->cmdidx == CMD_echoerr)
21136 {
21137 /* We don't want to abort following commands, restore did_emsg. */
21138 save_did_emsg = did_emsg;
21139 EMSG((char_u *)ga.ga_data);
21140 if (!force_abort)
21141 did_emsg = save_did_emsg;
21142 }
21143 else if (eap->cmdidx == CMD_execute)
21144 do_cmdline((char_u *)ga.ga_data,
21145 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21146 }
21147
21148 ga_clear(&ga);
21149
21150 if (eap->skip)
21151 --emsg_skip;
21152
21153 eap->nextcmd = check_nextcmd(arg);
21154}
21155
21156/*
21157 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21158 * "arg" points to the "&" or '+' when called, to "option" when returning.
21159 * Returns NULL when no option name found. Otherwise pointer to the char
21160 * after the option name.
21161 */
21162 static char_u *
21163find_option_end(arg, opt_flags)
21164 char_u **arg;
21165 int *opt_flags;
21166{
21167 char_u *p = *arg;
21168
21169 ++p;
21170 if (*p == 'g' && p[1] == ':')
21171 {
21172 *opt_flags = OPT_GLOBAL;
21173 p += 2;
21174 }
21175 else if (*p == 'l' && p[1] == ':')
21176 {
21177 *opt_flags = OPT_LOCAL;
21178 p += 2;
21179 }
21180 else
21181 *opt_flags = 0;
21182
21183 if (!ASCII_ISALPHA(*p))
21184 return NULL;
21185 *arg = p;
21186
21187 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21188 p += 4; /* termcap option */
21189 else
21190 while (ASCII_ISALPHA(*p))
21191 ++p;
21192 return p;
21193}
21194
21195/*
21196 * ":function"
21197 */
21198 void
21199ex_function(eap)
21200 exarg_T *eap;
21201{
21202 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021203 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 int j;
21205 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021207 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 char_u *name = NULL;
21209 char_u *p;
21210 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021211 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 garray_T newargs;
21213 garray_T newlines;
21214 int varargs = FALSE;
21215 int mustend = FALSE;
21216 int flags = 0;
21217 ufunc_T *fp;
21218 int indent;
21219 int nesting;
21220 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021221 dictitem_T *v;
21222 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021223 static int func_nr = 0; /* number for nameless function */
21224 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021225 hashtab_T *ht;
21226 int todo;
21227 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021228 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229
21230 /*
21231 * ":function" without argument: list functions.
21232 */
21233 if (ends_excmd(*eap->arg))
21234 {
21235 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021237 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021238 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021239 {
21240 if (!HASHITEM_EMPTY(hi))
21241 {
21242 --todo;
21243 fp = HI2UF(hi);
21244 if (!isdigit(*fp->uf_name))
21245 list_func_head(fp, FALSE);
21246 }
21247 }
21248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 eap->nextcmd = check_nextcmd(eap->arg);
21250 return;
21251 }
21252
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021253 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021254 * ":function /pat": list functions matching pattern.
21255 */
21256 if (*eap->arg == '/')
21257 {
21258 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21259 if (!eap->skip)
21260 {
21261 regmatch_T regmatch;
21262
21263 c = *p;
21264 *p = NUL;
21265 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21266 *p = c;
21267 if (regmatch.regprog != NULL)
21268 {
21269 regmatch.rm_ic = p_ic;
21270
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021271 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021272 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21273 {
21274 if (!HASHITEM_EMPTY(hi))
21275 {
21276 --todo;
21277 fp = HI2UF(hi);
21278 if (!isdigit(*fp->uf_name)
21279 && vim_regexec(&regmatch, fp->uf_name, 0))
21280 list_func_head(fp, FALSE);
21281 }
21282 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021283 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021284 }
21285 }
21286 if (*p == '/')
21287 ++p;
21288 eap->nextcmd = check_nextcmd(p);
21289 return;
21290 }
21291
21292 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021293 * Get the function name. There are these situations:
21294 * func normal function name
21295 * "name" == func, "fudi.fd_dict" == NULL
21296 * dict.func new dictionary entry
21297 * "name" == NULL, "fudi.fd_dict" set,
21298 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21299 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021300 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021301 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21302 * dict.func existing dict entry that's not a Funcref
21303 * "name" == NULL, "fudi.fd_dict" set,
21304 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 name = trans_function_name(&p, eap->skip, 0, &fudi);
21308 paren = (vim_strchr(p, '(') != NULL);
21309 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 {
21311 /*
21312 * Return on an invalid expression in braces, unless the expression
21313 * evaluation has been cancelled due to an aborting error, an
21314 * interrupt, or an exception.
21315 */
21316 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021318 if (!eap->skip && fudi.fd_newkey != NULL)
21319 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021320 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 else
21324 eap->skip = TRUE;
21325 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021326
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 /* An error in a function call during evaluation of an expression in magic
21328 * braces should not cause the function not to be defined. */
21329 saved_did_emsg = did_emsg;
21330 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331
21332 /*
21333 * ":function func" with only function name: list function.
21334 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021335 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 {
21337 if (!ends_excmd(*skipwhite(p)))
21338 {
21339 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021340 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 }
21342 eap->nextcmd = check_nextcmd(p);
21343 if (eap->nextcmd != NULL)
21344 *p = NUL;
21345 if (!eap->skip && !got_int)
21346 {
21347 fp = find_func(name);
21348 if (fp != NULL)
21349 {
21350 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021353 if (FUNCLINE(fp, j) == NULL)
21354 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 msg_putchar('\n');
21356 msg_outnum((long)(j + 1));
21357 if (j < 9)
21358 msg_putchar(' ');
21359 if (j < 99)
21360 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021361 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 out_flush(); /* show a line at a time */
21363 ui_breakcheck();
21364 }
21365 if (!got_int)
21366 {
21367 msg_putchar('\n');
21368 msg_puts((char_u *)" endfunction");
21369 }
21370 }
21371 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021372 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021374 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 }
21376
21377 /*
21378 * ":function name(arg1, arg2)" Define function.
21379 */
21380 p = skipwhite(p);
21381 if (*p != '(')
21382 {
21383 if (!eap->skip)
21384 {
21385 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021386 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 }
21388 /* attempt to continue by skipping some text */
21389 if (vim_strchr(p, '(') != NULL)
21390 p = vim_strchr(p, '(');
21391 }
21392 p = skipwhite(p + 1);
21393
21394 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21395 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21396
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021397 if (!eap->skip)
21398 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021399 /* Check the name of the function. Unless it's a dictionary function
21400 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021401 if (name != NULL)
21402 arg = name;
21403 else
21404 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021405 if (arg != NULL && (fudi.fd_di == NULL
21406 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021407 {
21408 if (*arg == K_SPECIAL)
21409 j = 3;
21410 else
21411 j = 0;
21412 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21413 : eval_isnamec(arg[j])))
21414 ++j;
21415 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021416 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021417 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021418 /* Disallow using the g: dict. */
21419 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21420 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021421 }
21422
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 /*
21424 * Isolate the arguments: "arg1, arg2, ...)"
21425 */
21426 while (*p != ')')
21427 {
21428 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21429 {
21430 varargs = TRUE;
21431 p += 3;
21432 mustend = TRUE;
21433 }
21434 else
21435 {
21436 arg = p;
21437 while (ASCII_ISALNUM(*p) || *p == '_')
21438 ++p;
21439 if (arg == p || isdigit(*arg)
21440 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21441 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21442 {
21443 if (!eap->skip)
21444 EMSG2(_("E125: Illegal argument: %s"), arg);
21445 break;
21446 }
21447 if (ga_grow(&newargs, 1) == FAIL)
21448 goto erret;
21449 c = *p;
21450 *p = NUL;
21451 arg = vim_strsave(arg);
21452 if (arg == NULL)
21453 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021454
21455 /* Check for duplicate argument name. */
21456 for (i = 0; i < newargs.ga_len; ++i)
21457 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21458 {
21459 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21460 goto erret;
21461 }
21462
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21464 *p = c;
21465 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 if (*p == ',')
21467 ++p;
21468 else
21469 mustend = TRUE;
21470 }
21471 p = skipwhite(p);
21472 if (mustend && *p != ')')
21473 {
21474 if (!eap->skip)
21475 EMSG2(_(e_invarg2), eap->arg);
21476 break;
21477 }
21478 }
21479 ++p; /* skip the ')' */
21480
Bram Moolenaare9a41262005-01-15 22:18:47 +000021481 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 for (;;)
21483 {
21484 p = skipwhite(p);
21485 if (STRNCMP(p, "range", 5) == 0)
21486 {
21487 flags |= FC_RANGE;
21488 p += 5;
21489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021490 else if (STRNCMP(p, "dict", 4) == 0)
21491 {
21492 flags |= FC_DICT;
21493 p += 4;
21494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 else if (STRNCMP(p, "abort", 5) == 0)
21496 {
21497 flags |= FC_ABORT;
21498 p += 5;
21499 }
21500 else
21501 break;
21502 }
21503
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021504 /* When there is a line break use what follows for the function body.
21505 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21506 if (*p == '\n')
21507 line_arg = p + 1;
21508 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 EMSG(_(e_trailing));
21510
21511 /*
21512 * Read the body of the function, until ":endfunction" is found.
21513 */
21514 if (KeyTyped)
21515 {
21516 /* Check if the function already exists, don't let the user type the
21517 * whole function before telling him it doesn't work! For a script we
21518 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519 if (!eap->skip && !eap->forceit)
21520 {
21521 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21522 EMSG(_(e_funcdict));
21523 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021524 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021527 if (!eap->skip && did_emsg)
21528 goto erret;
21529
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 msg_putchar('\n'); /* don't overwrite the function name */
21531 cmdline_row = msg_row;
21532 }
21533
21534 indent = 2;
21535 nesting = 0;
21536 for (;;)
21537 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021538 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021539 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021540 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021541 saved_wait_return = FALSE;
21542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021544 sourcing_lnum_off = sourcing_lnum;
21545
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021546 if (line_arg != NULL)
21547 {
21548 /* Use eap->arg, split up in parts by line breaks. */
21549 theline = line_arg;
21550 p = vim_strchr(theline, '\n');
21551 if (p == NULL)
21552 line_arg += STRLEN(line_arg);
21553 else
21554 {
21555 *p = NUL;
21556 line_arg = p + 1;
21557 }
21558 }
21559 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 theline = getcmdline(':', 0L, indent);
21561 else
21562 theline = eap->getline(':', eap->cookie, indent);
21563 if (KeyTyped)
21564 lines_left = Rows - 1;
21565 if (theline == NULL)
21566 {
21567 EMSG(_("E126: Missing :endfunction"));
21568 goto erret;
21569 }
21570
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021571 /* Detect line continuation: sourcing_lnum increased more than one. */
21572 if (sourcing_lnum > sourcing_lnum_off + 1)
21573 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21574 else
21575 sourcing_lnum_off = 0;
21576
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 if (skip_until != NULL)
21578 {
21579 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21580 * don't check for ":endfunc". */
21581 if (STRCMP(theline, skip_until) == 0)
21582 {
21583 vim_free(skip_until);
21584 skip_until = NULL;
21585 }
21586 }
21587 else
21588 {
21589 /* skip ':' and blanks*/
21590 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21591 ;
21592
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021593 /* Check for "endfunction". */
21594 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021596 if (line_arg == NULL)
21597 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 break;
21599 }
21600
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021601 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 * at "end". */
21603 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21604 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021605 else if (STRNCMP(p, "if", 2) == 0
21606 || STRNCMP(p, "wh", 2) == 0
21607 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 || STRNCMP(p, "try", 3) == 0)
21609 indent += 2;
21610
21611 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021612 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021614 if (*p == '!')
21615 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 p += eval_fname_script(p);
21617 if (ASCII_ISALPHA(*p))
21618 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 if (*skipwhite(p) == '(')
21621 {
21622 ++nesting;
21623 indent += 2;
21624 }
21625 }
21626 }
21627
21628 /* Check for ":append" or ":insert". */
21629 p = skip_range(p, NULL);
21630 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21631 || (p[0] == 'i'
21632 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21633 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21634 skip_until = vim_strsave((char_u *)".");
21635
21636 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21637 arg = skipwhite(skiptowhite(p));
21638 if (arg[0] == '<' && arg[1] =='<'
21639 && ((p[0] == 'p' && p[1] == 'y'
21640 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21641 || (p[0] == 'p' && p[1] == 'e'
21642 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21643 || (p[0] == 't' && p[1] == 'c'
21644 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021645 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21646 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21648 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021649 || (p[0] == 'm' && p[1] == 'z'
21650 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 ))
21652 {
21653 /* ":python <<" continues until a dot, like ":append" */
21654 p = skipwhite(arg + 2);
21655 if (*p == NUL)
21656 skip_until = vim_strsave((char_u *)".");
21657 else
21658 skip_until = vim_strsave(p);
21659 }
21660 }
21661
21662 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021663 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021664 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021665 if (line_arg == NULL)
21666 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021668 }
21669
21670 /* Copy the line to newly allocated memory. get_one_sourceline()
21671 * allocates 250 bytes per line, this saves 80% on average. The cost
21672 * is an extra alloc/free. */
21673 p = vim_strsave(theline);
21674 if (p != NULL)
21675 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021676 if (line_arg == NULL)
21677 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021678 theline = p;
21679 }
21680
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021681 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21682
21683 /* Add NULL lines for continuation lines, so that the line count is
21684 * equal to the index in the growarray. */
21685 while (sourcing_lnum_off-- > 0)
21686 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021687
21688 /* Check for end of eap->arg. */
21689 if (line_arg != NULL && *line_arg == NUL)
21690 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692
21693 /* Don't define the function when skipping commands or when an error was
21694 * detected. */
21695 if (eap->skip || did_emsg)
21696 goto erret;
21697
21698 /*
21699 * If there are no errors, add the function
21700 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021702 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021703 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021706 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021707 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 goto erret;
21709 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021710
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 fp = find_func(name);
21712 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714 if (!eap->forceit)
21715 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021716 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021717 goto erret;
21718 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021719 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021721 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 name);
21723 goto erret;
21724 }
21725 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726 ga_clear_strings(&(fp->uf_args));
21727 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021728 vim_free(name);
21729 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 }
21732 else
21733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021734 char numbuf[20];
21735
21736 fp = NULL;
21737 if (fudi.fd_newkey == NULL && !eap->forceit)
21738 {
21739 EMSG(_(e_funcdict));
21740 goto erret;
21741 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021742 if (fudi.fd_di == NULL)
21743 {
21744 /* Can't add a function to a locked dictionary */
21745 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21746 goto erret;
21747 }
21748 /* Can't change an existing function if it is locked */
21749 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21750 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751
21752 /* Give the function a sequential number. Can only be used with a
21753 * Funcref! */
21754 vim_free(name);
21755 sprintf(numbuf, "%d", ++func_nr);
21756 name = vim_strsave((char_u *)numbuf);
21757 if (name == NULL)
21758 goto erret;
21759 }
21760
21761 if (fp == NULL)
21762 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021763 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021764 {
21765 int slen, plen;
21766 char_u *scriptname;
21767
21768 /* Check that the autoload name matches the script name. */
21769 j = FAIL;
21770 if (sourcing_name != NULL)
21771 {
21772 scriptname = autoload_name(name);
21773 if (scriptname != NULL)
21774 {
21775 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021776 plen = (int)STRLEN(p);
21777 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021778 if (slen > plen && fnamecmp(p,
21779 sourcing_name + slen - plen) == 0)
21780 j = OK;
21781 vim_free(scriptname);
21782 }
21783 }
21784 if (j == FAIL)
21785 {
21786 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21787 goto erret;
21788 }
21789 }
21790
21791 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 if (fp == NULL)
21793 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021794
21795 if (fudi.fd_dict != NULL)
21796 {
21797 if (fudi.fd_di == NULL)
21798 {
21799 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021800 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021801 if (fudi.fd_di == NULL)
21802 {
21803 vim_free(fp);
21804 goto erret;
21805 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021806 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21807 {
21808 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021809 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021810 goto erret;
21811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021812 }
21813 else
21814 /* overwrite existing dict entry */
21815 clear_tv(&fudi.fd_di->di_tv);
21816 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021817 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021818 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021819 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021820
21821 /* behave like "dict" was used */
21822 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021823 }
21824
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 STRCPY(fp->uf_name, name);
21827 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021829 fp->uf_args = newargs;
21830 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021831#ifdef FEAT_PROFILE
21832 fp->uf_tml_count = NULL;
21833 fp->uf_tml_total = NULL;
21834 fp->uf_tml_self = NULL;
21835 fp->uf_profiling = FALSE;
21836 if (prof_def_func())
21837 func_do_profile(fp);
21838#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021839 fp->uf_varargs = varargs;
21840 fp->uf_flags = flags;
21841 fp->uf_calls = 0;
21842 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021843 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844
21845erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 ga_clear_strings(&newargs);
21847 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848ret_free:
21849 vim_free(skip_until);
21850 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021853 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854}
21855
21856/*
21857 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021858 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021860 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021861 * TFN_INT: internal function name OK
21862 * TFN_QUIET: be quiet
21863 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 * Advances "pp" to just after the function name (if no error).
21865 */
21866 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021867trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 char_u **pp;
21869 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021870 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021871 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021873 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 char_u *start;
21875 char_u *end;
21876 int lead;
21877 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021880
21881 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021884
21885 /* Check for hard coded <SNR>: already translated function ID (from a user
21886 * command). */
21887 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21888 && (*pp)[2] == (int)KE_SNR)
21889 {
21890 *pp += 3;
21891 len = get_id_len(pp) + 3;
21892 return vim_strnsave(start, len);
21893 }
21894
21895 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21896 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021898 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021900
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021901 /* Note that TFN_ flags use the same values as GLV_ flags. */
21902 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021903 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021904 if (end == start)
21905 {
21906 if (!skip)
21907 EMSG(_("E129: Function name required"));
21908 goto theend;
21909 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021910 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021911 {
21912 /*
21913 * Report an invalid expression in braces, unless the expression
21914 * evaluation has been cancelled due to an aborting error, an
21915 * interrupt, or an exception.
21916 */
21917 if (!aborting())
21918 {
21919 if (end != NULL)
21920 EMSG2(_(e_invarg2), start);
21921 }
21922 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021923 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021924 goto theend;
21925 }
21926
21927 if (lv.ll_tv != NULL)
21928 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021929 if (fdp != NULL)
21930 {
21931 fdp->fd_dict = lv.ll_dict;
21932 fdp->fd_newkey = lv.ll_newkey;
21933 lv.ll_newkey = NULL;
21934 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021935 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021936 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21937 {
21938 name = vim_strsave(lv.ll_tv->vval.v_string);
21939 *pp = end;
21940 }
21941 else
21942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021943 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21944 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021945 EMSG(_(e_funcref));
21946 else
21947 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021948 name = NULL;
21949 }
21950 goto theend;
21951 }
21952
21953 if (lv.ll_name == NULL)
21954 {
21955 /* Error found, but continue after the function name. */
21956 *pp = end;
21957 goto theend;
21958 }
21959
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021960 /* Check if the name is a Funcref. If so, use the value. */
21961 if (lv.ll_exp_name != NULL)
21962 {
21963 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021964 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021965 if (name == lv.ll_exp_name)
21966 name = NULL;
21967 }
21968 else
21969 {
21970 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021971 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021972 if (name == *pp)
21973 name = NULL;
21974 }
21975 if (name != NULL)
21976 {
21977 name = vim_strsave(name);
21978 *pp = end;
21979 goto theend;
21980 }
21981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021982 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021983 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021984 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021985 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21986 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21987 {
21988 /* When there was "s:" already or the name expanded to get a
21989 * leading "s:" then remove it. */
21990 lv.ll_name += 2;
21991 len -= 2;
21992 lead = 2;
21993 }
21994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021995 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021996 {
21997 if (lead == 2) /* skip over "s:" */
21998 lv.ll_name += 2;
21999 len = (int)(end - lv.ll_name);
22000 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022001
22002 /*
22003 * Copy the function name to allocated memory.
22004 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22005 * Accept <SNR>123_name() outside a script.
22006 */
22007 if (skip)
22008 lead = 0; /* do nothing */
22009 else if (lead > 0)
22010 {
22011 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022012 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22013 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022014 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022015 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022016 if (current_SID <= 0)
22017 {
22018 EMSG(_(e_usingsid));
22019 goto theend;
22020 }
22021 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22022 lead += (int)STRLEN(sid_buf);
22023 }
22024 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022025 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022026 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022027 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022028 goto theend;
22029 }
22030 name = alloc((unsigned)(len + lead + 1));
22031 if (name != NULL)
22032 {
22033 if (lead > 0)
22034 {
22035 name[0] = K_SPECIAL;
22036 name[1] = KS_EXTRA;
22037 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022038 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022039 STRCPY(name + 3, sid_buf);
22040 }
22041 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22042 name[len + lead] = NUL;
22043 }
22044 *pp = end;
22045
22046theend:
22047 clear_lval(&lv);
22048 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049}
22050
22051/*
22052 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22053 * Return 2 if "p" starts with "s:".
22054 * Return 0 otherwise.
22055 */
22056 static int
22057eval_fname_script(p)
22058 char_u *p;
22059{
22060 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22061 || STRNICMP(p + 1, "SNR>", 4) == 0))
22062 return 5;
22063 if (p[0] == 's' && p[1] == ':')
22064 return 2;
22065 return 0;
22066}
22067
22068/*
22069 * Return TRUE if "p" starts with "<SID>" or "s:".
22070 * Only works if eval_fname_script() returned non-zero for "p"!
22071 */
22072 static int
22073eval_fname_sid(p)
22074 char_u *p;
22075{
22076 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22077}
22078
22079/*
22080 * List the head of the function: "name(arg1, arg2)".
22081 */
22082 static void
22083list_func_head(fp, indent)
22084 ufunc_T *fp;
22085 int indent;
22086{
22087 int j;
22088
22089 msg_start();
22090 if (indent)
22091 MSG_PUTS(" ");
22092 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022093 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 {
22095 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022096 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 }
22098 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022099 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022101 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 {
22103 if (j)
22104 MSG_PUTS(", ");
22105 msg_puts(FUNCARG(fp, j));
22106 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022107 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 {
22109 if (j)
22110 MSG_PUTS(", ");
22111 MSG_PUTS("...");
22112 }
22113 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022114 if (fp->uf_flags & FC_ABORT)
22115 MSG_PUTS(" abort");
22116 if (fp->uf_flags & FC_RANGE)
22117 MSG_PUTS(" range");
22118 if (fp->uf_flags & FC_DICT)
22119 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022120 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022121 if (p_verbose > 0)
22122 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123}
22124
22125/*
22126 * Find a function by name, return pointer to it in ufuncs.
22127 * Return NULL for unknown function.
22128 */
22129 static ufunc_T *
22130find_func(name)
22131 char_u *name;
22132{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022133 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022135 hi = hash_find(&func_hashtab, name);
22136 if (!HASHITEM_EMPTY(hi))
22137 return HI2UF(hi);
22138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139}
22140
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022141#if defined(EXITFREE) || defined(PROTO)
22142 void
22143free_all_functions()
22144{
22145 hashitem_T *hi;
22146
22147 /* Need to start all over every time, because func_free() may change the
22148 * hash table. */
22149 while (func_hashtab.ht_used > 0)
22150 for (hi = func_hashtab.ht_array; ; ++hi)
22151 if (!HASHITEM_EMPTY(hi))
22152 {
22153 func_free(HI2UF(hi));
22154 break;
22155 }
22156}
22157#endif
22158
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022159 int
22160translated_function_exists(name)
22161 char_u *name;
22162{
22163 if (builtin_function(name))
22164 return find_internal_func(name) >= 0;
22165 return find_func(name) != NULL;
22166}
22167
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022168/*
22169 * Return TRUE if a function "name" exists.
22170 */
22171 static int
22172function_exists(name)
22173 char_u *name;
22174{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022175 char_u *nm = name;
22176 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022177 int n = FALSE;
22178
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022179 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22180 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022181 nm = skipwhite(nm);
22182
22183 /* Only accept "funcname", "funcname ", "funcname (..." and
22184 * "funcname(...", not "funcname!...". */
22185 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022186 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022187 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 return n;
22189}
22190
Bram Moolenaara1544c02013-05-30 12:35:52 +020022191 char_u *
22192get_expanded_name(name, check)
22193 char_u *name;
22194 int check;
22195{
22196 char_u *nm = name;
22197 char_u *p;
22198
22199 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22200
22201 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022202 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022203 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022204
Bram Moolenaara1544c02013-05-30 12:35:52 +020022205 vim_free(p);
22206 return NULL;
22207}
22208
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022209/*
22210 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022211 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022212 */
22213 static int
22214builtin_function(name)
22215 char_u *name;
22216{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022217 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22218 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022219}
22220
Bram Moolenaar05159a02005-02-26 23:04:13 +000022221#if defined(FEAT_PROFILE) || defined(PROTO)
22222/*
22223 * Start profiling function "fp".
22224 */
22225 static void
22226func_do_profile(fp)
22227 ufunc_T *fp;
22228{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022229 int len = fp->uf_lines.ga_len;
22230
22231 if (len == 0)
22232 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022233 fp->uf_tm_count = 0;
22234 profile_zero(&fp->uf_tm_self);
22235 profile_zero(&fp->uf_tm_total);
22236 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022237 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022238 if (fp->uf_tml_total == NULL)
22239 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022240 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022241 if (fp->uf_tml_self == NULL)
22242 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022243 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022244 fp->uf_tml_idx = -1;
22245 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22246 || fp->uf_tml_self == NULL)
22247 return; /* out of memory */
22248
22249 fp->uf_profiling = TRUE;
22250}
22251
22252/*
22253 * Dump the profiling results for all functions in file "fd".
22254 */
22255 void
22256func_dump_profile(fd)
22257 FILE *fd;
22258{
22259 hashitem_T *hi;
22260 int todo;
22261 ufunc_T *fp;
22262 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022263 ufunc_T **sorttab;
22264 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022265
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022266 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022267 if (todo == 0)
22268 return; /* nothing to dump */
22269
Bram Moolenaar73830342005-02-28 22:48:19 +000022270 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22271
Bram Moolenaar05159a02005-02-26 23:04:13 +000022272 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22273 {
22274 if (!HASHITEM_EMPTY(hi))
22275 {
22276 --todo;
22277 fp = HI2UF(hi);
22278 if (fp->uf_profiling)
22279 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022280 if (sorttab != NULL)
22281 sorttab[st_len++] = fp;
22282
Bram Moolenaar05159a02005-02-26 23:04:13 +000022283 if (fp->uf_name[0] == K_SPECIAL)
22284 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22285 else
22286 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22287 if (fp->uf_tm_count == 1)
22288 fprintf(fd, "Called 1 time\n");
22289 else
22290 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22291 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22292 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22293 fprintf(fd, "\n");
22294 fprintf(fd, "count total (s) self (s)\n");
22295
22296 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22297 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022298 if (FUNCLINE(fp, i) == NULL)
22299 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022300 prof_func_line(fd, fp->uf_tml_count[i],
22301 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022302 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22303 }
22304 fprintf(fd, "\n");
22305 }
22306 }
22307 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022308
22309 if (sorttab != NULL && st_len > 0)
22310 {
22311 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22312 prof_total_cmp);
22313 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22314 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22315 prof_self_cmp);
22316 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22317 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022318
22319 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022320}
Bram Moolenaar73830342005-02-28 22:48:19 +000022321
22322 static void
22323prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22324 FILE *fd;
22325 ufunc_T **sorttab;
22326 int st_len;
22327 char *title;
22328 int prefer_self; /* when equal print only self time */
22329{
22330 int i;
22331 ufunc_T *fp;
22332
22333 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22334 fprintf(fd, "count total (s) self (s) function\n");
22335 for (i = 0; i < 20 && i < st_len; ++i)
22336 {
22337 fp = sorttab[i];
22338 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22339 prefer_self);
22340 if (fp->uf_name[0] == K_SPECIAL)
22341 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22342 else
22343 fprintf(fd, " %s()\n", fp->uf_name);
22344 }
22345 fprintf(fd, "\n");
22346}
22347
22348/*
22349 * Print the count and times for one function or function line.
22350 */
22351 static void
22352prof_func_line(fd, count, total, self, prefer_self)
22353 FILE *fd;
22354 int count;
22355 proftime_T *total;
22356 proftime_T *self;
22357 int prefer_self; /* when equal print only self time */
22358{
22359 if (count > 0)
22360 {
22361 fprintf(fd, "%5d ", count);
22362 if (prefer_self && profile_equal(total, self))
22363 fprintf(fd, " ");
22364 else
22365 fprintf(fd, "%s ", profile_msg(total));
22366 if (!prefer_self && profile_equal(total, self))
22367 fprintf(fd, " ");
22368 else
22369 fprintf(fd, "%s ", profile_msg(self));
22370 }
22371 else
22372 fprintf(fd, " ");
22373}
22374
22375/*
22376 * Compare function for total time sorting.
22377 */
22378 static int
22379#ifdef __BORLANDC__
22380_RTLENTRYF
22381#endif
22382prof_total_cmp(s1, s2)
22383 const void *s1;
22384 const void *s2;
22385{
22386 ufunc_T *p1, *p2;
22387
22388 p1 = *(ufunc_T **)s1;
22389 p2 = *(ufunc_T **)s2;
22390 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22391}
22392
22393/*
22394 * Compare function for self time sorting.
22395 */
22396 static int
22397#ifdef __BORLANDC__
22398_RTLENTRYF
22399#endif
22400prof_self_cmp(s1, s2)
22401 const void *s1;
22402 const void *s2;
22403{
22404 ufunc_T *p1, *p2;
22405
22406 p1 = *(ufunc_T **)s1;
22407 p2 = *(ufunc_T **)s2;
22408 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22409}
22410
Bram Moolenaar05159a02005-02-26 23:04:13 +000022411#endif
22412
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022413/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022415 * Return TRUE if a package was loaded.
22416 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022417 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022418script_autoload(name, reload)
22419 char_u *name;
22420 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022421{
22422 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022423 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022424 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022425 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022426
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022427 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022428 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022429 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022430 return FALSE;
22431
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022432 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022433
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022434 /* Find the name in the list of previously loaded package names. Skip
22435 * "autoload/", it's always the same. */
22436 for (i = 0; i < ga_loaded.ga_len; ++i)
22437 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22438 break;
22439 if (!reload && i < ga_loaded.ga_len)
22440 ret = FALSE; /* was loaded already */
22441 else
22442 {
22443 /* Remember the name if it wasn't loaded already. */
22444 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22445 {
22446 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22447 tofree = NULL;
22448 }
22449
22450 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022451 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022452 ret = TRUE;
22453 }
22454
22455 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022456 return ret;
22457}
22458
22459/*
22460 * Return the autoload script name for a function or variable name.
22461 * Returns NULL when out of memory.
22462 */
22463 static char_u *
22464autoload_name(name)
22465 char_u *name;
22466{
22467 char_u *p;
22468 char_u *scriptname;
22469
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022470 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022471 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22472 if (scriptname == NULL)
22473 return FALSE;
22474 STRCPY(scriptname, "autoload/");
22475 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022476 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022477 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022478 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022479 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022480 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022481}
22482
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22484
22485/*
22486 * Function given to ExpandGeneric() to obtain the list of user defined
22487 * function names.
22488 */
22489 char_u *
22490get_user_func_name(xp, idx)
22491 expand_T *xp;
22492 int idx;
22493{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494 static long_u done;
22495 static hashitem_T *hi;
22496 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497
22498 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022500 done = 0;
22501 hi = func_hashtab.ht_array;
22502 }
22503 if (done < func_hashtab.ht_used)
22504 {
22505 if (done++ > 0)
22506 ++hi;
22507 while (HASHITEM_EMPTY(hi))
22508 ++hi;
22509 fp = HI2UF(hi);
22510
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022511 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022512 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022513
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22515 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516
22517 cat_func_name(IObuff, fp);
22518 if (xp->xp_context != EXPAND_USER_FUNC)
22519 {
22520 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022521 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 STRCAT(IObuff, ")");
22523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 return IObuff;
22525 }
22526 return NULL;
22527}
22528
22529#endif /* FEAT_CMDL_COMPL */
22530
22531/*
22532 * Copy the function name of "fp" to buffer "buf".
22533 * "buf" must be able to hold the function name plus three bytes.
22534 * Takes care of script-local function names.
22535 */
22536 static void
22537cat_func_name(buf, fp)
22538 char_u *buf;
22539 ufunc_T *fp;
22540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022541 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 {
22543 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 }
22546 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022547 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548}
22549
22550/*
22551 * ":delfunction {name}"
22552 */
22553 void
22554ex_delfunction(eap)
22555 exarg_T *eap;
22556{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022557 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 char_u *p;
22559 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561
22562 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022563 name = trans_function_name(&p, eap->skip, 0, &fudi);
22564 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022566 {
22567 if (fudi.fd_dict != NULL && !eap->skip)
22568 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 if (!ends_excmd(*skipwhite(p)))
22572 {
22573 vim_free(name);
22574 EMSG(_(e_trailing));
22575 return;
22576 }
22577 eap->nextcmd = check_nextcmd(p);
22578 if (eap->nextcmd != NULL)
22579 *p = NUL;
22580
22581 if (!eap->skip)
22582 fp = find_func(name);
22583 vim_free(name);
22584
22585 if (!eap->skip)
22586 {
22587 if (fp == NULL)
22588 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022589 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 return;
22591 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022592 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 {
22594 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22595 return;
22596 }
22597
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022598 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022600 /* Delete the dict item that refers to the function, it will
22601 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022602 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604 else
22605 func_free(fp);
22606 }
22607}
22608
22609/*
22610 * Free a function and remove it from the list of functions.
22611 */
22612 static void
22613func_free(fp)
22614 ufunc_T *fp;
22615{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022616 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617
22618 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022619 ga_clear_strings(&(fp->uf_args));
22620 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022621#ifdef FEAT_PROFILE
22622 vim_free(fp->uf_tml_count);
22623 vim_free(fp->uf_tml_total);
22624 vim_free(fp->uf_tml_self);
22625#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022626
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022627 /* remove the function from the function hashtable */
22628 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22629 if (HASHITEM_EMPTY(hi))
22630 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022631 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022632 hash_remove(&func_hashtab, hi);
22633
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022634 vim_free(fp);
22635}
22636
22637/*
22638 * Unreference a Function: decrement the reference count and free it when it
22639 * becomes zero. Only for numbered functions.
22640 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022641 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022642func_unref(name)
22643 char_u *name;
22644{
22645 ufunc_T *fp;
22646
22647 if (name != NULL && isdigit(*name))
22648 {
22649 fp = find_func(name);
22650 if (fp == NULL)
22651 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022652 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022653 {
22654 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022655 * when "uf_calls" becomes zero. */
22656 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022657 func_free(fp);
22658 }
22659 }
22660}
22661
22662/*
22663 * Count a reference to a Function.
22664 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022665 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022666func_ref(name)
22667 char_u *name;
22668{
22669 ufunc_T *fp;
22670
22671 if (name != NULL && isdigit(*name))
22672 {
22673 fp = find_func(name);
22674 if (fp == NULL)
22675 EMSG2(_(e_intern2), "func_ref()");
22676 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022677 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 }
22679}
22680
22681/*
22682 * Call a user function.
22683 */
22684 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022685call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 ufunc_T *fp; /* pointer to function */
22687 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 typval_T *argvars; /* arguments */
22689 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 linenr_T firstline; /* first line of range */
22691 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693{
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 char_u *save_sourcing_name;
22695 linenr_T save_sourcing_lnum;
22696 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022697 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 int save_did_emsg;
22699 static int depth = 0;
22700 dictitem_T *v;
22701 int fixvar_idx = 0; /* index in fixvar[] */
22702 int i;
22703 int ai;
22704 char_u numbuf[NUMBUFLEN];
22705 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022706#ifdef FEAT_PROFILE
22707 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022708 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710
22711 /* If depth of calling is getting too high, don't execute the function */
22712 if (depth >= p_mfd)
22713 {
22714 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022715 rettv->v_type = VAR_NUMBER;
22716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 return;
22718 }
22719 ++depth;
22720
22721 line_breakcheck(); /* check for CTRL-C hit */
22722
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022723 fc = (funccall_T *)alloc(sizeof(funccall_T));
22724 fc->caller = current_funccal;
22725 current_funccal = fc;
22726 fc->func = fp;
22727 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022728 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022729 fc->linenr = 0;
22730 fc->returned = FALSE;
22731 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022733 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22734 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735
Bram Moolenaar33570922005-01-25 22:26:29 +000022736 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022737 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22739 * each argument variable and saves a lot of time.
22740 */
22741 /*
22742 * Init l: variables.
22743 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022744 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022745 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022746 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022747 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22748 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022749 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022750 name = v->di_key;
22751 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022753 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022755 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 v->di_tv.vval.v_dict = selfdict;
22757 ++selfdict->dv_refcount;
22758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022759
Bram Moolenaar33570922005-01-25 22:26:29 +000022760 /*
22761 * Init a: variables.
22762 * Set a:0 to "argcount".
22763 * Set a:000 to a list with room for the "..." arguments.
22764 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022765 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022766 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022767 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022768 /* Use "name" to avoid a warning from some compiler that checks the
22769 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022770 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022771 name = v->di_key;
22772 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022774 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022776 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022777 v->di_tv.vval.v_list = &fc->l_varlist;
22778 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22779 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22780 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022781
22782 /*
22783 * Set a:firstline to "firstline" and a:lastline to "lastline".
22784 * Set a:name to named arguments.
22785 * Set a:N to the "..." arguments.
22786 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022787 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022789 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 (varnumber_T)lastline);
22791 for (i = 0; i < argcount; ++i)
22792 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022793 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 if (ai < 0)
22795 /* named argument a:name */
22796 name = FUNCARG(fp, i);
22797 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022798 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 /* "..." argument a:1, a:2, etc. */
22800 sprintf((char *)numbuf, "%d", ai + 1);
22801 name = numbuf;
22802 }
22803 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22804 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022805 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22807 }
22808 else
22809 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022810 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22811 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 if (v == NULL)
22813 break;
22814 v->di_flags = DI_FLAGS_RO;
22815 }
22816 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022817 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022818
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022819 /* Note: the values are copied directly to avoid alloc/free.
22820 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022821 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022822 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022823
22824 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22825 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022826 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22827 fc->l_listitems[ai].li_tv = argvars[i];
22828 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022829 }
22830 }
22831
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 /* Don't redraw while executing the function. */
22833 ++RedrawingDisabled;
22834 save_sourcing_name = sourcing_name;
22835 save_sourcing_lnum = sourcing_lnum;
22836 sourcing_lnum = 1;
22837 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022838 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 if (sourcing_name != NULL)
22840 {
22841 if (save_sourcing_name != NULL
22842 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22843 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22844 else
22845 STRCPY(sourcing_name, "function ");
22846 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22847
22848 if (p_verbose >= 12)
22849 {
22850 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022851 verbose_enter_scroll();
22852
Bram Moolenaar555b2802005-05-19 21:08:39 +000022853 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 if (p_verbose >= 14)
22855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022857 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022858 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022859 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860
22861 msg_puts((char_u *)"(");
22862 for (i = 0; i < argcount; ++i)
22863 {
22864 if (i > 0)
22865 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022866 if (argvars[i].v_type == VAR_NUMBER)
22867 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 else
22869 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022870 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22871 if (s != NULL)
22872 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022873 if (vim_strsize(s) > MSG_BUF_CLEN)
22874 {
22875 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22876 s = buf;
22877 }
22878 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022879 vim_free(tofree);
22880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 }
22882 }
22883 msg_puts((char_u *)")");
22884 }
22885 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022886
22887 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 --no_wait_return;
22889 }
22890 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022891#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022892 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022893 {
22894 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22895 func_do_profile(fp);
22896 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022897 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022898 {
22899 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022900 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022901 profile_zero(&fp->uf_tm_children);
22902 }
22903 script_prof_save(&wait_start);
22904 }
22905#endif
22906
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022908 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 save_did_emsg = did_emsg;
22910 did_emsg = FALSE;
22911
22912 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022913 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22915
22916 --RedrawingDisabled;
22917
22918 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022919 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022921 clear_tv(rettv);
22922 rettv->v_type = VAR_NUMBER;
22923 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 }
22925
Bram Moolenaar05159a02005-02-26 23:04:13 +000022926#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022927 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022928 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022929 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022930 profile_end(&call_start);
22931 profile_sub_wait(&wait_start, &call_start);
22932 profile_add(&fp->uf_tm_total, &call_start);
22933 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022934 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022935 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022936 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22937 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022938 }
22939 }
22940#endif
22941
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 /* when being verbose, mention the return value */
22943 if (p_verbose >= 12)
22944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022946 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022949 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022950 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022951 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022952 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022953 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022955 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022956 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022957 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022958 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022959
Bram Moolenaar555b2802005-05-19 21:08:39 +000022960 /* The value may be very long. Skip the middle part, so that we
22961 * have some idea how it starts and ends. smsg() would always
22962 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022963 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022964 if (s != NULL)
22965 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022966 if (vim_strsize(s) > MSG_BUF_CLEN)
22967 {
22968 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22969 s = buf;
22970 }
22971 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022972 vim_free(tofree);
22973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 }
22975 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022976
22977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 --no_wait_return;
22979 }
22980
22981 vim_free(sourcing_name);
22982 sourcing_name = save_sourcing_name;
22983 sourcing_lnum = save_sourcing_lnum;
22984 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022985#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022986 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022987 script_prof_restore(&wait_start);
22988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989
22990 if (p_verbose >= 12 && sourcing_name != NULL)
22991 {
22992 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022993 verbose_enter_scroll();
22994
Bram Moolenaar555b2802005-05-19 21:08:39 +000022995 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022997
22998 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 --no_wait_return;
23000 }
23001
23002 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023003 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023005
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023006 /* If the a:000 list and the l: and a: dicts are not referenced we can
23007 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023008 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23009 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23010 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23011 {
23012 free_funccal(fc, FALSE);
23013 }
23014 else
23015 {
23016 hashitem_T *hi;
23017 listitem_T *li;
23018 int todo;
23019
23020 /* "fc" is still in use. This can happen when returning "a:000" or
23021 * assigning "l:" to a global variable.
23022 * Link "fc" in the list for garbage collection later. */
23023 fc->caller = previous_funccal;
23024 previous_funccal = fc;
23025
23026 /* Make a copy of the a: variables, since we didn't do that above. */
23027 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23028 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23029 {
23030 if (!HASHITEM_EMPTY(hi))
23031 {
23032 --todo;
23033 v = HI2DI(hi);
23034 copy_tv(&v->di_tv, &v->di_tv);
23035 }
23036 }
23037
23038 /* Make a copy of the a:000 items, since we didn't do that above. */
23039 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23040 copy_tv(&li->li_tv, &li->li_tv);
23041 }
23042}
23043
23044/*
23045 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023046 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023047 */
23048 static int
23049can_free_funccal(fc, copyID)
23050 funccall_T *fc;
23051 int copyID;
23052{
23053 return (fc->l_varlist.lv_copyID != copyID
23054 && fc->l_vars.dv_copyID != copyID
23055 && fc->l_avars.dv_copyID != copyID);
23056}
23057
23058/*
23059 * Free "fc" and what it contains.
23060 */
23061 static void
23062free_funccal(fc, free_val)
23063 funccall_T *fc;
23064 int free_val; /* a: vars were allocated */
23065{
23066 listitem_T *li;
23067
23068 /* The a: variables typevals may not have been allocated, only free the
23069 * allocated variables. */
23070 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23071
23072 /* free all l: variables */
23073 vars_clear(&fc->l_vars.dv_hashtab);
23074
23075 /* Free the a:000 variables if they were allocated. */
23076 if (free_val)
23077 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23078 clear_tv(&li->li_tv);
23079
23080 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081}
23082
23083/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 * Add a number variable "name" to dict "dp" with value "nr".
23085 */
23086 static void
23087add_nr_var(dp, v, name, nr)
23088 dict_T *dp;
23089 dictitem_T *v;
23090 char *name;
23091 varnumber_T nr;
23092{
23093 STRCPY(v->di_key, name);
23094 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23095 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23096 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023097 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 v->di_tv.vval.v_number = nr;
23099}
23100
23101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 * ":return [expr]"
23103 */
23104 void
23105ex_return(eap)
23106 exarg_T *eap;
23107{
23108 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 int returning = FALSE;
23111
23112 if (current_funccal == NULL)
23113 {
23114 EMSG(_("E133: :return not inside a function"));
23115 return;
23116 }
23117
23118 if (eap->skip)
23119 ++emsg_skip;
23120
23121 eap->nextcmd = NULL;
23122 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023123 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 {
23125 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023126 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023128 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 }
23130 /* It's safer to return also on error. */
23131 else if (!eap->skip)
23132 {
23133 /*
23134 * Return unless the expression evaluation has been cancelled due to an
23135 * aborting error, an interrupt, or an exception.
23136 */
23137 if (!aborting())
23138 returning = do_return(eap, FALSE, TRUE, NULL);
23139 }
23140
23141 /* When skipping or the return gets pending, advance to the next command
23142 * in this line (!returning). Otherwise, ignore the rest of the line.
23143 * Following lines will be ignored by get_func_line(). */
23144 if (returning)
23145 eap->nextcmd = NULL;
23146 else if (eap->nextcmd == NULL) /* no argument */
23147 eap->nextcmd = check_nextcmd(arg);
23148
23149 if (eap->skip)
23150 --emsg_skip;
23151}
23152
23153/*
23154 * Return from a function. Possibly makes the return pending. Also called
23155 * for a pending return at the ":endtry" or after returning from an extra
23156 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023157 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023158 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 * FALSE when the return gets pending.
23160 */
23161 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023162do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 exarg_T *eap;
23164 int reanimate;
23165 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023166 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167{
23168 int idx;
23169 struct condstack *cstack = eap->cstack;
23170
23171 if (reanimate)
23172 /* Undo the return. */
23173 current_funccal->returned = FALSE;
23174
23175 /*
23176 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23177 * not in its finally clause (which then is to be executed next) is found.
23178 * In this case, make the ":return" pending for execution at the ":endtry".
23179 * Otherwise, return normally.
23180 */
23181 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23182 if (idx >= 0)
23183 {
23184 cstack->cs_pending[idx] = CSTP_RETURN;
23185
23186 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 /* A pending return again gets pending. "rettv" points to an
23188 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023190 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 else
23192 {
23193 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023194 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023196 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023198 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 {
23200 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023201 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023202 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 else
23204 EMSG(_(e_outofmem));
23205 }
23206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023207 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208
23209 if (reanimate)
23210 {
23211 /* The pending return value could be overwritten by a ":return"
23212 * without argument in a finally clause; reset the default
23213 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023214 current_funccal->rettv->v_type = VAR_NUMBER;
23215 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 }
23217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023218 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 }
23220 else
23221 {
23222 current_funccal->returned = TRUE;
23223
23224 /* If the return is carried out now, store the return value. For
23225 * a return immediately after reanimation, the value is already
23226 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023227 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023229 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023230 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023232 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 }
23234 }
23235
23236 return idx < 0;
23237}
23238
23239/*
23240 * Free the variable with a pending return value.
23241 */
23242 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023243discard_pending_return(rettv)
23244 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245{
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247}
23248
23249/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023250 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 * is an allocated string. Used by report_pending() for verbose messages.
23252 */
23253 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023254get_return_cmd(rettv)
23255 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023257 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023258 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023259 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023261 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023262 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023263 if (s == NULL)
23264 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023265
23266 STRCPY(IObuff, ":return ");
23267 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23268 if (STRLEN(s) + 8 >= IOSIZE)
23269 STRCPY(IObuff + IOSIZE - 4, "...");
23270 vim_free(tofree);
23271 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272}
23273
23274/*
23275 * Get next function line.
23276 * Called by do_cmdline() to get the next line.
23277 * Returns allocated string, or NULL for end of function.
23278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 char_u *
23280get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023281 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023283 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284{
Bram Moolenaar33570922005-01-25 22:26:29 +000023285 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023286 ufunc_T *fp = fcp->func;
23287 char_u *retval;
23288 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289
23290 /* If breakpoints have been added/deleted need to check for it. */
23291 if (fcp->dbg_tick != debug_tick)
23292 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023293 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 sourcing_lnum);
23295 fcp->dbg_tick = debug_tick;
23296 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023297#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023298 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023299 func_line_end(cookie);
23300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301
Bram Moolenaar05159a02005-02-26 23:04:13 +000023302 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023303 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23304 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 retval = NULL;
23306 else
23307 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023308 /* Skip NULL lines (continuation lines). */
23309 while (fcp->linenr < gap->ga_len
23310 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23311 ++fcp->linenr;
23312 if (fcp->linenr >= gap->ga_len)
23313 retval = NULL;
23314 else
23315 {
23316 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23317 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023318#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023319 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023320 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023321#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 }
23324
23325 /* Did we encounter a breakpoint? */
23326 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23327 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023328 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023330 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 sourcing_lnum);
23332 fcp->dbg_tick = debug_tick;
23333 }
23334
23335 return retval;
23336}
23337
Bram Moolenaar05159a02005-02-26 23:04:13 +000023338#if defined(FEAT_PROFILE) || defined(PROTO)
23339/*
23340 * Called when starting to read a function line.
23341 * "sourcing_lnum" must be correct!
23342 * When skipping lines it may not actually be executed, but we won't find out
23343 * until later and we need to store the time now.
23344 */
23345 void
23346func_line_start(cookie)
23347 void *cookie;
23348{
23349 funccall_T *fcp = (funccall_T *)cookie;
23350 ufunc_T *fp = fcp->func;
23351
23352 if (fp->uf_profiling && sourcing_lnum >= 1
23353 && sourcing_lnum <= fp->uf_lines.ga_len)
23354 {
23355 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023356 /* Skip continuation lines. */
23357 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23358 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023359 fp->uf_tml_execed = FALSE;
23360 profile_start(&fp->uf_tml_start);
23361 profile_zero(&fp->uf_tml_children);
23362 profile_get_wait(&fp->uf_tml_wait);
23363 }
23364}
23365
23366/*
23367 * Called when actually executing a function line.
23368 */
23369 void
23370func_line_exec(cookie)
23371 void *cookie;
23372{
23373 funccall_T *fcp = (funccall_T *)cookie;
23374 ufunc_T *fp = fcp->func;
23375
23376 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23377 fp->uf_tml_execed = TRUE;
23378}
23379
23380/*
23381 * Called when done with a function line.
23382 */
23383 void
23384func_line_end(cookie)
23385 void *cookie;
23386{
23387 funccall_T *fcp = (funccall_T *)cookie;
23388 ufunc_T *fp = fcp->func;
23389
23390 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23391 {
23392 if (fp->uf_tml_execed)
23393 {
23394 ++fp->uf_tml_count[fp->uf_tml_idx];
23395 profile_end(&fp->uf_tml_start);
23396 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023397 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023398 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23399 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023400 }
23401 fp->uf_tml_idx = -1;
23402 }
23403}
23404#endif
23405
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406/*
23407 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023408 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 */
23410 int
23411func_has_ended(cookie)
23412 void *cookie;
23413{
Bram Moolenaar33570922005-01-25 22:26:29 +000023414 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415
23416 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23417 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023418 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 || fcp->returned);
23420}
23421
23422/*
23423 * return TRUE if cookie indicates a function which "abort"s on errors.
23424 */
23425 int
23426func_has_abort(cookie)
23427 void *cookie;
23428{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023429 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430}
23431
23432#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23433typedef enum
23434{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023435 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23436 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23437 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438} var_flavour_T;
23439
23440static var_flavour_T var_flavour __ARGS((char_u *varname));
23441
23442 static var_flavour_T
23443var_flavour(varname)
23444 char_u *varname;
23445{
23446 char_u *p = varname;
23447
23448 if (ASCII_ISUPPER(*p))
23449 {
23450 while (*(++p))
23451 if (ASCII_ISLOWER(*p))
23452 return VAR_FLAVOUR_SESSION;
23453 return VAR_FLAVOUR_VIMINFO;
23454 }
23455 else
23456 return VAR_FLAVOUR_DEFAULT;
23457}
23458#endif
23459
23460#if defined(FEAT_VIMINFO) || defined(PROTO)
23461/*
23462 * Restore global vars that start with a capital from the viminfo file
23463 */
23464 int
23465read_viminfo_varlist(virp, writing)
23466 vir_T *virp;
23467 int writing;
23468{
23469 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023470 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472
23473 if (!writing && (find_viminfo_parameter('!') != NULL))
23474 {
23475 tab = vim_strchr(virp->vir_line + 1, '\t');
23476 if (tab != NULL)
23477 {
23478 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023479 switch (*tab)
23480 {
23481 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023482#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023483 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023484#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023485 case 'D': type = VAR_DICT; break;
23486 case 'L': type = VAR_LIST; break;
23487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488
23489 tab = vim_strchr(tab, '\t');
23490 if (tab != NULL)
23491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023492 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023493 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023494 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023496#ifdef FEAT_FLOAT
23497 else if (type == VAR_FLOAT)
23498 (void)string2float(tab + 1, &tv.vval.v_float);
23499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023501 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023502 if (type == VAR_DICT || type == VAR_LIST)
23503 {
23504 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23505
23506 if (etv == NULL)
23507 /* Failed to parse back the dict or list, use it as a
23508 * string. */
23509 tv.v_type = VAR_STRING;
23510 else
23511 {
23512 vim_free(tv.vval.v_string);
23513 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023514 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023515 }
23516 }
23517
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023518 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023519
23520 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023521 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023522 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23523 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 }
23525 }
23526 }
23527
23528 return viminfo_readline(virp);
23529}
23530
23531/*
23532 * Write global vars that start with a capital to the viminfo file
23533 */
23534 void
23535write_viminfo_varlist(fp)
23536 FILE *fp;
23537{
Bram Moolenaar33570922005-01-25 22:26:29 +000023538 hashitem_T *hi;
23539 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023540 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023541 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023543 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023544 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545
23546 if (find_viminfo_parameter('!') == NULL)
23547 return;
23548
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023549 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023551 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023552 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023554 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023556 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 this_var = HI2DI(hi);
23558 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023561 {
23562 case VAR_STRING: s = "STR"; break;
23563 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023564#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023565 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023566#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023567 case VAR_DICT: s = "DIC"; break;
23568 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023569 default: continue;
23570 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023571 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023572 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023573 if (p != NULL)
23574 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023575 vim_free(tofree);
23576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 }
23578 }
23579}
23580#endif
23581
23582#if defined(FEAT_SESSION) || defined(PROTO)
23583 int
23584store_session_globals(fd)
23585 FILE *fd;
23586{
Bram Moolenaar33570922005-01-25 22:26:29 +000023587 hashitem_T *hi;
23588 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023589 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 char_u *p, *t;
23591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023592 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023593 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023595 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023597 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023598 this_var = HI2DI(hi);
23599 if ((this_var->di_tv.v_type == VAR_NUMBER
23600 || this_var->di_tv.v_type == VAR_STRING)
23601 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023602 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023603 /* Escape special characters with a backslash. Turn a LF and
23604 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023605 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023606 (char_u *)"\\\"\n\r");
23607 if (p == NULL) /* out of memory */
23608 break;
23609 for (t = p; *t != NUL; ++t)
23610 if (*t == '\n')
23611 *t = 'n';
23612 else if (*t == '\r')
23613 *t = 'r';
23614 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023615 this_var->di_key,
23616 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23617 : ' ',
23618 p,
23619 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23620 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023621 || put_eol(fd) == FAIL)
23622 {
23623 vim_free(p);
23624 return FAIL;
23625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 vim_free(p);
23627 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023628#ifdef FEAT_FLOAT
23629 else if (this_var->di_tv.v_type == VAR_FLOAT
23630 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23631 {
23632 float_T f = this_var->di_tv.vval.v_float;
23633 int sign = ' ';
23634
23635 if (f < 0)
23636 {
23637 f = -f;
23638 sign = '-';
23639 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023640 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023641 this_var->di_key, sign, f) < 0)
23642 || put_eol(fd) == FAIL)
23643 return FAIL;
23644 }
23645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 }
23647 }
23648 return OK;
23649}
23650#endif
23651
Bram Moolenaar661b1822005-07-28 22:36:45 +000023652/*
23653 * Display script name where an item was last set.
23654 * Should only be invoked when 'verbose' is non-zero.
23655 */
23656 void
23657last_set_msg(scriptID)
23658 scid_T scriptID;
23659{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023660 char_u *p;
23661
Bram Moolenaar661b1822005-07-28 22:36:45 +000023662 if (scriptID != 0)
23663 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023664 p = home_replace_save(NULL, get_scriptname(scriptID));
23665 if (p != NULL)
23666 {
23667 verbose_enter();
23668 MSG_PUTS(_("\n\tLast set from "));
23669 MSG_PUTS(p);
23670 vim_free(p);
23671 verbose_leave();
23672 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023673 }
23674}
23675
Bram Moolenaard812df62008-11-09 12:46:09 +000023676/*
23677 * List v:oldfiles in a nice way.
23678 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023679 void
23680ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023681 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023682{
23683 list_T *l = vimvars[VV_OLDFILES].vv_list;
23684 listitem_T *li;
23685 int nr = 0;
23686
23687 if (l == NULL)
23688 msg((char_u *)_("No old files"));
23689 else
23690 {
23691 msg_start();
23692 msg_scroll = TRUE;
23693 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23694 {
23695 msg_outnum((long)++nr);
23696 MSG_PUTS(": ");
23697 msg_outtrans(get_tv_string(&li->li_tv));
23698 msg_putchar('\n');
23699 out_flush(); /* output one line at a time */
23700 ui_breakcheck();
23701 }
23702 /* Assume "got_int" was set to truncate the listing. */
23703 got_int = FALSE;
23704
23705#ifdef FEAT_BROWSE_CMD
23706 if (cmdmod.browse)
23707 {
23708 quit_more = FALSE;
23709 nr = prompt_for_number(FALSE);
23710 msg_starthere();
23711 if (nr > 0)
23712 {
23713 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23714 (long)nr);
23715
23716 if (p != NULL)
23717 {
23718 p = expand_env_save(p);
23719 eap->arg = p;
23720 eap->cmdidx = CMD_edit;
23721 cmdmod.browse = FALSE;
23722 do_exedit(eap, NULL);
23723 vim_free(p);
23724 }
23725 }
23726 }
23727#endif
23728 }
23729}
23730
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731#endif /* FEAT_EVAL */
23732
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023734#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735
23736#ifdef WIN3264
23737/*
23738 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23739 */
23740static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23741static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23742static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23743
23744/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023745 * Get the short path (8.3) for the filename in "fnamep".
23746 * Only works for a valid file name.
23747 * When the path gets longer "fnamep" is changed and the allocated buffer
23748 * is put in "bufp".
23749 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23750 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 */
23752 static int
23753get_short_pathname(fnamep, bufp, fnamelen)
23754 char_u **fnamep;
23755 char_u **bufp;
23756 int *fnamelen;
23757{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023758 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 char_u *newbuf;
23760
23761 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 l = GetShortPathName(*fnamep, *fnamep, len);
23763 if (l > len - 1)
23764 {
23765 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023766 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 newbuf = vim_strnsave(*fnamep, l);
23768 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770
23771 vim_free(*bufp);
23772 *fnamep = *bufp = newbuf;
23773
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023774 /* Really should always succeed, as the buffer is big enough. */
23775 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 }
23777
23778 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023779 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780}
23781
23782/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023783 * Get the short path (8.3) for the filename in "fname". The converted
23784 * path is returned in "bufp".
23785 *
23786 * Some of the directories specified in "fname" may not exist. This function
23787 * will shorten the existing directories at the beginning of the path and then
23788 * append the remaining non-existing path.
23789 *
23790 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023791 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023792 * bufp - Pointer to an allocated buffer for the filename.
23793 * fnamelen - Length of the filename pointed to by fname
23794 *
23795 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 */
23797 static int
23798shortpath_for_invalid_fname(fname, bufp, fnamelen)
23799 char_u **fname;
23800 char_u **bufp;
23801 int *fnamelen;
23802{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023803 char_u *short_fname, *save_fname, *pbuf_unused;
23804 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023806 int old_len, len;
23807 int new_len, sfx_len;
23808 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809
23810 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023811 old_len = *fnamelen;
23812 save_fname = vim_strnsave(*fname, old_len);
23813 pbuf_unused = NULL;
23814 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023816 endp = save_fname + old_len - 1; /* Find the end of the copy */
23817 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023819 /*
23820 * Try shortening the supplied path till it succeeds by removing one
23821 * directory at a time from the tail of the path.
23822 */
23823 len = 0;
23824 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023826 /* go back one path-separator */
23827 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23828 --endp;
23829 if (endp <= save_fname)
23830 break; /* processed the complete path */
23831
23832 /*
23833 * Replace the path separator with a NUL and try to shorten the
23834 * resulting path.
23835 */
23836 ch = *endp;
23837 *endp = 0;
23838 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023839 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023840 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23841 {
23842 retval = FAIL;
23843 goto theend;
23844 }
23845 *endp = ch; /* preserve the string */
23846
23847 if (len > 0)
23848 break; /* successfully shortened the path */
23849
23850 /* failed to shorten the path. Skip the path separator */
23851 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 }
23853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023854 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023856 /*
23857 * Succeeded in shortening the path. Now concatenate the shortened
23858 * path with the remaining path at the tail.
23859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023861 /* Compute the length of the new path. */
23862 sfx_len = (int)(save_endp - endp) + 1;
23863 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023865 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023867 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023869 /* There is not enough space in the currently allocated string,
23870 * copy it to a buffer big enough. */
23871 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023873 {
23874 retval = FAIL;
23875 goto theend;
23876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 }
23878 else
23879 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023880 /* Transfer short_fname to the main buffer (it's big enough),
23881 * unless get_short_pathname() did its work in-place. */
23882 *fname = *bufp = save_fname;
23883 if (short_fname != save_fname)
23884 vim_strncpy(save_fname, short_fname, len);
23885 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023887
23888 /* concat the not-shortened part of the path */
23889 vim_strncpy(*fname + len, endp, sfx_len);
23890 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023892
23893theend:
23894 vim_free(pbuf_unused);
23895 vim_free(save_fname);
23896
23897 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898}
23899
23900/*
23901 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023902 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 */
23904 static int
23905shortpath_for_partial(fnamep, bufp, fnamelen)
23906 char_u **fnamep;
23907 char_u **bufp;
23908 int *fnamelen;
23909{
23910 int sepcount, len, tflen;
23911 char_u *p;
23912 char_u *pbuf, *tfname;
23913 int hasTilde;
23914
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023915 /* Count up the path separators from the RHS.. so we know which part
23916 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023918 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 if (vim_ispathsep(*p))
23920 ++sepcount;
23921
23922 /* Need full path first (use expand_env() to remove a "~/") */
23923 hasTilde = (**fnamep == '~');
23924 if (hasTilde)
23925 pbuf = tfname = expand_env_save(*fnamep);
23926 else
23927 pbuf = tfname = FullName_save(*fnamep, FALSE);
23928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023929 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023931 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933
23934 if (len == 0)
23935 {
23936 /* Don't have a valid filename, so shorten the rest of the
23937 * path if we can. This CAN give us invalid 8.3 filenames, but
23938 * there's not a lot of point in guessing what it might be.
23939 */
23940 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023941 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
23944
23945 /* Count the paths backward to find the beginning of the desired string. */
23946 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023947 {
23948#ifdef FEAT_MBYTE
23949 if (has_mbyte)
23950 p -= mb_head_off(tfname, p);
23951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 if (vim_ispathsep(*p))
23953 {
23954 if (sepcount == 0 || (hasTilde && sepcount == 1))
23955 break;
23956 else
23957 sepcount --;
23958 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 if (hasTilde)
23961 {
23962 --p;
23963 if (p >= tfname)
23964 *p = '~';
23965 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 }
23968 else
23969 ++p;
23970
23971 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23972 vim_free(*bufp);
23973 *fnamelen = (int)STRLEN(p);
23974 *bufp = pbuf;
23975 *fnamep = p;
23976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978}
23979#endif /* WIN3264 */
23980
23981/*
23982 * Adjust a filename, according to a string of modifiers.
23983 * *fnamep must be NUL terminated when called. When returning, the length is
23984 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023985 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 * When there is an error, *fnamep is set to NULL.
23987 */
23988 int
23989modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23990 char_u *src; /* string with modifiers */
23991 int *usedlen; /* characters after src that are used */
23992 char_u **fnamep; /* file name so far */
23993 char_u **bufp; /* buffer for allocated file name or NULL */
23994 int *fnamelen; /* length of fnamep */
23995{
23996 int valid = 0;
23997 char_u *tail;
23998 char_u *s, *p, *pbuf;
23999 char_u dirname[MAXPATHL];
24000 int c;
24001 int has_fullname = 0;
24002#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024003 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 int has_shortname = 0;
24005#endif
24006
24007repeat:
24008 /* ":p" - full path/file_name */
24009 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24010 {
24011 has_fullname = 1;
24012
24013 valid |= VALID_PATH;
24014 *usedlen += 2;
24015
24016 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24017 if ((*fnamep)[0] == '~'
24018#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24019 && ((*fnamep)[1] == '/'
24020# ifdef BACKSLASH_IN_FILENAME
24021 || (*fnamep)[1] == '\\'
24022# endif
24023 || (*fnamep)[1] == NUL)
24024
24025#endif
24026 )
24027 {
24028 *fnamep = expand_env_save(*fnamep);
24029 vim_free(*bufp); /* free any allocated file name */
24030 *bufp = *fnamep;
24031 if (*fnamep == NULL)
24032 return -1;
24033 }
24034
24035 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024036 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 {
24038 if (vim_ispathsep(*p)
24039 && p[1] == '.'
24040 && (p[2] == NUL
24041 || vim_ispathsep(p[2])
24042 || (p[2] == '.'
24043 && (p[3] == NUL || vim_ispathsep(p[3])))))
24044 break;
24045 }
24046
24047 /* FullName_save() is slow, don't use it when not needed. */
24048 if (*p != NUL || !vim_isAbsName(*fnamep))
24049 {
24050 *fnamep = FullName_save(*fnamep, *p != NUL);
24051 vim_free(*bufp); /* free any allocated file name */
24052 *bufp = *fnamep;
24053 if (*fnamep == NULL)
24054 return -1;
24055 }
24056
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024057#ifdef WIN3264
24058# if _WIN32_WINNT >= 0x0500
24059 if (vim_strchr(*fnamep, '~') != NULL)
24060 {
24061 /* Expand 8.3 filename to full path. Needed to make sure the same
24062 * file does not have two different names.
24063 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24064 p = alloc(_MAX_PATH + 1);
24065 if (p != NULL)
24066 {
24067 if (GetLongPathName(*fnamep, p, MAXPATHL))
24068 {
24069 vim_free(*bufp);
24070 *bufp = *fnamep = p;
24071 }
24072 else
24073 vim_free(p);
24074 }
24075 }
24076# endif
24077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 /* Append a path separator to a directory. */
24079 if (mch_isdir(*fnamep))
24080 {
24081 /* Make room for one or two extra characters. */
24082 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24083 vim_free(*bufp); /* free any allocated file name */
24084 *bufp = *fnamep;
24085 if (*fnamep == NULL)
24086 return -1;
24087 add_pathsep(*fnamep);
24088 }
24089 }
24090
24091 /* ":." - path relative to the current directory */
24092 /* ":~" - path relative to the home directory */
24093 /* ":8" - shortname path - postponed till after */
24094 while (src[*usedlen] == ':'
24095 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24096 {
24097 *usedlen += 2;
24098 if (c == '8')
24099 {
24100#ifdef WIN3264
24101 has_shortname = 1; /* Postpone this. */
24102#endif
24103 continue;
24104 }
24105 pbuf = NULL;
24106 /* Need full path first (use expand_env() to remove a "~/") */
24107 if (!has_fullname)
24108 {
24109 if (c == '.' && **fnamep == '~')
24110 p = pbuf = expand_env_save(*fnamep);
24111 else
24112 p = pbuf = FullName_save(*fnamep, FALSE);
24113 }
24114 else
24115 p = *fnamep;
24116
24117 has_fullname = 0;
24118
24119 if (p != NULL)
24120 {
24121 if (c == '.')
24122 {
24123 mch_dirname(dirname, MAXPATHL);
24124 s = shorten_fname(p, dirname);
24125 if (s != NULL)
24126 {
24127 *fnamep = s;
24128 if (pbuf != NULL)
24129 {
24130 vim_free(*bufp); /* free any allocated file name */
24131 *bufp = pbuf;
24132 pbuf = NULL;
24133 }
24134 }
24135 }
24136 else
24137 {
24138 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24139 /* Only replace it when it starts with '~' */
24140 if (*dirname == '~')
24141 {
24142 s = vim_strsave(dirname);
24143 if (s != NULL)
24144 {
24145 *fnamep = s;
24146 vim_free(*bufp);
24147 *bufp = s;
24148 }
24149 }
24150 }
24151 vim_free(pbuf);
24152 }
24153 }
24154
24155 tail = gettail(*fnamep);
24156 *fnamelen = (int)STRLEN(*fnamep);
24157
24158 /* ":h" - head, remove "/file_name", can be repeated */
24159 /* Don't remove the first "/" or "c:\" */
24160 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24161 {
24162 valid |= VALID_HEAD;
24163 *usedlen += 2;
24164 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024165 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024166 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 *fnamelen = (int)(tail - *fnamep);
24168#ifdef VMS
24169 if (*fnamelen > 0)
24170 *fnamelen += 1; /* the path separator is part of the path */
24171#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024172 if (*fnamelen == 0)
24173 {
24174 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24175 p = vim_strsave((char_u *)".");
24176 if (p == NULL)
24177 return -1;
24178 vim_free(*bufp);
24179 *bufp = *fnamep = tail = p;
24180 *fnamelen = 1;
24181 }
24182 else
24183 {
24184 while (tail > s && !after_pathsep(s, tail))
24185 mb_ptr_back(*fnamep, tail);
24186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 }
24188
24189 /* ":8" - shortname */
24190 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24191 {
24192 *usedlen += 2;
24193#ifdef WIN3264
24194 has_shortname = 1;
24195#endif
24196 }
24197
24198#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024199 /*
24200 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201 */
24202 if (has_shortname)
24203 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024204 /* Copy the string if it is shortened by :h and when it wasn't copied
24205 * yet, because we are going to change it in place. Avoids changing
24206 * the buffer name for "%:8". */
24207 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 {
24209 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024210 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 return -1;
24212 vim_free(*bufp);
24213 *bufp = *fnamep = p;
24214 }
24215
24216 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024217 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 if (!has_fullname && !vim_isAbsName(*fnamep))
24219 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024220 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 return -1;
24222 }
24223 else
24224 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024225 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226
Bram Moolenaardc935552011-08-17 15:23:23 +020024227 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024229 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 return -1;
24231
24232 if (l == 0)
24233 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024234 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024236 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 return -1;
24238 }
24239 *fnamelen = l;
24240 }
24241 }
24242#endif /* WIN3264 */
24243
24244 /* ":t" - tail, just the basename */
24245 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24246 {
24247 *usedlen += 2;
24248 *fnamelen -= (int)(tail - *fnamep);
24249 *fnamep = tail;
24250 }
24251
24252 /* ":e" - extension, can be repeated */
24253 /* ":r" - root, without extension, can be repeated */
24254 while (src[*usedlen] == ':'
24255 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24256 {
24257 /* find a '.' in the tail:
24258 * - for second :e: before the current fname
24259 * - otherwise: The last '.'
24260 */
24261 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24262 s = *fnamep - 2;
24263 else
24264 s = *fnamep + *fnamelen - 1;
24265 for ( ; s > tail; --s)
24266 if (s[0] == '.')
24267 break;
24268 if (src[*usedlen + 1] == 'e') /* :e */
24269 {
24270 if (s > tail)
24271 {
24272 *fnamelen += (int)(*fnamep - (s + 1));
24273 *fnamep = s + 1;
24274#ifdef VMS
24275 /* cut version from the extension */
24276 s = *fnamep + *fnamelen - 1;
24277 for ( ; s > *fnamep; --s)
24278 if (s[0] == ';')
24279 break;
24280 if (s > *fnamep)
24281 *fnamelen = s - *fnamep;
24282#endif
24283 }
24284 else if (*fnamep <= tail)
24285 *fnamelen = 0;
24286 }
24287 else /* :r */
24288 {
24289 if (s > tail) /* remove one extension */
24290 *fnamelen = (int)(s - *fnamep);
24291 }
24292 *usedlen += 2;
24293 }
24294
24295 /* ":s?pat?foo?" - substitute */
24296 /* ":gs?pat?foo?" - global substitute */
24297 if (src[*usedlen] == ':'
24298 && (src[*usedlen + 1] == 's'
24299 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24300 {
24301 char_u *str;
24302 char_u *pat;
24303 char_u *sub;
24304 int sep;
24305 char_u *flags;
24306 int didit = FALSE;
24307
24308 flags = (char_u *)"";
24309 s = src + *usedlen + 2;
24310 if (src[*usedlen + 1] == 'g')
24311 {
24312 flags = (char_u *)"g";
24313 ++s;
24314 }
24315
24316 sep = *s++;
24317 if (sep)
24318 {
24319 /* find end of pattern */
24320 p = vim_strchr(s, sep);
24321 if (p != NULL)
24322 {
24323 pat = vim_strnsave(s, (int)(p - s));
24324 if (pat != NULL)
24325 {
24326 s = p + 1;
24327 /* find end of substitution */
24328 p = vim_strchr(s, sep);
24329 if (p != NULL)
24330 {
24331 sub = vim_strnsave(s, (int)(p - s));
24332 str = vim_strnsave(*fnamep, *fnamelen);
24333 if (sub != NULL && str != NULL)
24334 {
24335 *usedlen = (int)(p + 1 - src);
24336 s = do_string_sub(str, pat, sub, flags);
24337 if (s != NULL)
24338 {
24339 *fnamep = s;
24340 *fnamelen = (int)STRLEN(s);
24341 vim_free(*bufp);
24342 *bufp = s;
24343 didit = TRUE;
24344 }
24345 }
24346 vim_free(sub);
24347 vim_free(str);
24348 }
24349 vim_free(pat);
24350 }
24351 }
24352 /* after using ":s", repeat all the modifiers */
24353 if (didit)
24354 goto repeat;
24355 }
24356 }
24357
Bram Moolenaar26df0922014-02-23 23:39:13 +010024358 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24359 {
24360 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24361 if (p == NULL)
24362 return -1;
24363 vim_free(*bufp);
24364 *bufp = *fnamep = p;
24365 *fnamelen = (int)STRLEN(p);
24366 *usedlen += 2;
24367 }
24368
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 return valid;
24370}
24371
24372/*
24373 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24374 * "flags" can be "g" to do a global substitute.
24375 * Returns an allocated string, NULL for error.
24376 */
24377 char_u *
24378do_string_sub(str, pat, sub, flags)
24379 char_u *str;
24380 char_u *pat;
24381 char_u *sub;
24382 char_u *flags;
24383{
24384 int sublen;
24385 regmatch_T regmatch;
24386 int i;
24387 int do_all;
24388 char_u *tail;
24389 garray_T ga;
24390 char_u *ret;
24391 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024392 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393
24394 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24395 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024396 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397
24398 ga_init2(&ga, 1, 200);
24399
24400 do_all = (flags[0] == 'g');
24401
24402 regmatch.rm_ic = p_ic;
24403 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24404 if (regmatch.regprog != NULL)
24405 {
24406 tail = str;
24407 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24408 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024409 /* Skip empty match except for first match. */
24410 if (regmatch.startp[0] == regmatch.endp[0])
24411 {
24412 if (zero_width == regmatch.startp[0])
24413 {
24414 /* avoid getting stuck on a match with an empty string */
24415 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24416 ++ga.ga_len;
24417 continue;
24418 }
24419 zero_width = regmatch.startp[0];
24420 }
24421
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422 /*
24423 * Get some space for a temporary buffer to do the substitution
24424 * into. It will contain:
24425 * - The text up to where the match is.
24426 * - The substituted text.
24427 * - The text after the match.
24428 */
24429 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24430 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24431 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24432 {
24433 ga_clear(&ga);
24434 break;
24435 }
24436
24437 /* copy the text up to where the match is */
24438 i = (int)(regmatch.startp[0] - tail);
24439 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24440 /* add the substituted text */
24441 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24442 + ga.ga_len + i, TRUE, TRUE, FALSE);
24443 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024444 tail = regmatch.endp[0];
24445 if (*tail == NUL)
24446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 if (!do_all)
24448 break;
24449 }
24450
24451 if (ga.ga_data != NULL)
24452 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24453
Bram Moolenaar473de612013-06-08 18:19:48 +020024454 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 }
24456
24457 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24458 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024459 if (p_cpo == empty_option)
24460 p_cpo = save_cpo;
24461 else
24462 /* Darn, evaluating {sub} expression changed the value. */
24463 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464
24465 return ret;
24466}
24467
24468#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */