blob: 246e404bc87e1234159e4c680de8b46169a606e4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518#ifdef FEAT_FLOAT
519static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
520#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000523static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000524static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
530static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000534static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000543static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000545static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000551static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000559static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000560static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000561static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000562static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200565static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000566static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000574static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000588static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100593static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000595static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000607#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200608static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000609static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
610#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200611#ifdef FEAT_LUA
612static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
613#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000618static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000619static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000620static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000622static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000626#ifdef vim_mkdir
627static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100630#ifdef FEAT_MZSCHEME
631static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100635static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000636static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#ifdef FEAT_FLOAT
638static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000641static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000642static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200643#ifdef FEAT_PYTHON3
644static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
646#ifdef FEAT_PYTHON
647static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000651static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200666static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100668static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000671static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000673static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000680static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000681static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000682static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000683static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200685static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000686static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100688#ifdef FEAT_CRYPT
689static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
690#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000691static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200692static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100748static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100752static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000759static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100763static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000765static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000766static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static int get_env_len __ARGS((char_u **arg));
768static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
771#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
772#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
773 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000774static 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 +0000775static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000776static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100777static 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 +0000778static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static typval_T *alloc_tv __ARGS((void));
780static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void init_tv __ARGS((typval_T *varp));
782static long get_tv_number __ARGS((typval_T *varp));
783static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000784static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static char_u *get_tv_string __ARGS((typval_T *varp));
786static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000787static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100788static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
789static 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 +0000790static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
791static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
792static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000793static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
794static 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 +0000795static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
796static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000797static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200798static int var_check_func_name __ARGS((char_u *name, int new_var));
799static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000800static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000801static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
803static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
804static int eval_fname_script __ARGS((char_u *p));
805static int eval_fname_sid __ARGS((char_u *p));
806static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807static ufunc_T *find_func __ARGS((char_u *name));
808static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000809static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#ifdef FEAT_PROFILE
811static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000812static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
813static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_total_cmp __ARGS((const void *s1, const void *s2));
819static int
820# ifdef __BORLANDC__
821 _RTLENTRYF
822# endif
823 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200825static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000826static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000827static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static 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 +0000830static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
831static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
834static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000835static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000836static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000837static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200839
840#ifdef EBCDIC
841static int compare_func_name __ARGS((const void *s1, const void *s2));
842static void sortFunctions __ARGS(());
843#endif
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200854 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
855 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100879 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200880 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200881
882#ifdef EBCDIC
883 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100884 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885 */
886 sortFunctions();
887#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000888}
889
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000890#if defined(EXITFREE) || defined(PROTO)
891 void
892eval_clear()
893{
894 int i;
895 struct vimvar *p;
896
897 for (i = 0; i < VV_LEN; ++i)
898 {
899 p = &vimvars[i];
900 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000902 vim_free(p->vv_str);
903 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000904 }
905 else if (p->vv_di.di_tv.v_type == VAR_LIST)
906 {
907 list_unref(p->vv_list);
908 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000910 }
911 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000912 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913 hash_clear(&compat_hashtab);
914
Bram Moolenaard9fba312005-06-26 22:34:35 +0000915 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200917 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100918# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000919
920 /* global variables */
921 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000922
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000923 /* autoloaded script names */
924 ga_clear_strings(&ga_loaded);
925
Bram Moolenaarcca74132013-09-25 21:00:28 +0200926 /* Script-local variables. First clear all the variables and in a second
927 * loop free the scriptvar_T, because a variable in one script might hold
928 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200931 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200932 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 ga_clear(&ga_scripts);
934
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000935 /* unreferenced lists and dicts */
936 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000937
938 /* functions */
939 free_all_functions();
940 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000941}
942#endif
943
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 * Return the name of the executed function.
946 */
947 char_u *
948func_name(cookie)
949 void *cookie;
950{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000951 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954/*
955 * Return the address holding the next breakpoint line for a funccall cookie.
956 */
957 linenr_T *
958func_breakpoint(cookie)
959 void *cookie;
960{
Bram Moolenaar33570922005-01-25 22:26:29 +0000961 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Return the address holding the debug tick for a funccall cookie.
966 */
967 int *
968func_dbg_tick(cookie)
969 void *cookie;
970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
974/*
975 * Return the nesting level for a funccall cookie.
976 */
977 int
978func_level(cookie)
979 void *cookie;
980{
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983
984/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000985funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000987/* pointer to list of previously used funccal, still around because some
988 * item in it is still being used. */
989funccall_T *previous_funccal = NULL;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
992 * Return TRUE when a function was ended by a ":return" command.
993 */
994 int
995current_func_returned()
996{
997 return current_funccal->returned;
998}
999
1000
1001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Set an internal variable to a string value. Creates the variable if it does
1003 * not already exist.
1004 */
1005 void
1006set_internal_string_var(name, value)
1007 char_u *name;
1008 char_u *value;
1009{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001011 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 val = vim_strsave(value);
1014 if (val != NULL)
1015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001019 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 }
1023}
1024
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001026static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027static char_u *redir_endp = NULL;
1028static char_u *redir_varname = NULL;
1029
1030/*
1031 * Start recording command output to a variable
1032 * Returns OK if successfully completed the setup. FAIL otherwise.
1033 */
1034 int
1035var_redir_start(name, append)
1036 char_u *name;
1037 int append; /* append to an existing variable */
1038{
1039 int save_emsg;
1040 int err;
1041 typval_T tv;
1042
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001043 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001044 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 {
1046 EMSG(_(e_invarg));
1047 return FAIL;
1048 }
1049
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001050 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 redir_varname = vim_strsave(name);
1052 if (redir_varname == NULL)
1053 return FAIL;
1054
1055 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1056 if (redir_lval == NULL)
1057 {
1058 var_redir_stop();
1059 return FAIL;
1060 }
1061
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001062 /* The output is stored in growarray "redir_ga" until redirection ends. */
1063 ga_init2(&redir_ga, (int)sizeof(char), 500);
1064
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001066 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001067 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1069 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001070 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 if (redir_endp != NULL && *redir_endp != NUL)
1072 /* Trailing characters are present after the variable name */
1073 EMSG(_(e_trailing));
1074 else
1075 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001076 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 var_redir_stop();
1078 return FAIL;
1079 }
1080
1081 /* check if we can write to the variable: set it to or append an empty
1082 * string */
1083 save_emsg = did_emsg;
1084 did_emsg = FALSE;
1085 tv.v_type = VAR_STRING;
1086 tv.vval.v_string = (char_u *)"";
1087 if (append)
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1089 else
1090 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001091 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001093 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 if (err)
1095 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001096 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 var_redir_stop();
1098 return FAIL;
1099 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 * Append "value[value_len]" to the variable set by var_redir_start().
1106 * The actual appending is postponed until redirection ends, because the value
1107 * appended may in fact be the string we write to, changing it may cause freed
1108 * memory to be used:
1109 * :redir => foo
1110 * :let foo
1111 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 */
1113 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
1120 if (redir_lval == NULL)
1121 return;
1122
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 {
1130 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001131 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 }
1133 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135}
1136
1137/*
1138 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 */
1141 void
1142var_redir_stop()
1143{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001144 typval_T tv;
1145
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (redir_lval != NULL)
1147 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 /* If there was no error: assign the text to the variable. */
1149 if (redir_endp != NULL)
1150 {
1151 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1152 tv.v_type = VAR_STRING;
1153 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001154 /* Call get_lval() again, if it's inside a Dict or List it may
1155 * have changed. */
1156 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001157 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1159 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1160 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 /* free the collected output */
1164 vim_free(redir_ga.ga_data);
1165 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 vim_free(redir_lval);
1168 redir_lval = NULL;
1169 }
1170 vim_free(redir_varname);
1171 redir_varname = NULL;
1172}
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174# if defined(FEAT_MBYTE) || defined(PROTO)
1175 int
1176eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1177 char_u *enc_from;
1178 char_u *enc_to;
1179 char_u *fname_from;
1180 char_u *fname_to;
1181{
1182 int err = FALSE;
1183
1184 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1185 set_vim_var_string(VV_CC_TO, enc_to, -1);
1186 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1187 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1188 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1189 err = TRUE;
1190 set_vim_var_string(VV_CC_FROM, NULL, -1);
1191 set_vim_var_string(VV_CC_TO, NULL, -1);
1192 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1193 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1194
1195 if (err)
1196 return FAIL;
1197 return OK;
1198}
1199# endif
1200
1201# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1202 int
1203eval_printexpr(fname, args)
1204 char_u *fname;
1205 char_u *args;
1206{
1207 int err = FALSE;
1208
1209 set_vim_var_string(VV_FNAME_IN, fname, -1);
1210 set_vim_var_string(VV_CMDARG, args, -1);
1211 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1212 err = TRUE;
1213 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1214 set_vim_var_string(VV_CMDARG, NULL, -1);
1215
1216 if (err)
1217 {
1218 mch_remove(fname);
1219 return FAIL;
1220 }
1221 return OK;
1222}
1223# endif
1224
1225# if defined(FEAT_DIFF) || defined(PROTO)
1226 void
1227eval_diff(origfile, newfile, outfile)
1228 char_u *origfile;
1229 char_u *newfile;
1230 char_u *outfile;
1231{
1232 int err = FALSE;
1233
1234 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1235 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1236 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1237 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1238 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1239 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1240 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1241}
1242
1243 void
1244eval_patch(origfile, difffile, outfile)
1245 char_u *origfile;
1246 char_u *difffile;
1247 char_u *outfile;
1248{
1249 int err;
1250
1251 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1252 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1253 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1254 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258}
1259# endif
1260
1261/*
1262 * Top level evaluation function, returning a boolean.
1263 * Sets "error" to TRUE if there was an error.
1264 * Return TRUE or FALSE.
1265 */
1266 int
1267eval_to_bool(arg, error, nextcmd, skip)
1268 char_u *arg;
1269 int *error;
1270 char_u **nextcmd;
1271 int skip; /* only parse, don't execute */
1272{
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 int retval = FALSE;
1275
1276 if (skip)
1277 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 else
1281 {
1282 *error = FALSE;
1283 if (!skip)
1284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001285 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 }
1289 if (skip)
1290 --emsg_skip;
1291
1292 return retval;
1293}
1294
1295/*
1296 * Top level evaluation function, returning a string. If "skip" is TRUE,
1297 * only parsing to "nextcmd" is done, without reporting errors. Return
1298 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1299 */
1300 char_u *
1301eval_to_string_skip(arg, nextcmd, skip)
1302 char_u *arg;
1303 char_u **nextcmd;
1304 int skip; /* only parse, don't execute */
1305{
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 char_u *retval;
1308
1309 if (skip)
1310 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 retval = NULL;
1313 else
1314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 retval = vim_strsave(get_tv_string(&tv));
1316 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318 if (skip)
1319 --emsg_skip;
1320
1321 return retval;
1322}
1323
1324/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325 * Skip over an expression at "*pp".
1326 * Return FAIL for an error, OK otherwise.
1327 */
1328 int
1329skip_expr(pp)
1330 char_u **pp;
1331{
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001333
1334 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336}
1337
1338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 * When "convert" is TRUE convert a List into a sequence of lines and convert
1341 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 * Return pointer to allocated memory, or NULL for failure.
1343 */
1344 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *arg;
1347 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
Bram Moolenaar33570922005-01-25 22:26:29 +00001350 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001352 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 retval = NULL;
1359 else
1360 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001361 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 {
1363 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001366 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001367 if (tv.vval.v_list->lv_len > 0)
1368 ga_append(&ga, NL);
1369 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 ga_append(&ga, NUL);
1371 retval = (char_u *)ga.ga_data;
1372 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001373#ifdef FEAT_FLOAT
1374 else if (convert && tv.v_type == VAR_FLOAT)
1375 {
1376 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1377 retval = vim_strsave(numbuf);
1378 }
1379#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001380 else
1381 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384
1385 return retval;
1386}
1387
1388/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389 * Call eval_to_string() without using current local variables and using
1390 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 */
1392 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 char_u *arg;
1395 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 char_u *retval;
1399 void *save_funccalp;
1400
1401 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402 if (use_sandbox)
1403 ++sandbox;
1404 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001405 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 --sandbox;
1408 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 restore_funccal(save_funccalp);
1410 return retval;
1411}
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413/*
1414 * Top level evaluation function, returning a number.
1415 * Evaluates "expr" silently.
1416 * Returns -1 for an error.
1417 */
1418 int
1419eval_to_number(expr)
1420 char_u *expr;
1421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001424 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 ++emsg_off;
1427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = -1;
1430 else
1431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001432 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 --emsg_off;
1436
1437 return retval;
1438}
1439
Bram Moolenaara40058a2005-07-11 22:42:07 +00001440/*
1441 * Prepare v: variable "idx" to be used.
1442 * Save the current typeval in "save_tv".
1443 * When not used yet add the variable to the v: hashtable.
1444 */
1445 static void
1446prepare_vimvar(idx, save_tv)
1447 int idx;
1448 typval_T *save_tv;
1449{
1450 *save_tv = vimvars[idx].vv_tv;
1451 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1452 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1453}
1454
1455/*
1456 * Restore v: variable "idx" to typeval "save_tv".
1457 * When no longer defined, remove the variable from the v: hashtable.
1458 */
1459 static void
1460restore_vimvar(idx, save_tv)
1461 int idx;
1462 typval_T *save_tv;
1463{
1464 hashitem_T *hi;
1465
Bram Moolenaara40058a2005-07-11 22:42:07 +00001466 vimvars[idx].vv_tv = *save_tv;
1467 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1468 {
1469 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1470 if (HASHITEM_EMPTY(hi))
1471 EMSG2(_(e_intern2), "restore_vimvar()");
1472 else
1473 hash_remove(&vimvarht, hi);
1474 }
1475}
1476
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001477#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001478/*
1479 * Evaluate an expression to a list with suggestions.
1480 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001481 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482 */
1483 list_T *
1484eval_spell_expr(badword, expr)
1485 char_u *badword;
1486 char_u *expr;
1487{
1488 typval_T save_val;
1489 typval_T rettv;
1490 list_T *list = NULL;
1491 char_u *p = skipwhite(expr);
1492
1493 /* Set "v:val" to the bad word. */
1494 prepare_vimvar(VV_VAL, &save_val);
1495 vimvars[VV_VAL].vv_type = VAR_STRING;
1496 vimvars[VV_VAL].vv_str = badword;
1497 if (p_verbose == 0)
1498 ++emsg_off;
1499
1500 if (eval1(&p, &rettv, TRUE) == OK)
1501 {
1502 if (rettv.v_type != VAR_LIST)
1503 clear_tv(&rettv);
1504 else
1505 list = rettv.vval.v_list;
1506 }
1507
1508 if (p_verbose == 0)
1509 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510 restore_vimvar(VV_VAL, &save_val);
1511
1512 return list;
1513}
1514
1515/*
1516 * "list" is supposed to contain two items: a word and a number. Return the
1517 * word in "pp" and the number as the return value.
1518 * Return -1 if anything isn't right.
1519 * Used to get the good word and score from the eval_spell_expr() result.
1520 */
1521 int
1522get_spellword(list, pp)
1523 list_T *list;
1524 char_u **pp;
1525{
1526 listitem_T *li;
1527
1528 li = list->lv_first;
1529 if (li == NULL)
1530 return -1;
1531 *pp = get_tv_string(&li->li_tv);
1532
1533 li = li->li_next;
1534 if (li == NULL)
1535 return -1;
1536 return get_tv_number(&li->li_tv);
1537}
1538#endif
1539
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001541 * Top level evaluation function.
1542 * Returns an allocated typval_T with the result.
1543 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 */
1545 typval_T *
1546eval_expr(arg, nextcmd)
1547 char_u *arg;
1548 char_u **nextcmd;
1549{
1550 typval_T *tv;
1551
1552 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001553 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001554 {
1555 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001556 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001557 }
1558
1559 return tv;
1560}
1561
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565 * Uses argv[argc] for the function arguments. Only Number and String
1566 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001569 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001570call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 char_u *func;
1572 int argc;
1573 char_u **argv;
1574 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001575 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 long n;
1580 int len;
1581 int i;
1582 int doesrange;
1583 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001586 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 for (i = 0; i < argc; i++)
1591 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001592 /* Pass a NULL or empty argument as an empty string */
1593 if (argv[i] == NULL || *argv[i] == NUL)
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_STRING;
1596 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001597 continue;
1598 }
1599
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001600 if (str_arg_only)
1601 len = 0;
1602 else
1603 /* Recognize a number argument, the others must be strings. */
1604 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (len != 0 && len == (int)STRLEN(argv[i]))
1606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001607 argvars[i].v_type = VAR_NUMBER;
1608 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_STRING;
1613 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615 }
1616
1617 if (safe)
1618 {
1619 save_funccalp = save_funccal();
1620 ++sandbox;
1621 }
1622
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1624 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (safe)
1628 {
1629 --sandbox;
1630 restore_funccal(save_funccalp);
1631 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 vim_free(argvars);
1633
1634 if (ret == FAIL)
1635 clear_tv(rettv);
1636
1637 return ret;
1638}
1639
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001640/*
1641 * Call vimL function "func" and return the result as a number.
1642 * Returns -1 when calling the function fails.
1643 * Uses argv[argc] for the function arguments.
1644 */
1645 long
1646call_func_retnr(func, argc, argv, safe)
1647 char_u *func;
1648 int argc;
1649 char_u **argv;
1650 int safe; /* use the sandbox */
1651{
1652 typval_T rettv;
1653 long retval;
1654
1655 /* All arguments are passed as strings, no conversion to number. */
1656 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1657 return -1;
1658
1659 retval = get_tv_number_chk(&rettv, NULL);
1660 clear_tv(&rettv);
1661 return retval;
1662}
1663
1664#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1665 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1666
Bram Moolenaar4f688582007-07-24 12:34:30 +00001667# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001669 * Call vimL function "func" and return the result as a string.
1670 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 * Uses argv[argc] for the function arguments.
1672 */
1673 void *
1674call_func_retstr(func, argc, argv, safe)
1675 char_u *func;
1676 int argc;
1677 char_u **argv;
1678 int safe; /* use the sandbox */
1679{
1680 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001681 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001683 /* All arguments are passed as strings, no conversion to number. */
1684 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 return NULL;
1686
1687 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return retval;
1690}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001691# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001693/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001696 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 */
1698 void *
1699call_func_retlist(func, argc, argv, safe)
1700 char_u *func;
1701 int argc;
1702 char_u **argv;
1703 int safe; /* use the sandbox */
1704{
1705 typval_T rettv;
1706
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001707 /* All arguments are passed as strings, no conversion to number. */
1708 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709 return NULL;
1710
1711 if (rettv.v_type != VAR_LIST)
1712 {
1713 clear_tv(&rettv);
1714 return NULL;
1715 }
1716
1717 return rettv.vval.v_list;
1718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720
1721/*
1722 * Save the current function call pointer, and set it to NULL.
1723 * Used when executing autocommands and for ":source".
1724 */
1725 void *
1726save_funccal()
1727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 current_funccal = NULL;
1731 return (void *)fc;
1732}
1733
1734 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735restore_funccal(vfc)
1736 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001738 funccall_T *fc = (funccall_T *)vfc;
1739
1740 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741}
1742
Bram Moolenaar05159a02005-02-26 23:04:13 +00001743#if defined(FEAT_PROFILE) || defined(PROTO)
1744/*
1745 * Prepare profiling for entering a child or something else that is not
1746 * counted for the script/function itself.
1747 * Should always be called in pair with prof_child_exit().
1748 */
1749 void
1750prof_child_enter(tm)
1751 proftime_T *tm; /* place to store waittime */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 profile_start(&fc->prof_child);
1757 script_prof_save(tm);
1758}
1759
1760/*
1761 * Take care of time spent in a child.
1762 * Should always be called after prof_child_enter().
1763 */
1764 void
1765prof_child_exit(tm)
1766 proftime_T *tm; /* where waittime was stored */
1767{
1768 funccall_T *fc = current_funccal;
1769
1770 if (fc != NULL && fc->func->uf_profiling)
1771 {
1772 profile_end(&fc->prof_child);
1773 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1774 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1775 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1776 }
1777 script_prof_restore(tm);
1778}
1779#endif
1780
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_FOLDING
1783/*
1784 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1785 * it in "*cp". Doesn't give error messages.
1786 */
1787 int
1788eval_foldexpr(arg, cp)
1789 char_u *arg;
1790 int *cp;
1791{
Bram Moolenaar33570922005-01-25 22:26:29 +00001792 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 int retval;
1794 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001795 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1796 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001799 if (use_sandbox)
1800 ++sandbox;
1801 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 retval = 0;
1805 else
1806 {
1807 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 if (tv.v_type == VAR_NUMBER)
1809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 retval = 0;
1812 else
1813 {
1814 /* If the result is a string, check if there is a non-digit before
1815 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (!VIM_ISDIGIT(*s) && *s != '-')
1818 *cp = *s++;
1819 retval = atol((char *)s);
1820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001824 if (use_sandbox)
1825 --sandbox;
1826 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828 return retval;
1829}
1830#endif
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let" list all variable values
1834 * ":let var1 var2" list variable values
1835 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 * ":let var += expr" assignment command.
1837 * ":let var -= expr" assignment command.
1838 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
1841 void
1842ex_let(eap)
1843 exarg_T *eap;
1844{
1845 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 int var_count = 0;
1850 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaardb552d602006-03-23 22:59:57 +00001855 argend = skip_var_list(arg, &var_count, &semicolon);
1856 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001858 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1859 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001860 expr = skipwhite(argend);
1861 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1862 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001864 /*
1865 * ":let" without "=": list variables
1866 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 if (*arg == '[')
1868 EMSG(_(e_invarg));
1869 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 list_glob_vars(&first);
1876 list_buf_vars(&first);
1877 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001878#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001880#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 list_script_vars(&first);
1882 list_func_vars(&first);
1883 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 eap->nextcmd = check_nextcmd(arg);
1886 }
1887 else
1888 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 op[0] = '=';
1890 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001891 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001893 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1894 op[0] = *expr; /* +=, -= or .= */
1895 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 else
1898 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (eap->skip)
1901 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (eap->skip)
1904 {
1905 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 --emsg_skip;
1908 }
1909 else if (i != FAIL)
1910 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 }
1916}
1917
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918/*
1919 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1920 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * When "nextchars" is not NULL it points to a string with characters that
1922 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1923 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 * Returns OK or FAIL;
1925 */
1926 static int
1927ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1928 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 int copy; /* copy values from "tv", don't move */
1931 int semicolon; /* from skip_var_list() */
1932 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934{
1935 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 listitem_T *item;
1939 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940
1941 if (*arg != '[')
1942 {
1943 /*
1944 * ":let var = expr" or ":for var in list"
1945 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 return OK;
1949 }
1950
1951 /*
1952 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1953 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001954 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 {
1956 EMSG(_(e_listreq));
1957 return FAIL;
1958 }
1959
1960 i = list_len(l);
1961 if (semicolon == 0 && var_count < i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966 if (var_count - semicolon > i)
1967 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001968 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 }
1971
1972 item = l->lv_first;
1973 while (*arg != ']')
1974 {
1975 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 item = item->li_next;
1978 if (arg == NULL)
1979 return FAIL;
1980
1981 arg = skipwhite(arg);
1982 if (*arg == ';')
1983 {
1984 /* Put the rest of the list (may be empty) in the var after ';'.
1985 * Create a new list for this. */
1986 l = list_alloc();
1987 if (l == NULL)
1988 return FAIL;
1989 while (item != NULL)
1990 {
1991 list_append_tv(l, &item->li_tv);
1992 item = item->li_next;
1993 }
1994
1995 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001996 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 ltv.vval.v_list = l;
1998 l->lv_refcount = 1;
1999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2001 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 clear_tv(&ltv);
2003 if (arg == NULL)
2004 return FAIL;
2005 break;
2006 }
2007 else if (*arg != ',' && *arg != ']')
2008 {
2009 EMSG2(_(e_intern2), "ex_let_vars()");
2010 return FAIL;
2011 }
2012 }
2013
2014 return OK;
2015}
2016
2017/*
2018 * Skip over assignable variable "var" or list of variables "[var, var]".
2019 * Used for ":let varvar = expr" and ":for varvar in expr".
2020 * For "[var, var]" increment "*var_count" for each variable.
2021 * for "[var, var; var]" set "semicolon".
2022 * Return NULL for an error.
2023 */
2024 static char_u *
2025skip_var_list(arg, var_count, semicolon)
2026 char_u *arg;
2027 int *var_count;
2028 int *semicolon;
2029{
2030 char_u *p, *s;
2031
2032 if (*arg == '[')
2033 {
2034 /* "[var, var]": find the matching ']'. */
2035 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002036 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037 {
2038 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2039 s = skip_var_one(p);
2040 if (s == p)
2041 {
2042 EMSG2(_(e_invarg2), p);
2043 return NULL;
2044 }
2045 ++*var_count;
2046
2047 p = skipwhite(s);
2048 if (*p == ']')
2049 break;
2050 else if (*p == ';')
2051 {
2052 if (*semicolon == 1)
2053 {
2054 EMSG(_("Double ; in list of variables"));
2055 return NULL;
2056 }
2057 *semicolon = 1;
2058 }
2059 else if (*p != ',')
2060 {
2061 EMSG2(_(e_invarg2), p);
2062 return NULL;
2063 }
2064 }
2065 return p + 1;
2066 }
2067 else
2068 return skip_var_one(arg);
2069}
2070
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002072 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002073 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075 static char_u *
2076skip_var_one(arg)
2077 char_u *arg;
2078{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002079 if (*arg == '@' && arg[1] != NUL)
2080 return arg + 2;
2081 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2082 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083}
2084
Bram Moolenaara7043832005-01-21 11:56:39 +00002085/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 * List variables for hashtab "ht" with prefix "prefix".
2087 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002095{
Bram Moolenaar33570922005-01-25 22:26:29 +00002096 hashitem_T *hi;
2097 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 int todo;
2099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002100 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2102 {
2103 if (!HASHITEM_EMPTY(hi))
2104 {
2105 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 di = HI2DI(hi);
2107 if (empty || di->di_tv.v_type != VAR_STRING
2108 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110 }
2111 }
2112}
2113
2114/*
2115 * List global variables.
2116 */
2117 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118list_glob_vars(first)
2119 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002120{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List buffer variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_buf_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 char_u numbuf[NUMBUFLEN];
2132
Bram Moolenaar429fa852013-04-15 12:27:36 +02002133 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135
2136 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2138 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
2141/*
2142 * List window variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_win_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002148 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002152#ifdef FEAT_WINDOWS
2153/*
2154 * List tab page variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_tab_vars(first)
2158 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162}
2163#endif
2164
Bram Moolenaara7043832005-01-21 11:56:39 +00002165/*
2166 * List Vim variables.
2167 */
2168 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169list_vim_vars(first)
2170 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173}
2174
2175/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176 * List script-local variables, if there is a script.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_script_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2184 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
2188 * List function variables, if there is a function.
2189 */
2190 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_func_vars(first)
2192 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002193{
2194 if (current_funccal != NULL)
2195 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197}
2198
2199/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 * List variables in "arg".
2201 */
2202 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 exarg_T *eap;
2205 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207{
2208 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 char_u *name_start;
2212 char_u *arg_subsc;
2213 char_u *tofree;
2214 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215
2216 while (!ends_excmd(*arg) && !got_int)
2217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002220 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2222 {
2223 emsg_severe = TRUE;
2224 EMSG(_(e_trailing));
2225 break;
2226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* get_name_len() takes care of expanding curly braces */
2231 name_start = name = arg;
2232 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2233 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* This is mainly to keep test 49 working: when expanding
2236 * curly braces fails overrule the exception error message. */
2237 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 emsg_severe = TRUE;
2240 EMSG2(_(e_invarg2), arg);
2241 break;
2242 }
2243 error = TRUE;
2244 }
2245 else
2246 {
2247 if (tofree != NULL)
2248 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002249 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 else
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 /* handle d.key, l[idx], f(expr) */
2254 arg_subsc = arg;
2255 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 case 'g': list_glob_vars(first); break;
2264 case 'b': list_buf_vars(first); break;
2265 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002268#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 case 'v': list_vim_vars(first); break;
2270 case 's': list_script_vars(first); break;
2271 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 default:
2273 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
2276 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 {
2278 char_u numbuf[NUMBUFLEN];
2279 char_u *tf;
2280 int c;
2281 char_u *s;
2282
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002283 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 c = *arg;
2285 *arg = NUL;
2286 list_one_var_a((char_u *)"",
2287 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002288 tv.v_type,
2289 s == NULL ? (char_u *)"" : s,
2290 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 *arg = c;
2292 vim_free(tf);
2293 }
2294 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 }
2297 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298
2299 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301
2302 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
2304
2305 return arg;
2306}
2307
2308/*
2309 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2310 * Returns a pointer to the char just after the var name.
2311 * Returns NULL if there is an error.
2312 */
2313 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002318 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320{
2321 int c1;
2322 char_u *name;
2323 char_u *p;
2324 char_u *arg_end = NULL;
2325 int len;
2326 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328
2329 /*
2330 * ":let $VAR = expr": Set environment variable.
2331 */
2332 if (*arg == '$')
2333 {
2334 /* Find the end of the name. */
2335 ++arg;
2336 name = arg;
2337 len = get_env_len(&arg);
2338 if (len == 0)
2339 EMSG2(_(e_invarg2), name - 1);
2340 else
2341 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 if (op != NULL && (*op == '+' || *op == '-'))
2343 EMSG2(_(e_letwrong), op);
2344 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002347 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 {
2349 c1 = name[len];
2350 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002351 p = get_tv_string_chk(tv);
2352 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 {
2354 int mustfree = FALSE;
2355 char_u *s = vim_getenv(name, &mustfree);
2356
2357 if (s != NULL)
2358 {
2359 p = tofree = concat_str(s, p);
2360 if (mustfree)
2361 vim_free(s);
2362 }
2363 }
2364 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002367 if (STRICMP(name, "HOME") == 0)
2368 init_homedir();
2369 else if (didset_vim && STRICMP(name, "VIM") == 0)
2370 didset_vim = FALSE;
2371 else if (didset_vimruntime
2372 && STRICMP(name, "VIMRUNTIME") == 0)
2373 didset_vimruntime = FALSE;
2374 arg_end = arg;
2375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 }
2379 }
2380 }
2381
2382 /*
2383 * ":let &option = expr": Set option value.
2384 * ":let &l:option = expr": Set local option value.
2385 * ":let &g:option = expr": Set global option value.
2386 */
2387 else if (*arg == '&')
2388 {
2389 /* Find the end of the name. */
2390 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002391 if (p == NULL || (endchars != NULL
2392 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 EMSG(_(e_letunexp));
2394 else
2395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 long n;
2397 int opt_type;
2398 long numval;
2399 char_u *stringval = NULL;
2400 char_u *s;
2401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 c1 = *p;
2403 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404
2405 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 s = get_tv_string_chk(tv); /* != NULL if number or string */
2407 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 {
2409 opt_type = get_option_value(arg, &numval,
2410 &stringval, opt_flags);
2411 if ((opt_type == 1 && *op == '.')
2412 || (opt_type == 0 && *op != '.'))
2413 EMSG2(_(e_letwrong), op);
2414 else
2415 {
2416 if (opt_type == 1) /* number */
2417 {
2418 if (*op == '+')
2419 n = numval + n;
2420 else
2421 n = numval - n;
2422 }
2423 else if (opt_type == 0 && stringval != NULL) /* string */
2424 {
2425 s = concat_str(stringval, s);
2426 vim_free(stringval);
2427 stringval = s;
2428 }
2429 }
2430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 if (s != NULL)
2432 {
2433 set_option_value(arg, n, s, opt_flags);
2434 arg_end = p;
2435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
2439 }
2440
2441 /*
2442 * ":let @r = expr": Set register contents.
2443 */
2444 else if (*arg == '@')
2445 {
2446 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 if (op != NULL && (*op == '+' || *op == '-'))
2448 EMSG2(_(e_letwrong), op);
2449 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 EMSG(_(e_letunexp));
2452 else
2453 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 char_u *s;
2456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 p = get_tv_string_chk(tv);
2458 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002460 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 if (s != NULL)
2462 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(s);
2465 }
2466 }
2467 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 arg_end = arg + 1;
2471 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002472 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002480 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002482 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002484 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2488 EMSG(_(e_letunexp));
2489 else
2490 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 arg_end = p;
2493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
2497
2498 else
2499 EMSG2(_(e_invarg2), arg);
2500
2501 return arg_end;
2502}
2503
2504/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2506 */
2507 static int
2508check_changedtick(arg)
2509 char_u *arg;
2510{
2511 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2512 {
2513 EMSG2(_(e_readonlyvar), arg);
2514 return TRUE;
2515 }
2516 return FALSE;
2517}
2518
2519/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * Get an lval: variable, Dict item or List item that can be assigned a value
2521 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2522 * "name.key", "name.key[expr]" etc.
2523 * Indexing only works if "name" is an existing List or Dictionary.
2524 * "name" points to the start of the name.
2525 * If "rettv" is not NULL it points to the value to be assigned.
2526 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2527 * wrong; must end in space or cmd separator.
2528 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002529 * flags:
2530 * GLV_QUIET: do not give error messages
2531 * GLV_NO_AUTOLOAD: do not use script autoloading
2532 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002534 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 */
2537 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002538get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 typval_T *rettv;
2541 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 int unlet;
2543 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002544 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 char_u *expr_start, *expr_end;
2549 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 dictitem_T *v;
2551 typval_T var1;
2552 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562
2563 if (skip)
2564 {
2565 /* When skipping just find the end of the name. */
2566 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002567 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 }
2569
2570 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 if (expr_start != NULL)
2573 {
2574 /* Don't expand the name when we already know there is an error. */
2575 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2576 && *p != '[' && *p != '.')
2577 {
2578 EMSG(_(e_trailing));
2579 return NULL;
2580 }
2581
2582 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2583 if (lp->ll_exp_name == NULL)
2584 {
2585 /* Report an invalid expression in braces, unless the
2586 * expression evaluation has been cancelled due to an
2587 * aborting error, an interrupt, or an exception. */
2588 if (!aborting() && !quiet)
2589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002590 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 EMSG2(_(e_invarg2), name);
2592 return NULL;
2593 }
2594 }
2595 lp->ll_name = lp->ll_exp_name;
2596 }
2597 else
2598 lp->ll_name = name;
2599
2600 /* Without [idx] or .key we are done. */
2601 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2602 return p;
2603
2604 cc = *p;
2605 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002606 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (v == NULL && !quiet)
2608 EMSG2(_(e_undefvar), lp->ll_name);
2609 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 if (v == NULL)
2611 return NULL;
2612
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 /*
2614 * Loop until no more [idx] or .key is following.
2615 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002616 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2620 && !(lp->ll_tv->v_type == VAR_DICT
2621 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!quiet)
2624 EMSG(_("E689: Can only index a List or Dictionary"));
2625 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!quiet)
2630 EMSG(_("E708: [:] must come last"));
2631 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 len = -1;
2635 if (*p == '.')
2636 {
2637 key = p + 1;
2638 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2639 ;
2640 if (len == 0)
2641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_(e_emptykey));
2644 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 }
2646 p = key + len;
2647 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 else
2649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (*p == ':')
2653 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 else
2655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 empty1 = FALSE;
2657 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659 if (get_tv_string_chk(&var1) == NULL)
2660 {
2661 /* not a number or string */
2662 clear_tv(&var1);
2663 return NULL;
2664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 }
2666
2667 /* Optionally get the second index [ :expr]. */
2668 if (*p == ':')
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 if (!empty1)
2675 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (rettv != NULL && (rettv->v_type != VAR_LIST
2679 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (!quiet)
2682 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (!empty1)
2684 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
2687 p = skipwhite(p + 1);
2688 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 else
2691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 if (get_tv_string_chk(&var2) == NULL)
2700 {
2701 /* not a number or string */
2702 if (!empty1)
2703 clear_tv(&var1);
2704 clear_tv(&var2);
2705 return NULL;
2706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723
2724 /* Skip to past ']'. */
2725 ++p;
2726 }
2727
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 {
2730 if (len == -1)
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (*key == NUL)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (!quiet)
2737 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
2741 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002742 lp->ll_list = NULL;
2743 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 /* When assigning to a scope dictionary check that a function and
2747 * variable name is valid (only variable name unless it is l: or
2748 * g: dictionary). Disallow overwriting a builtin function. */
2749 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002750 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002751 int prevval;
2752 int wrong;
2753
2754 if (len != -1)
2755 {
2756 prevval = key[len];
2757 key[len] = NUL;
2758 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002759 else
2760 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2762 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002764 || !valid_varname(key);
2765 if (len != -1)
2766 key[len] = prevval;
2767 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 return NULL;
2769 }
2770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 /* Can't add "v:" variable. */
2774 if (lp->ll_dict == &vimvardict)
2775 {
2776 EMSG2(_(e_illvar), name);
2777 return NULL;
2778 }
2779
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (len == -1)
2786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 }
2789 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 if (len == -1)
2794 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 p = NULL;
2797 break;
2798 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 /* existing variable, need to check if it can be changed */
2800 else if (var_check_ro(lp->ll_di->di_flags, name))
2801 return NULL;
2802
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 }
2807 else
2808 {
2809 /*
2810 * Get the number and item for the only or first index of the List.
2811 */
2812 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 else
2815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002816 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var1);
2818 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_list = lp->ll_tv->vval.v_list;
2821 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2822 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002824 if (lp->ll_n1 < 0)
2825 {
2826 lp->ll_n1 = 0;
2827 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2828 }
2829 }
2830 if (lp->ll_li == NULL)
2831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 }
2838
2839 /*
2840 * May need to find the item or absolute index for the second
2841 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 * When no index given: "lp->ll_empty2" is TRUE.
2843 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002847 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 {
2854 if (!quiet)
2855 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 }
2860
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2862 if (lp->ll_n1 < 0)
2863 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2864 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002865 {
2866 if (!quiet)
2867 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return p;
2877}
2878
2879/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 */
2882 static void
2883clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885{
2886 vim_free(lp->ll_exp_name);
2887 vim_free(lp->ll_newkey);
2888}
2889
2890/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002891 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 */
2895 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902{
2903 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 listitem_T *ri;
2905 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906
2907 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 cc = *endp;
2912 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002913 if (op != NULL && *op != '=')
2914 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002916
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002918 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002919 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 {
2921 if (tv_op(&tv, rettv, op) == OK)
2922 set_var(lp->ll_name, &tv, FALSE);
2923 clear_tv(&tv);
2924 }
2925 }
2926 else
2927 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 else if (tv_check_lock(lp->ll_newkey == NULL
2932 ? lp->ll_tv->v_lock
2933 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2934 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 else if (lp->ll_range)
2936 {
2937 /*
2938 * Assign the List values to the list items.
2939 */
2940 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2944 else
2945 {
2946 clear_tv(&lp->ll_li->li_tv);
2947 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 ri = ri->li_next;
2950 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2951 break;
2952 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002955 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 ri = NULL;
2958 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002959 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_li = lp->ll_li->li_next;
2962 ++lp->ll_n1;
2963 }
2964 if (ri != NULL)
2965 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 else if (lp->ll_empty2
2967 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 : lp->ll_n1 != lp->ll_n2)
2969 EMSG(_("E711: List value has not enough items"));
2970 }
2971 else
2972 {
2973 /*
2974 * Assign to a List or Dictionary item.
2975 */
2976 if (lp->ll_newkey != NULL)
2977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 if (op != NULL && *op != '=')
2979 {
2980 EMSG2(_(e_letwrong), op);
2981 return;
2982 }
2983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002985 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 if (di == NULL)
2987 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002988 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2989 {
2990 vim_free(di);
2991 return;
2992 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 else if (op != NULL && *op != '=')
2996 {
2997 tv_op(lp->ll_tv, rettv, op);
2998 return;
2999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003000 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003002
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /*
3004 * Assign the value to the variable or list item.
3005 */
3006 if (copy)
3007 copy_tv(rettv, lp->ll_tv);
3008 else
3009 {
3010 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003011 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003013 }
3014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015}
3016
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003017/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3019 * Returns OK or FAIL.
3020 */
3021 static int
3022tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 typval_T *tv1;
3024 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 char_u *op;
3026{
3027 long n;
3028 char_u numbuf[NUMBUFLEN];
3029 char_u *s;
3030
3031 /* Can't do anything with a Funcref or a Dict on the right. */
3032 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3033 {
3034 switch (tv1->v_type)
3035 {
3036 case VAR_DICT:
3037 case VAR_FUNC:
3038 break;
3039
3040 case VAR_LIST:
3041 if (*op != '+' || tv2->v_type != VAR_LIST)
3042 break;
3043 /* List += List */
3044 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3045 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3046 return OK;
3047
3048 case VAR_NUMBER:
3049 case VAR_STRING:
3050 if (tv2->v_type == VAR_LIST)
3051 break;
3052 if (*op == '+' || *op == '-')
3053 {
3054 /* nr += nr or nr -= nr*/
3055 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056#ifdef FEAT_FLOAT
3057 if (tv2->v_type == VAR_FLOAT)
3058 {
3059 float_T f = n;
3060
3061 if (*op == '+')
3062 f += tv2->vval.v_float;
3063 else
3064 f -= tv2->vval.v_float;
3065 clear_tv(tv1);
3066 tv1->v_type = VAR_FLOAT;
3067 tv1->vval.v_float = f;
3068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070#endif
3071 {
3072 if (*op == '+')
3073 n += get_tv_number(tv2);
3074 else
3075 n -= get_tv_number(tv2);
3076 clear_tv(tv1);
3077 tv1->v_type = VAR_NUMBER;
3078 tv1->vval.v_number = n;
3079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 }
3081 else
3082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003083 if (tv2->v_type == VAR_FLOAT)
3084 break;
3085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 /* str .= str */
3087 s = get_tv_string(tv1);
3088 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3089 clear_tv(tv1);
3090 tv1->v_type = VAR_STRING;
3091 tv1->vval.v_string = s;
3092 }
3093 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094
3095#ifdef FEAT_FLOAT
3096 case VAR_FLOAT:
3097 {
3098 float_T f;
3099
3100 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3101 && tv2->v_type != VAR_NUMBER
3102 && tv2->v_type != VAR_STRING))
3103 break;
3104 if (tv2->v_type == VAR_FLOAT)
3105 f = tv2->vval.v_float;
3106 else
3107 f = get_tv_number(tv2);
3108 if (*op == '+')
3109 tv1->vval.v_float += f;
3110 else
3111 tv1->vval.v_float -= f;
3112 }
3113 return OK;
3114#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 }
3116 }
3117
3118 EMSG2(_(e_letwrong), op);
3119 return FAIL;
3120}
3121
3122/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123 * Add a watcher to a list.
3124 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003125 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 list_T *l;
3128 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129{
3130 lw->lw_next = l->lv_watch;
3131 l->lv_watch = lw;
3132}
3133
3134/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003135 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136 * No warning when it isn't found...
3137 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003138 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 list_T *l;
3141 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142{
Bram Moolenaar33570922005-01-25 22:26:29 +00003143 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144
3145 lwp = &l->lv_watch;
3146 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3147 {
3148 if (lw == lwrem)
3149 {
3150 *lwp = lw->lw_next;
3151 break;
3152 }
3153 lwp = &lw->lw_next;
3154 }
3155}
3156
3157/*
3158 * Just before removing an item from a list: advance watchers to the next
3159 * item.
3160 */
3161 static void
3162list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 list_T *l;
3164 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165{
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167
3168 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3169 if (lw->lw_item == item)
3170 lw->lw_item = item->li_next;
3171}
3172
3173/*
3174 * Evaluate the expression used in a ":for var in expr" command.
3175 * "arg" points to "var".
3176 * Set "*errp" to TRUE for an error, FALSE otherwise;
3177 * Return a pointer that holds the info. Null when there is an error.
3178 */
3179 void *
3180eval_for_line(arg, errp, nextcmdp, skip)
3181 char_u *arg;
3182 int *errp;
3183 char_u **nextcmdp;
3184 int skip;
3185{
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 typval_T tv;
3189 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190
3191 *errp = TRUE; /* default: there is an error */
3192
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 if (fi == NULL)
3195 return NULL;
3196
3197 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3198 if (expr == NULL)
3199 return fi;
3200
3201 expr = skipwhite(expr);
3202 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3203 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003204 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 return fi;
3206 }
3207
3208 if (skip)
3209 ++emsg_skip;
3210 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3211 {
3212 *errp = FALSE;
3213 if (!skip)
3214 {
3215 l = tv.vval.v_list;
3216 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003217 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003219 clear_tv(&tv);
3220 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 else
3222 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003223 /* No need to increment the refcount, it's already set for the
3224 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 fi->fi_list = l;
3226 list_add_watch(l, &fi->fi_lw);
3227 fi->fi_lw.lw_item = l->lv_first;
3228 }
3229 }
3230 }
3231 if (skip)
3232 --emsg_skip;
3233
3234 return fi;
3235}
3236
3237/*
3238 * Use the first item in a ":for" list. Advance to the next.
3239 * Assign the values to the variable (list). "arg" points to the first one.
3240 * Return TRUE when a valid item was found, FALSE when at end of list or
3241 * something wrong.
3242 */
3243 int
3244next_for_item(fi_void, arg)
3245 void *fi_void;
3246 char_u *arg;
3247{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003248 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251
3252 item = fi->fi_lw.lw_item;
3253 if (item == NULL)
3254 result = FALSE;
3255 else
3256 {
3257 fi->fi_lw.lw_item = item->li_next;
3258 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3259 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3260 }
3261 return result;
3262}
3263
3264/*
3265 * Free the structure used to store info used by ":for".
3266 */
3267 void
3268free_for_info(fi_void)
3269 void *fi_void;
3270{
Bram Moolenaar33570922005-01-25 22:26:29 +00003271 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003273 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 list_unref(fi->fi_list);
3277 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 vim_free(fi);
3279}
3280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3282
3283 void
3284set_context_for_expression(xp, arg, cmdidx)
3285 expand_T *xp;
3286 char_u *arg;
3287 cmdidx_T cmdidx;
3288{
3289 int got_eq = FALSE;
3290 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 if (cmdidx == CMD_let)
3294 {
3295 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003296 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 {
3298 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003299 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 {
3301 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 if (vim_iswhite(*p))
3304 break;
3305 }
3306 return;
3307 }
3308 }
3309 else
3310 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3311 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 while ((xp->xp_pattern = vim_strpbrk(arg,
3313 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3314 {
3315 c = *xp->xp_pattern;
3316 if (c == '&')
3317 {
3318 c = xp->xp_pattern[1];
3319 if (c == '&')
3320 {
3321 ++xp->xp_pattern;
3322 xp->xp_context = cmdidx != CMD_let || got_eq
3323 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3324 }
3325 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003328 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3329 xp->xp_pattern += 2;
3330
3331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333 else if (c == '$')
3334 {
3335 /* environment variable */
3336 xp->xp_context = EXPAND_ENV_VARS;
3337 }
3338 else if (c == '=')
3339 {
3340 got_eq = TRUE;
3341 xp->xp_context = EXPAND_EXPRESSION;
3342 }
3343 else if (c == '<'
3344 && xp->xp_context == EXPAND_FUNCTIONS
3345 && vim_strchr(xp->xp_pattern, '(') == NULL)
3346 {
3347 /* Function name can start with "<SNR>" */
3348 break;
3349 }
3350 else if (cmdidx != CMD_let || got_eq)
3351 {
3352 if (c == '"') /* string */
3353 {
3354 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3355 if (c == '\\' && xp->xp_pattern[1] != NUL)
3356 ++xp->xp_pattern;
3357 xp->xp_context = EXPAND_NOTHING;
3358 }
3359 else if (c == '\'') /* literal string */
3360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3363 /* skip */ ;
3364 xp->xp_context = EXPAND_NOTHING;
3365 }
3366 else if (c == '|')
3367 {
3368 if (xp->xp_pattern[1] == '|')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = EXPAND_EXPRESSION;
3372 }
3373 else
3374 xp->xp_context = EXPAND_COMMANDS;
3375 }
3376 else
3377 xp->xp_context = EXPAND_EXPRESSION;
3378 }
3379 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003380 /* Doesn't look like something valid, expand as an expression
3381 * anyway. */
3382 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 arg = xp->xp_pattern;
3384 if (*arg != NUL)
3385 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3386 /* skip */ ;
3387 }
3388 xp->xp_pattern = arg;
3389}
3390
3391#endif /* FEAT_CMDL_COMPL */
3392
3393/*
3394 * ":1,25call func(arg1, arg2)" function call.
3395 */
3396 void
3397ex_call(eap)
3398 exarg_T *eap;
3399{
3400 char_u *arg = eap->arg;
3401 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003403 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003405 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 linenr_T lnum;
3407 int doesrange;
3408 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003411 if (eap->skip)
3412 {
3413 /* trans_function_name() doesn't work well when skipping, use eval0()
3414 * instead to skip to any following command, e.g. for:
3415 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003416 ++emsg_skip;
3417 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3418 clear_tv(&rettv);
3419 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003420 return;
3421 }
3422
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003423 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003424 if (fudi.fd_newkey != NULL)
3425 {
3426 /* Still need to give an error message for missing key. */
3427 EMSG2(_(e_dictkey), fudi.fd_newkey);
3428 vim_free(fudi.fd_newkey);
3429 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003430 if (tofree == NULL)
3431 return;
3432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003433 /* Increase refcount on dictionary, it could get deleted when evaluating
3434 * the arguments. */
3435 if (fudi.fd_dict != NULL)
3436 ++fudi.fd_dict->dv_refcount;
3437
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003438 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003439 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003440 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar532c7802005-01-27 14:44:31 +00003442 /* Skip white space to allow ":call func ()". Not good, but required for
3443 * backward compatibility. */
3444 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 if (*startarg != '(')
3448 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003449 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 goto end;
3451 }
3452
3453 /*
3454 * When skipping, evaluate the function once, to find the end of the
3455 * arguments.
3456 * When the function takes a range, this is discovered after the first
3457 * call, and the loop is broken.
3458 */
3459 if (eap->skip)
3460 {
3461 ++emsg_skip;
3462 lnum = eap->line2; /* do it once, also with an invalid range */
3463 }
3464 else
3465 lnum = eap->line1;
3466 for ( ; lnum <= eap->line2; ++lnum)
3467 {
3468 if (!eap->skip && eap->addr_count > 0)
3469 {
3470 curwin->w_cursor.lnum = lnum;
3471 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003472#ifdef FEAT_VIRTUALEDIT
3473 curwin->w_cursor.coladd = 0;
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003477 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 eap->line1, eap->line2, &doesrange,
3479 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 failed = TRUE;
3482 break;
3483 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003484
3485 /* Handle a function returning a Funcref, Dictionary or List. */
3486 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3487 {
3488 failed = TRUE;
3489 break;
3490 }
3491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (doesrange || eap->skip)
3494 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003497 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003498 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003499 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (aborting())
3501 break;
3502 }
3503 if (eap->skip)
3504 --emsg_skip;
3505
3506 if (!failed)
3507 {
3508 /* Check for trailing illegal characters and a following command. */
3509 if (!ends_excmd(*arg))
3510 {
3511 emsg_severe = TRUE;
3512 EMSG(_(e_trailing));
3513 }
3514 else
3515 eap->nextcmd = check_nextcmd(arg);
3516 }
3517
3518end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003519 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003520 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521}
3522
3523/*
3524 * ":unlet[!] var1 ... " command.
3525 */
3526 void
3527ex_unlet(eap)
3528 exarg_T *eap;
3529{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003530 ex_unletlock(eap, eap->arg, 0);
3531}
3532
3533/*
3534 * ":lockvar" and ":unlockvar" commands
3535 */
3536 void
3537ex_lockvar(eap)
3538 exarg_T *eap;
3539{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003541 int deep = 2;
3542
3543 if (eap->forceit)
3544 deep = -1;
3545 else if (vim_isdigit(*arg))
3546 {
3547 deep = getdigits(&arg);
3548 arg = skipwhite(arg);
3549 }
3550
3551 ex_unletlock(eap, arg, deep);
3552}
3553
3554/*
3555 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3556 */
3557 static void
3558ex_unletlock(eap, argstart, deep)
3559 exarg_T *eap;
3560 char_u *argstart;
3561 int deep;
3562{
3563 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003566 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 do
3569 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003571 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003572 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (lv.ll_name == NULL)
3574 error = TRUE; /* error but continue parsing */
3575 if (name_end == NULL || (!vim_iswhite(*name_end)
3576 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003578 if (name_end != NULL)
3579 {
3580 emsg_severe = TRUE;
3581 EMSG(_(e_trailing));
3582 }
3583 if (!(eap->skip || error))
3584 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 break;
3586 }
3587
3588 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 {
3590 if (eap->cmdidx == CMD_unlet)
3591 {
3592 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3593 error = TRUE;
3594 }
3595 else
3596 {
3597 if (do_lock_var(&lv, name_end, deep,
3598 eap->cmdidx == CMD_lockvar) == FAIL)
3599 error = TRUE;
3600 }
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 if (!eap->skip)
3604 clear_lval(&lv);
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 arg = skipwhite(name_end);
3607 } while (!ends_excmd(*arg));
3608
3609 eap->nextcmd = check_nextcmd(arg);
3610}
3611
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003614 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 int forceit;
3617{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 int ret = OK;
3619 int cc;
3620
3621 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 cc = *name_end;
3624 *name_end = NUL;
3625
3626 /* Normal name or expanded name. */
3627 if (check_changedtick(lp->ll_name))
3628 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3634 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 else if (lp->ll_range)
3636 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003637 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638
3639 /* Delete a range of List items. */
3640 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3641 {
3642 li = lp->ll_li->li_next;
3643 listitem_remove(lp->ll_list, lp->ll_li);
3644 lp->ll_li = li;
3645 ++lp->ll_n1;
3646 }
3647 }
3648 else
3649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 /* unlet a List item. */
3652 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003655 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 }
3657
3658 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659}
3660
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661/*
3662 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 hashtab_T *ht;
3671 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003672 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003673 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar33570922005-01-25 22:26:29 +00003675 ht = find_var_ht(name, &varname);
3676 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003678 hi = hash_find(ht, varname);
3679 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003680 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003681 di = HI2DI(hi);
3682 if (var_check_fixed(di->di_flags, name)
3683 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 return FAIL;
3685 delete_var(ht, hi);
3686 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689 if (forceit)
3690 return OK;
3691 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 return FAIL;
3693}
3694
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695/*
3696 * Lock or unlock variable indicated by "lp".
3697 * "deep" is the levels to go (-1 for unlimited);
3698 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3699 */
3700 static int
3701do_lock_var(lp, name_end, deep, lock)
3702 lval_T *lp;
3703 char_u *name_end;
3704 int deep;
3705 int lock;
3706{
3707 int ret = OK;
3708 int cc;
3709 dictitem_T *di;
3710
3711 if (deep == 0) /* nothing to do */
3712 return OK;
3713
3714 if (lp->ll_tv == NULL)
3715 {
3716 cc = *name_end;
3717 *name_end = NUL;
3718
3719 /* Normal name or expanded name. */
3720 if (check_changedtick(lp->ll_name))
3721 ret = FAIL;
3722 else
3723 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003724 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 if (di == NULL)
3726 ret = FAIL;
3727 else
3728 {
3729 if (lock)
3730 di->di_flags |= DI_FLAGS_LOCK;
3731 else
3732 di->di_flags &= ~DI_FLAGS_LOCK;
3733 item_lock(&di->di_tv, deep, lock);
3734 }
3735 }
3736 *name_end = cc;
3737 }
3738 else if (lp->ll_range)
3739 {
3740 listitem_T *li = lp->ll_li;
3741
3742 /* (un)lock a range of List items. */
3743 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3744 {
3745 item_lock(&li->li_tv, deep, lock);
3746 li = li->li_next;
3747 ++lp->ll_n1;
3748 }
3749 }
3750 else if (lp->ll_list != NULL)
3751 /* (un)lock a List item. */
3752 item_lock(&lp->ll_li->li_tv, deep, lock);
3753 else
3754 /* un(lock) a Dictionary item. */
3755 item_lock(&lp->ll_di->di_tv, deep, lock);
3756
3757 return ret;
3758}
3759
3760/*
3761 * Lock or unlock an item. "deep" is nr of levels to go.
3762 */
3763 static void
3764item_lock(tv, deep, lock)
3765 typval_T *tv;
3766 int deep;
3767 int lock;
3768{
3769 static int recurse = 0;
3770 list_T *l;
3771 listitem_T *li;
3772 dict_T *d;
3773 hashitem_T *hi;
3774 int todo;
3775
3776 if (recurse >= DICT_MAXNEST)
3777 {
3778 EMSG(_("E743: variable nested too deep for (un)lock"));
3779 return;
3780 }
3781 if (deep == 0)
3782 return;
3783 ++recurse;
3784
3785 /* lock/unlock the item itself */
3786 if (lock)
3787 tv->v_lock |= VAR_LOCKED;
3788 else
3789 tv->v_lock &= ~VAR_LOCKED;
3790
3791 switch (tv->v_type)
3792 {
3793 case VAR_LIST:
3794 if ((l = tv->vval.v_list) != NULL)
3795 {
3796 if (lock)
3797 l->lv_lock |= VAR_LOCKED;
3798 else
3799 l->lv_lock &= ~VAR_LOCKED;
3800 if (deep < 0 || deep > 1)
3801 /* recursive: lock/unlock the items the List contains */
3802 for (li = l->lv_first; li != NULL; li = li->li_next)
3803 item_lock(&li->li_tv, deep - 1, lock);
3804 }
3805 break;
3806 case VAR_DICT:
3807 if ((d = tv->vval.v_dict) != NULL)
3808 {
3809 if (lock)
3810 d->dv_lock |= VAR_LOCKED;
3811 else
3812 d->dv_lock &= ~VAR_LOCKED;
3813 if (deep < 0 || deep > 1)
3814 {
3815 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003816 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003817 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3818 {
3819 if (!HASHITEM_EMPTY(hi))
3820 {
3821 --todo;
3822 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3823 }
3824 }
3825 }
3826 }
3827 }
3828 --recurse;
3829}
3830
Bram Moolenaara40058a2005-07-11 22:42:07 +00003831/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003832 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3833 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003834 */
3835 static int
3836tv_islocked(tv)
3837 typval_T *tv;
3838{
3839 return (tv->v_lock & VAR_LOCKED)
3840 || (tv->v_type == VAR_LIST
3841 && tv->vval.v_list != NULL
3842 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3843 || (tv->v_type == VAR_DICT
3844 && tv->vval.v_dict != NULL
3845 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3846}
3847
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3849/*
3850 * Delete all "menutrans_" variables.
3851 */
3852 void
3853del_menutrans_vars()
3854{
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaar33570922005-01-25 22:26:29 +00003858 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003859 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 {
3862 if (!HASHITEM_EMPTY(hi))
3863 {
3864 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3866 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003867 }
3868 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871#endif
3872
3873#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3874
3875/*
3876 * Local string buffer for the next two functions to store a variable name
3877 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3878 * get_user_var_name().
3879 */
3880
3881static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3882
3883static char_u *varnamebuf = NULL;
3884static int varnamebuflen = 0;
3885
3886/*
3887 * Function to concatenate a prefix and a variable name.
3888 */
3889 static char_u *
3890cat_prefix_varname(prefix, name)
3891 int prefix;
3892 char_u *name;
3893{
3894 int len;
3895
3896 len = (int)STRLEN(name) + 3;
3897 if (len > varnamebuflen)
3898 {
3899 vim_free(varnamebuf);
3900 len += 10; /* some additional space */
3901 varnamebuf = alloc(len);
3902 if (varnamebuf == NULL)
3903 {
3904 varnamebuflen = 0;
3905 return NULL;
3906 }
3907 varnamebuflen = len;
3908 }
3909 *varnamebuf = prefix;
3910 varnamebuf[1] = ':';
3911 STRCPY(varnamebuf + 2, name);
3912 return varnamebuf;
3913}
3914
3915/*
3916 * Function given to ExpandGeneric() to obtain the list of user defined
3917 * (global/buffer/window/built-in) variable names.
3918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 char_u *
3920get_user_var_name(xp, idx)
3921 expand_T *xp;
3922 int idx;
3923{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003924 static long_u gdone;
3925 static long_u bdone;
3926 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003927#ifdef FEAT_WINDOWS
3928 static long_u tdone;
3929#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 static int vidx;
3931 static hashitem_T *hi;
3932 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003935 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003936 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003937#ifdef FEAT_WINDOWS
3938 tdone = 0;
3939#endif
3940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941
3942 /* Global variables */
3943 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003947 else
3948 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 while (HASHITEM_EMPTY(hi))
3950 ++hi;
3951 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3952 return cat_prefix_varname('g', hi->hi_key);
3953 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955
3956 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003957 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003962 else
3963 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 while (HASHITEM_EMPTY(hi))
3965 ++hi;
3966 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003970 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 return (char_u *)"b:changedtick";
3972 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003973
3974 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003975 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003978 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003980 else
3981 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003982 while (HASHITEM_EMPTY(hi))
3983 ++hi;
3984 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003986
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987#ifdef FEAT_WINDOWS
3988 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003989 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003990 if (tdone < ht->ht_used)
3991 {
3992 if (tdone++ == 0)
3993 hi = ht->ht_array;
3994 else
3995 ++hi;
3996 while (HASHITEM_EMPTY(hi))
3997 ++hi;
3998 return cat_prefix_varname('t', hi->hi_key);
3999 }
4000#endif
4001
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 /* v: variables */
4003 if (vidx < VV_LEN)
4004 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 vim_free(varnamebuf);
4007 varnamebuf = NULL;
4008 varnamebuflen = 0;
4009 return NULL;
4010}
4011
4012#endif /* FEAT_CMDL_COMPL */
4013
4014/*
4015 * types for expressions.
4016 */
4017typedef enum
4018{
4019 TYPE_UNKNOWN = 0
4020 , TYPE_EQUAL /* == */
4021 , TYPE_NEQUAL /* != */
4022 , TYPE_GREATER /* > */
4023 , TYPE_GEQUAL /* >= */
4024 , TYPE_SMALLER /* < */
4025 , TYPE_SEQUAL /* <= */
4026 , TYPE_MATCH /* =~ */
4027 , TYPE_NOMATCH /* !~ */
4028} exptype_T;
4029
4030/*
4031 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4034 */
4035
4036/*
4037 * Handle zero level expression.
4038 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004040 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 * Return OK or FAIL.
4042 */
4043 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u **nextcmd;
4048 int evaluate;
4049{
4050 int ret;
4051 char_u *p;
4052
4053 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (ret == FAIL || !ends_excmd(*p))
4056 {
4057 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 /*
4060 * Report the invalid expression unless the expression evaluation has
4061 * been cancelled due to an aborting error, an interrupt, or an
4062 * exception.
4063 */
4064 if (!aborting())
4065 EMSG2(_(e_invexpr2), arg);
4066 ret = FAIL;
4067 }
4068 if (nextcmd != NULL)
4069 *nextcmd = check_nextcmd(p);
4070
4071 return ret;
4072}
4073
4074/*
4075 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004076 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 *
4078 * "arg" must point to the first non-white of the expression.
4079 * "arg" is advanced to the next non-white after the recognized expression.
4080 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004081 * Note: "rettv.v_lock" is not set.
4082 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * Return OK or FAIL.
4084 */
4085 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 int evaluate;
4090{
4091 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 /*
4095 * Get the first variable.
4096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099
4100 if ((*arg)[0] == '?')
4101 {
4102 result = FALSE;
4103 if (evaluate)
4104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 int error = FALSE;
4106
4107 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110 if (error)
4111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /*
4115 * Get the second variable.
4116 */
4117 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 return FAIL;
4120
4121 /*
4122 * Check for the ":".
4123 */
4124 if ((*arg)[0] != ':')
4125 {
4126 EMSG(_("E109: Missing ':' after '?'"));
4127 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return FAIL;
4130 }
4131
4132 /*
4133 * Get the third variable.
4134 */
4135 *arg = skipwhite(*arg + 1);
4136 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4137 {
4138 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 return FAIL;
4141 }
4142 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145
4146 return OK;
4147}
4148
4149/*
4150 * Handle first level expression:
4151 * expr2 || expr2 || expr2 logical OR
4152 *
4153 * "arg" must point to the first non-white of the expression.
4154 * "arg" is advanced to the next non-white after the recognized expression.
4155 *
4156 * Return OK or FAIL.
4157 */
4158 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 int evaluate;
4163{
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 long result;
4166 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 /*
4170 * Get the first variable.
4171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 return FAIL;
4174
4175 /*
4176 * Repeat until there is no following "||".
4177 */
4178 first = TRUE;
4179 result = FALSE;
4180 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4181 {
4182 if (evaluate && first)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 first = FALSE;
4190 }
4191
4192 /*
4193 * Get the second variable.
4194 */
4195 *arg = skipwhite(*arg + 2);
4196 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4197 return FAIL;
4198
4199 /*
4200 * Compute the result.
4201 */
4202 if (evaluate && !result)
4203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 if (error)
4208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 if (evaluate)
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 rettv->v_type = VAR_NUMBER;
4213 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 }
4216
4217 return OK;
4218}
4219
4220/*
4221 * Handle second level expression:
4222 * expr3 && expr3 && expr3 logical AND
4223 *
4224 * "arg" must point to the first non-white of the expression.
4225 * "arg" is advanced to the next non-white after the recognized expression.
4226 *
4227 * Return OK or FAIL.
4228 */
4229 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int evaluate;
4234{
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 long result;
4237 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /*
4241 * Get the first variable.
4242 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
4245
4246 /*
4247 * Repeat until there is no following "&&".
4248 */
4249 first = TRUE;
4250 result = TRUE;
4251 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4252 {
4253 if (evaluate && first)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first = FALSE;
4261 }
4262
4263 /*
4264 * Get the second variable.
4265 */
4266 *arg = skipwhite(*arg + 2);
4267 if (eval4(arg, &var2, evaluate && result) == FAIL)
4268 return FAIL;
4269
4270 /*
4271 * Compute the result.
4272 */
4273 if (evaluate && result)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 if (evaluate)
4282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle third level expression:
4293 * var1 == var2
4294 * var1 =~ var2
4295 * var1 != var2
4296 * var1 !~ var2
4297 * var1 > var2
4298 * var1 >= var2
4299 * var1 < var2
4300 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004301 * var1 is var2
4302 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 *
4304 * "arg" must point to the first non-white of the expression.
4305 * "arg" is advanced to the next non-white after the recognized expression.
4306 *
4307 * Return OK or FAIL.
4308 */
4309 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 int evaluate;
4314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 char_u *p;
4317 int i;
4318 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004319 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 int len = 2;
4321 long n1, n2;
4322 char_u *s1, *s2;
4323 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4324 regmatch_T regmatch;
4325 int ic;
4326 char_u *save_cpo;
4327
4328 /*
4329 * Get the first variable.
4330 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return FAIL;
4333
4334 p = *arg;
4335 switch (p[0])
4336 {
4337 case '=': if (p[1] == '=')
4338 type = TYPE_EQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_MATCH;
4341 break;
4342 case '!': if (p[1] == '=')
4343 type = TYPE_NEQUAL;
4344 else if (p[1] == '~')
4345 type = TYPE_NOMATCH;
4346 break;
4347 case '>': if (p[1] != '=')
4348 {
4349 type = TYPE_GREATER;
4350 len = 1;
4351 }
4352 else
4353 type = TYPE_GEQUAL;
4354 break;
4355 case '<': if (p[1] != '=')
4356 {
4357 type = TYPE_SMALLER;
4358 len = 1;
4359 }
4360 else
4361 type = TYPE_SEQUAL;
4362 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004363 case 'i': if (p[1] == 's')
4364 {
4365 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4366 len = 5;
4367 if (!vim_isIDc(p[len]))
4368 {
4369 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4370 type_is = TRUE;
4371 }
4372 }
4373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375
4376 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 */
4379 if (type != TYPE_UNKNOWN)
4380 {
4381 /* extra question mark appended: ignore case */
4382 if (p[len] == '?')
4383 {
4384 ic = TRUE;
4385 ++len;
4386 }
4387 /* extra '#' appended: match case */
4388 else if (p[len] == '#')
4389 {
4390 ic = FALSE;
4391 ++len;
4392 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004393 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 else
4395 ic = p_ic;
4396
4397 /*
4398 * Get the second variable.
4399 */
4400 *arg = skipwhite(p + len);
4401 if (eval5(arg, &var2, evaluate) == FAIL)
4402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FAIL;
4405 }
4406
4407 if (evaluate)
4408 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 if (type_is && rettv->v_type != var2.v_type)
4410 {
4411 /* For "is" a different type always means FALSE, for "notis"
4412 * it means TRUE. */
4413 n1 = (type == TYPE_NEQUAL);
4414 }
4415 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4416 {
4417 if (type_is)
4418 {
4419 n1 = (rettv->v_type == var2.v_type
4420 && rettv->vval.v_list == var2.vval.v_list);
4421 if (type == TYPE_NEQUAL)
4422 n1 = !n1;
4423 }
4424 else if (rettv->v_type != var2.v_type
4425 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4426 {
4427 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004428 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004429 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004430 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 clear_tv(rettv);
4432 clear_tv(&var2);
4433 return FAIL;
4434 }
4435 else
4436 {
4437 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004438 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4439 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 if (type == TYPE_NEQUAL)
4441 n1 = !n1;
4442 }
4443 }
4444
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004445 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4446 {
4447 if (type_is)
4448 {
4449 n1 = (rettv->v_type == var2.v_type
4450 && rettv->vval.v_dict == var2.vval.v_dict);
4451 if (type == TYPE_NEQUAL)
4452 n1 = !n1;
4453 }
4454 else if (rettv->v_type != var2.v_type
4455 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4456 {
4457 if (rettv->v_type != var2.v_type)
4458 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4459 else
4460 EMSG(_("E736: Invalid operation for Dictionary"));
4461 clear_tv(rettv);
4462 clear_tv(&var2);
4463 return FAIL;
4464 }
4465 else
4466 {
4467 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004468 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4469 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004470 if (type == TYPE_NEQUAL)
4471 n1 = !n1;
4472 }
4473 }
4474
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4476 {
4477 if (rettv->v_type != var2.v_type
4478 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4479 {
4480 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004481 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004482 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004483 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004484 clear_tv(rettv);
4485 clear_tv(&var2);
4486 return FAIL;
4487 }
4488 else
4489 {
4490 /* Compare two Funcrefs for being equal or unequal. */
4491 if (rettv->vval.v_string == NULL
4492 || var2.vval.v_string == NULL)
4493 n1 = FALSE;
4494 else
4495 n1 = STRCMP(rettv->vval.v_string,
4496 var2.vval.v_string) == 0;
4497 if (type == TYPE_NEQUAL)
4498 n1 = !n1;
4499 }
4500 }
4501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004502#ifdef FEAT_FLOAT
4503 /*
4504 * If one of the two variables is a float, compare as a float.
4505 * When using "=~" or "!~", always compare as string.
4506 */
4507 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4508 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4509 {
4510 float_T f1, f2;
4511
4512 if (rettv->v_type == VAR_FLOAT)
4513 f1 = rettv->vval.v_float;
4514 else
4515 f1 = get_tv_number(rettv);
4516 if (var2.v_type == VAR_FLOAT)
4517 f2 = var2.vval.v_float;
4518 else
4519 f2 = get_tv_number(&var2);
4520 n1 = FALSE;
4521 switch (type)
4522 {
4523 case TYPE_EQUAL: n1 = (f1 == f2); break;
4524 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4525 case TYPE_GREATER: n1 = (f1 > f2); break;
4526 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4527 case TYPE_SMALLER: n1 = (f1 < f2); break;
4528 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4529 case TYPE_UNKNOWN:
4530 case TYPE_MATCH:
4531 case TYPE_NOMATCH: break; /* avoid gcc warning */
4532 }
4533 }
4534#endif
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 /*
4537 * If one of the two variables is a number, compare as a number.
4538 * When using "=~" or "!~", always compare as string.
4539 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 n1 = get_tv_number(rettv);
4544 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 switch (type)
4546 {
4547 case TYPE_EQUAL: n1 = (n1 == n2); break;
4548 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4549 case TYPE_GREATER: n1 = (n1 > n2); break;
4550 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4551 case TYPE_SMALLER: n1 = (n1 < n2); break;
4552 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4553 case TYPE_UNKNOWN:
4554 case TYPE_MATCH:
4555 case TYPE_NOMATCH: break; /* avoid gcc warning */
4556 }
4557 }
4558 else
4559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 s1 = get_tv_string_buf(rettv, buf1);
4561 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4563 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4564 else
4565 i = 0;
4566 n1 = FALSE;
4567 switch (type)
4568 {
4569 case TYPE_EQUAL: n1 = (i == 0); break;
4570 case TYPE_NEQUAL: n1 = (i != 0); break;
4571 case TYPE_GREATER: n1 = (i > 0); break;
4572 case TYPE_GEQUAL: n1 = (i >= 0); break;
4573 case TYPE_SMALLER: n1 = (i < 0); break;
4574 case TYPE_SEQUAL: n1 = (i <= 0); break;
4575
4576 case TYPE_MATCH:
4577 case TYPE_NOMATCH:
4578 /* avoid 'l' flag in 'cpoptions' */
4579 save_cpo = p_cpo;
4580 p_cpo = (char_u *)"";
4581 regmatch.regprog = vim_regcomp(s2,
4582 RE_MAGIC + RE_STRING);
4583 regmatch.rm_ic = ic;
4584 if (regmatch.regprog != NULL)
4585 {
4586 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004587 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (type == TYPE_NOMATCH)
4589 n1 = !n1;
4590 }
4591 p_cpo = save_cpo;
4592 break;
4593
4594 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4595 }
4596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 clear_tv(rettv);
4598 clear_tv(&var2);
4599 rettv->v_type = VAR_NUMBER;
4600 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 }
4603
4604 return OK;
4605}
4606
4607/*
4608 * Handle fourth level expression:
4609 * + number addition
4610 * - number subtraction
4611 * . string concatenation
4612 *
4613 * "arg" must point to the first non-white of the expression.
4614 * "arg" is advanced to the next non-white after the recognized expression.
4615 *
4616 * Return OK or FAIL.
4617 */
4618 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 int evaluate;
4623{
Bram Moolenaar33570922005-01-25 22:26:29 +00004624 typval_T var2;
4625 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int op;
4627 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628#ifdef FEAT_FLOAT
4629 float_T f1 = 0, f2 = 0;
4630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 char_u *s1, *s2;
4632 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4633 char_u *p;
4634
4635 /*
4636 * Get the first variable.
4637 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004638 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 return FAIL;
4640
4641 /*
4642 * Repeat computing, until no '+', '-' or '.' is following.
4643 */
4644 for (;;)
4645 {
4646 op = **arg;
4647 if (op != '+' && op != '-' && op != '.')
4648 break;
4649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004650 if ((op != '+' || rettv->v_type != VAR_LIST)
4651#ifdef FEAT_FLOAT
4652 && (op == '.' || rettv->v_type != VAR_FLOAT)
4653#endif
4654 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004655 {
4656 /* For "list + ...", an illegal use of the first operand as
4657 * a number cannot be determined before evaluating the 2nd
4658 * operand: if this is also a list, all is ok.
4659 * For "something . ...", "something - ..." or "non-list + ...",
4660 * we know that the first operand needs to be a string or number
4661 * without evaluating the 2nd operand. So check before to avoid
4662 * side effects after an error. */
4663 if (evaluate && get_tv_string_chk(rettv) == NULL)
4664 {
4665 clear_tv(rettv);
4666 return FAIL;
4667 }
4668 }
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 /*
4671 * Get the second variable.
4672 */
4673 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004674 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return FAIL;
4678 }
4679
4680 if (evaluate)
4681 {
4682 /*
4683 * Compute the result.
4684 */
4685 if (op == '.')
4686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4688 s2 = get_tv_string_buf_chk(&var2, buf2);
4689 if (s2 == NULL) /* type error ? */
4690 {
4691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004695 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 clear_tv(rettv);
4697 rettv->v_type = VAR_STRING;
4698 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004700 else if (op == '+' && rettv->v_type == VAR_LIST
4701 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004702 {
4703 /* concatenate Lists */
4704 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4705 &var3) == FAIL)
4706 {
4707 clear_tv(rettv);
4708 clear_tv(&var2);
4709 return FAIL;
4710 }
4711 clear_tv(rettv);
4712 *rettv = var3;
4713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 else
4715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 int error = FALSE;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718#ifdef FEAT_FLOAT
4719 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721 f1 = rettv->vval.v_float;
4722 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 else
4725#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727 n1 = get_tv_number_chk(rettv, &error);
4728 if (error)
4729 {
4730 /* This can only happen for "list + non-list". For
4731 * "non-list + ..." or "something - ...", we returned
4732 * before evaluating the 2nd operand. */
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 f1 = n1;
4739#endif
4740 }
4741#ifdef FEAT_FLOAT
4742 if (var2.v_type == VAR_FLOAT)
4743 {
4744 f2 = var2.vval.v_float;
4745 n2 = 0;
4746 }
4747 else
4748#endif
4749 {
4750 n2 = get_tv_number_chk(&var2, &error);
4751 if (error)
4752 {
4753 clear_tv(rettv);
4754 clear_tv(&var2);
4755 return FAIL;
4756 }
4757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
4759 f2 = n2;
4760#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763
4764#ifdef FEAT_FLOAT
4765 /* If there is a float on either side the result is a float. */
4766 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4767 {
4768 if (op == '+')
4769 f1 = f1 + f2;
4770 else
4771 f1 = f1 - f2;
4772 rettv->v_type = VAR_FLOAT;
4773 rettv->vval.v_float = f1;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776#endif
4777 {
4778 if (op == '+')
4779 n1 = n1 + n2;
4780 else
4781 n1 = n1 - n2;
4782 rettv->v_type = VAR_NUMBER;
4783 rettv->vval.v_number = n1;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 }
4789 return OK;
4790}
4791
4792/*
4793 * Handle fifth level expression:
4794 * * number multiplication
4795 * / number division
4796 * % number modulo
4797 *
4798 * "arg" must point to the first non-white of the expression.
4799 * "arg" is advanced to the next non-white after the recognized expression.
4800 *
4801 * Return OK or FAIL.
4802 */
4803 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004804eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int op;
4812 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813#ifdef FEAT_FLOAT
4814 int use_float = FALSE;
4815 float_T f1 = 0, f2;
4816#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819 /*
4820 * Get the first variable.
4821 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004822 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FAIL;
4824
4825 /*
4826 * Repeat computing, until no '*', '/' or '%' is following.
4827 */
4828 for (;;)
4829 {
4830 op = **arg;
4831 if (op != '*' && op != '/' && op != '%')
4832 break;
4833
4834 if (evaluate)
4835 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
4838 {
4839 f1 = rettv->vval.v_float;
4840 use_float = TRUE;
4841 n1 = 0;
4842 }
4843 else
4844#endif
4845 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004847 if (error)
4848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 else
4851 n1 = 0;
4852
4853 /*
4854 * Get the second variable.
4855 */
4856 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004857 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return FAIL;
4859
4860 if (evaluate)
4861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862#ifdef FEAT_FLOAT
4863 if (var2.v_type == VAR_FLOAT)
4864 {
4865 if (!use_float)
4866 {
4867 f1 = n1;
4868 use_float = TRUE;
4869 }
4870 f2 = var2.vval.v_float;
4871 n2 = 0;
4872 }
4873 else
4874#endif
4875 {
4876 n2 = get_tv_number_chk(&var2, &error);
4877 clear_tv(&var2);
4878 if (error)
4879 return FAIL;
4880#ifdef FEAT_FLOAT
4881 if (use_float)
4882 f2 = n2;
4883#endif
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 if (op == '*')
4894 f1 = f1 * f2;
4895 else if (op == '/')
4896 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897# ifdef VMS
4898 /* VMS crashes on divide by zero, work around it */
4899 if (f2 == 0.0)
4900 {
4901 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004902 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004903 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004904 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004905 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 }
4908 else
4909 f1 = f1 / f2;
4910# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 /* We rely on the floating point library to handle divide
4912 * by zero to result in "inf" and not a crash. */
4913 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004914# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004918 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 return FAIL;
4920 }
4921 rettv->v_type = VAR_FLOAT;
4922 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 if (op == '*')
4928 n1 = n1 * n2;
4929 else if (op == '/')
4930 {
4931 if (n2 == 0) /* give an error message? */
4932 {
4933 if (n1 == 0)
4934 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4935 else if (n1 < 0)
4936 n1 = -0x7fffffffL;
4937 else
4938 n1 = 0x7fffffffL;
4939 }
4940 else
4941 n1 = n1 / n2;
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004944 {
4945 if (n2 == 0) /* give an error message? */
4946 n1 = 0;
4947 else
4948 n1 = n1 % n2;
4949 }
4950 rettv->v_type = VAR_NUMBER;
4951 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 }
4955
4956 return OK;
4957}
4958
4959/*
4960 * Handle sixth level expression:
4961 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004962 * "string" string constant
4963 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * &option-name option value
4965 * @r register contents
4966 * identifier variable value
4967 * function() function call
4968 * $VAR environment variable
4969 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004970 * [expr, expr] List
4971 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 *
4973 * Also handle:
4974 * ! in front logical NOT
4975 * - in front unary minus
4976 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 * trailing [] subscript in String or List
4978 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 *
4980 * "arg" must point to the first non-white of the expression.
4981 * "arg" is advanced to the next non-white after the recognized expression.
4982 *
4983 * Return OK or FAIL.
4984 */
4985 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004986eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004990 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 long n;
4993 int len;
4994 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 char_u *start_leader, *end_leader;
4996 int ret = OK;
4997 char_u *alias;
4998
4999 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005000 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005001 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 /*
5006 * Skip '!' and '-' characters. They are handled later.
5007 */
5008 start_leader = *arg;
5009 while (**arg == '!' || **arg == '-' || **arg == '+')
5010 *arg = skipwhite(*arg + 1);
5011 end_leader = *arg;
5012
5013 switch (**arg)
5014 {
5015 /*
5016 * Number constant.
5017 */
5018 case '0':
5019 case '1':
5020 case '2':
5021 case '3':
5022 case '4':
5023 case '5':
5024 case '6':
5025 case '7':
5026 case '8':
5027 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029#ifdef FEAT_FLOAT
5030 char_u *p = skipdigits(*arg + 1);
5031 int get_float = FALSE;
5032
5033 /* We accept a float when the format matches
5034 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005035 * strict to avoid backwards compatibility problems.
5036 * Don't look for a float after the "." operator, so that
5037 * ":let vers = 1.2.3" doesn't fail. */
5038 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005040 get_float = TRUE;
5041 p = skipdigits(p + 2);
5042 if (*p == 'e' || *p == 'E')
5043 {
5044 ++p;
5045 if (*p == '-' || *p == '+')
5046 ++p;
5047 if (!vim_isdigit(*p))
5048 get_float = FALSE;
5049 else
5050 p = skipdigits(p + 1);
5051 }
5052 if (ASCII_ISALPHA(*p) || *p == '.')
5053 get_float = FALSE;
5054 }
5055 if (get_float)
5056 {
5057 float_T f;
5058
5059 *arg += string2float(*arg, &f);
5060 if (evaluate)
5061 {
5062 rettv->v_type = VAR_FLOAT;
5063 rettv->vval.v_float = f;
5064 }
5065 }
5066 else
5067#endif
5068 {
5069 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5070 *arg += len;
5071 if (evaluate)
5072 {
5073 rettv->v_type = VAR_NUMBER;
5074 rettv->vval.v_number = n;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 /*
5081 * String constant: "string".
5082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085
5086 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090 break;
5091
5092 /*
5093 * List: [expr, expr]
5094 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097
5098 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 * Dictionary: {key: val, key: val}
5100 */
5101 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5102 break;
5103
5104 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005105 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005107 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /*
5111 * Environment variable: $VAR.
5112 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005113 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 break;
5115
5116 /*
5117 * Register contents: @r.
5118 */
5119 case '@': ++*arg;
5120 if (evaluate)
5121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005123 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 if (**arg != NUL)
5126 ++*arg;
5127 break;
5128
5129 /*
5130 * nested expression: (expression).
5131 */
5132 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (**arg == ')')
5135 ++*arg;
5136 else if (ret == OK)
5137 {
5138 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 ret = FAIL;
5141 }
5142 break;
5143
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147
5148 if (ret == NOTDONE)
5149 {
5150 /*
5151 * Must be a variable or function name.
5152 * Can also be a curly-braces kind of name: {expr}.
5153 */
5154 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005155 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 if (alias != NULL)
5157 s = alias;
5158
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005159 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 ret = FAIL;
5161 else
5162 {
5163 if (**arg == '(') /* recursive! */
5164 {
5165 /* If "s" is the name of a variable of type VAR_FUNC
5166 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005167 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168
5169 /* Invoke the function. */
5170 ret = get_func_tv(s, len, rettv, arg,
5171 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005173
5174 /* If evaluate is FALSE rettv->v_type was not set in
5175 * get_func_tv, but it's needed in handle_subscript() to parse
5176 * what follows. So set it here. */
5177 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5178 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005179 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005180 rettv->v_type = VAR_FUNC;
5181 }
5182
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 /* Stop the expression evaluation when immediately
5184 * aborting on error, or when an interrupt occurred or
5185 * an exception was thrown but not caught. */
5186 if (aborting())
5187 {
5188 if (ret == OK)
5189 clear_tv(rettv);
5190 ret = FAIL;
5191 }
5192 }
5193 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005194 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005195 else
5196 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005198 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 }
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *arg = skipwhite(*arg);
5202
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005203 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5204 * expr(expr). */
5205 if (ret == OK)
5206 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208 /*
5209 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5210 */
5211 if (ret == OK && evaluate && end_leader > start_leader)
5212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214 int val = 0;
5215#ifdef FEAT_FLOAT
5216 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 if (rettv->v_type == VAR_FLOAT)
5219 f = rettv->vval.v_float;
5220 else
5221#endif
5222 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 clear_tv(rettv);
5226 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 else
5229 {
5230 while (end_leader > start_leader)
5231 {
5232 --end_leader;
5233 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005234 {
5235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 f = !f;
5238 else
5239#endif
5240 val = !val;
5241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 {
5244#ifdef FEAT_FLOAT
5245 if (rettv->v_type == VAR_FLOAT)
5246 f = -f;
5247 else
5248#endif
5249 val = -val;
5250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005252#ifdef FEAT_FLOAT
5253 if (rettv->v_type == VAR_FLOAT)
5254 {
5255 clear_tv(rettv);
5256 rettv->vval.v_float = f;
5257 }
5258 else
5259#endif
5260 {
5261 clear_tv(rettv);
5262 rettv->v_type = VAR_NUMBER;
5263 rettv->vval.v_number = val;
5264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267
5268 return ret;
5269}
5270
5271/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005272 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5273 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5275 */
5276 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282{
5283 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 long len = -1;
5287 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005291 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005293 if (verbose)
5294 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 return FAIL;
5296 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005297#ifdef FEAT_FLOAT
5298 else if (rettv->v_type == VAR_FLOAT)
5299 {
5300 if (verbose)
5301 EMSG(_(e_float_as_string));
5302 return FAIL;
5303 }
5304#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 /*
5309 * dict.name
5310 */
5311 key = *arg + 1;
5312 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5313 ;
5314 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005316 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 /*
5321 * something[idx]
5322 *
5323 * Get the (first) variable from inside the [].
5324 */
5325 *arg = skipwhite(*arg + 1);
5326 if (**arg == ':')
5327 empty1 = TRUE;
5328 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5329 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5331 {
5332 /* not a number or string */
5333 clear_tv(&var1);
5334 return FAIL;
5335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336
5337 /*
5338 * Get the second variable from inside the [:].
5339 */
5340 if (**arg == ':')
5341 {
5342 range = TRUE;
5343 *arg = skipwhite(*arg + 1);
5344 if (**arg == ']')
5345 empty2 = TRUE;
5346 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005348 if (!empty1)
5349 clear_tv(&var1);
5350 return FAIL;
5351 }
5352 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5353 {
5354 /* not a number or string */
5355 if (!empty1)
5356 clear_tv(&var1);
5357 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 return FAIL;
5359 }
5360 }
5361
5362 /* Check for the ']'. */
5363 if (**arg != ']')
5364 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005365 if (verbose)
5366 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 clear_tv(&var1);
5368 if (range)
5369 clear_tv(&var2);
5370 return FAIL;
5371 }
5372 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 }
5374
5375 if (evaluate)
5376 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 n1 = 0;
5378 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 n1 = get_tv_number(&var1);
5381 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 }
5383 if (range)
5384 {
5385 if (empty2)
5386 n2 = -1;
5387 else
5388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 n2 = get_tv_number(&var2);
5390 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 }
5392 }
5393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
5396 case VAR_NUMBER:
5397 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005398 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 len = (long)STRLEN(s);
5400 if (range)
5401 {
5402 /* The resulting variable is a substring. If the indexes
5403 * are out of range the result is empty. */
5404 if (n1 < 0)
5405 {
5406 n1 = len + n1;
5407 if (n1 < 0)
5408 n1 = 0;
5409 }
5410 if (n2 < 0)
5411 n2 = len + n2;
5412 else if (n2 >= len)
5413 n2 = len;
5414 if (n1 >= len || n2 < 0 || n1 > n2)
5415 s = NULL;
5416 else
5417 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5418 }
5419 else
5420 {
5421 /* The resulting variable is a string of a single
5422 * character. If the index is too big or negative the
5423 * result is empty. */
5424 if (n1 >= len || n1 < 0)
5425 s = NULL;
5426 else
5427 s = vim_strnsave(s + n1, 1);
5428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 clear_tv(rettv);
5430 rettv->v_type = VAR_STRING;
5431 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 break;
5433
5434 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 if (n1 < 0)
5437 n1 = len + n1;
5438 if (!empty1 && (n1 < 0 || n1 >= len))
5439 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005440 /* For a range we allow invalid values and return an empty
5441 * list. A list index out of range is an error. */
5442 if (!range)
5443 {
5444 if (verbose)
5445 EMSGN(_(e_listidx), n1);
5446 return FAIL;
5447 }
5448 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
5450 if (range)
5451 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 list_T *l;
5453 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454
5455 if (n2 < 0)
5456 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005457 else if (n2 >= len)
5458 n2 = len - 1;
5459 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005460 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 l = list_alloc();
5462 if (l == NULL)
5463 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 n1 <= n2; ++n1)
5466 {
5467 if (list_append_tv(l, &item->li_tv) == FAIL)
5468 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005469 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 return FAIL;
5471 }
5472 item = item->li_next;
5473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 clear_tv(rettv);
5475 rettv->v_type = VAR_LIST;
5476 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005477 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 else
5480 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005481 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 clear_tv(rettv);
5483 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486
5487 case VAR_DICT:
5488 if (range)
5489 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005490 if (verbose)
5491 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 if (len == -1)
5493 clear_tv(&var1);
5494 return FAIL;
5495 }
5496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
5499 if (len == -1)
5500 {
5501 key = get_tv_string(&var1);
5502 if (*key == NUL)
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 return FAIL;
5508 }
5509 }
5510
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005511 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005513 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005514 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 if (len == -1)
5516 clear_tv(&var1);
5517 if (item == NULL)
5518 return FAIL;
5519
5520 copy_tv(&item->di_tv, &var1);
5521 clear_tv(rettv);
5522 *rettv = var1;
5523 }
5524 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 }
5526 }
5527
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 return OK;
5529}
5530
5531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * Get an option value.
5533 * "arg" points to the '&' or '+' before the option name.
5534 * "arg" is advanced to character after the option name.
5535 * Return OK or FAIL.
5536 */
5537 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 int evaluate;
5542{
5543 char_u *option_end;
5544 long numval;
5545 char_u *stringval;
5546 int opt_type;
5547 int c;
5548 int working = (**arg == '+'); /* has("+option") */
5549 int ret = OK;
5550 int opt_flags;
5551
5552 /*
5553 * Isolate the option name and find its value.
5554 */
5555 option_end = find_option_end(arg, &opt_flags);
5556 if (option_end == NULL)
5557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 EMSG2(_("E112: Option name missing: %s"), *arg);
5560 return FAIL;
5561 }
5562
5563 if (!evaluate)
5564 {
5565 *arg = option_end;
5566 return OK;
5567 }
5568
5569 c = *option_end;
5570 *option_end = NUL;
5571 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 if (opt_type == -3) /* invalid name */
5575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 EMSG2(_("E113: Unknown option: %s"), *arg);
5578 ret = FAIL;
5579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 if (opt_type == -2) /* hidden string option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_STRING;
5585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == -1) /* hidden number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == 1) /* number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else /* string option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_STRING;
5600 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 }
5603 else if (working && (opt_type == -2 || opt_type == -1))
5604 ret = FAIL;
5605
5606 *option_end = c; /* put back for error messages */
5607 *arg = option_end;
5608
5609 return ret;
5610}
5611
5612/*
5613 * Allocate a variable for a string constant.
5614 * Return OK or FAIL.
5615 */
5616 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 int evaluate;
5621{
5622 char_u *p;
5623 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 int extra = 0;
5625
5626 /*
5627 * Find the end of the string, skipping backslashed characters.
5628 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005629 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 if (*p == '\\' && p[1] != NUL)
5632 {
5633 ++p;
5634 /* A "\<x>" form occupies at least 4 characters, and produces up
5635 * to 6 characters: reserve space for 2 extra */
5636 if (*p == '<')
5637 extra += 2;
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
5640
5641 if (*p != '"')
5642 {
5643 EMSG2(_("E114: Missing quote: %s"), *arg);
5644 return FAIL;
5645 }
5646
5647 /* If only parsing, set *arg and return here */
5648 if (!evaluate)
5649 {
5650 *arg = p + 1;
5651 return OK;
5652 }
5653
5654 /*
5655 * Copy the string into allocated memory, handling backslashed
5656 * characters.
5657 */
5658 name = alloc((unsigned)(p - *arg + extra));
5659 if (name == NULL)
5660 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 rettv->v_type = VAR_STRING;
5662 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 if (*p == '\\')
5667 {
5668 switch (*++p)
5669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 case 'b': *name++ = BS; ++p; break;
5671 case 'e': *name++ = ESC; ++p; break;
5672 case 'f': *name++ = FF; ++p; break;
5673 case 'n': *name++ = NL; ++p; break;
5674 case 'r': *name++ = CAR; ++p; break;
5675 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 case 'X': /* hex: "\x1", "\x12" */
5678 case 'x':
5679 case 'u': /* Unicode: "\u0023" */
5680 case 'U':
5681 if (vim_isxdigit(p[1]))
5682 {
5683 int n, nr;
5684 int c = toupper(*p);
5685
5686 if (c == 'X')
5687 n = 2;
5688 else
5689 n = 4;
5690 nr = 0;
5691 while (--n >= 0 && vim_isxdigit(p[1]))
5692 {
5693 ++p;
5694 nr = (nr << 4) + hex2nr(*p);
5695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697#ifdef FEAT_MBYTE
5698 /* For "\u" store the number according to
5699 * 'encoding'. */
5700 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else
5703#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 break;
5707
5708 /* octal: "\1", "\12", "\123" */
5709 case '0':
5710 case '1':
5711 case '2':
5712 case '3':
5713 case '4':
5714 case '5':
5715 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 case '7': *name = *p++ - '0';
5717 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 *name = (*name << 3) + *p++ - '0';
5720 if (*p >= '0' && *p <= '7')
5721 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 break;
5725
5726 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (extra != 0)
5729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 /* FALLTHROUGH */
5734
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 }
5739 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 *arg = p + 1;
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 return OK;
5747}
5748
5749/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 * Return OK or FAIL.
5752 */
5753 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 int evaluate;
5758{
5759 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 int reduce = 0;
5762
5763 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 */
5766 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 break;
5772 ++reduce;
5773 ++p;
5774 }
5775 }
5776
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 return FAIL;
5781 }
5782
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 if (!evaluate)
5785 {
5786 *arg = p + 1;
5787 return OK;
5788 }
5789
5790 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 */
5793 str = alloc((unsigned)((p - *arg) - reduce));
5794 if (str == NULL)
5795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 rettv->v_type = VAR_STRING;
5797 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 break;
5805 ++p;
5806 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 *arg = p + 1;
5811
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 return OK;
5813}
5814
5815/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 * Allocate a variable for a List and fill it from "*arg".
5817 * Return OK or FAIL.
5818 */
5819 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005820get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005822 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 int evaluate;
5824{
Bram Moolenaar33570922005-01-25 22:26:29 +00005825 list_T *l = NULL;
5826 typval_T tv;
5827 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828
5829 if (evaluate)
5830 {
5831 l = list_alloc();
5832 if (l == NULL)
5833 return FAIL;
5834 }
5835
5836 *arg = skipwhite(*arg + 1);
5837 while (**arg != ']' && **arg != NUL)
5838 {
5839 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5840 goto failret;
5841 if (evaluate)
5842 {
5843 item = listitem_alloc();
5844 if (item != NULL)
5845 {
5846 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005847 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848 list_append(l, item);
5849 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005850 else
5851 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 }
5853
5854 if (**arg == ']')
5855 break;
5856 if (**arg != ',')
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 goto failret;
5860 }
5861 *arg = skipwhite(*arg + 1);
5862 }
5863
5864 if (**arg != ']')
5865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867failret:
5868 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005869 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 return FAIL;
5871 }
5872
5873 *arg = skipwhite(*arg + 1);
5874 if (evaluate)
5875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005876 rettv->v_type = VAR_LIST;
5877 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 ++l->lv_refcount;
5879 }
5880
5881 return OK;
5882}
5883
5884/*
5885 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005886 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005888 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889list_alloc()
5890{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 list_T *l;
5892
5893 l = (list_T *)alloc_clear(sizeof(list_T));
5894 if (l != NULL)
5895 {
5896 /* Prepend the list to the list of lists for garbage collection. */
5897 if (first_list != NULL)
5898 first_list->lv_used_prev = l;
5899 l->lv_used_prev = NULL;
5900 l->lv_used_next = first_list;
5901 first_list = l;
5902 }
5903 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904}
5905
5906/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005907 * Allocate an empty list for a return value.
5908 * Returns OK or FAIL.
5909 */
5910 static int
5911rettv_list_alloc(rettv)
5912 typval_T *rettv;
5913{
5914 list_T *l = list_alloc();
5915
5916 if (l == NULL)
5917 return FAIL;
5918
5919 rettv->vval.v_list = l;
5920 rettv->v_type = VAR_LIST;
5921 ++l->lv_refcount;
5922 return OK;
5923}
5924
5925/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 * Unreference a list: decrement the reference count and free it when it
5927 * becomes zero.
5928 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005929 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005933 if (l != NULL && --l->lv_refcount <= 0)
5934 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935}
5936
5937/*
5938 * Free a list, including all items it points to.
5939 * Ignores the reference count.
5940 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005941 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005942list_free(l, recurse)
5943 list_T *l;
5944 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945{
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005948 /* Remove the list from the list of lists for garbage collection. */
5949 if (l->lv_used_prev == NULL)
5950 first_list = l->lv_used_next;
5951 else
5952 l->lv_used_prev->lv_used_next = l->lv_used_next;
5953 if (l->lv_used_next != NULL)
5954 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5955
Bram Moolenaard9fba312005-06-26 22:34:35 +00005956 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005958 /* Remove the item before deleting it. */
5959 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005960 if (recurse || (item->li_tv.v_type != VAR_LIST
5961 && item->li_tv.v_type != VAR_DICT))
5962 clear_tv(&item->li_tv);
5963 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 }
5965 vim_free(l);
5966}
5967
5968/*
5969 * Allocate a list item.
5970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005971 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972listitem_alloc()
5973{
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975}
5976
5977/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005978 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005980 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005984 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 vim_free(item);
5986}
5987
5988/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005989 * Remove a list item from a List and free it. Also clears the value.
5990 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005991 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005992listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l;
5994 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995{
5996 list_remove(l, item, item);
5997 listitem_free(item);
5998}
5999
6000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 * Get the number of items in a list.
6002 */
6003 static long
6004list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 if (l == NULL)
6008 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006009 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010}
6011
6012/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 * Return TRUE when two lists have exactly the same values.
6014 */
6015 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006016list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 list_T *l1;
6018 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006019 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006020 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021{
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006023
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006024 if (l1 == NULL || l2 == NULL)
6025 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006026 if (l1 == l2)
6027 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006028 if (list_len(l1) != list_len(l2))
6029 return FALSE;
6030
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006031 for (item1 = l1->lv_first, item2 = l2->lv_first;
6032 item1 != NULL && item2 != NULL;
6033 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006034 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035 return FALSE;
6036 return item1 == NULL && item2 == NULL;
6037}
6038
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006039#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6040 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006041/*
6042 * Return the dictitem that an entry in a hashtable points to.
6043 */
6044 dictitem_T *
6045dict_lookup(hi)
6046 hashitem_T *hi;
6047{
6048 return HI2DI(hi);
6049}
6050#endif
6051
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006052/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 * Return TRUE when two dictionaries have exactly the same key/values.
6054 */
6055 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 dict_T *d1;
6058 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061{
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 hashitem_T *hi;
6063 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006064 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006066 if (d1 == NULL || d2 == NULL)
6067 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006068 if (d1 == d2)
6069 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 if (dict_len(d1) != dict_len(d2))
6071 return FALSE;
6072
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006073 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 if (!HASHITEM_EMPTY(hi))
6077 {
6078 item2 = dict_find(d2, hi->hi_key, -1);
6079 if (item2 == NULL)
6080 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 return FALSE;
6083 --todo;
6084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006085 }
6086 return TRUE;
6087}
6088
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089static int tv_equal_recurse_limit;
6090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006094 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 */
6096 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 typval_T *tv1;
6099 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 int ic; /* ignore case */
6101 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102{
6103 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006104 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006106 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006109 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006111 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 * recursiveness to a limit. We guess they are equal then.
6113 * A fixed limit has the problem of still taking an awful long time.
6114 * Reduce the limit every time running into it. That should work fine for
6115 * deeply linked structures that are not recursively linked and catch
6116 * recursiveness quickly. */
6117 if (!recursive)
6118 tv_equal_recurse_limit = 1000;
6119 if (recursive_cnt >= tv_equal_recurse_limit)
6120 {
6121 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006122 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006124
6125 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 ++recursive_cnt;
6129 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6130 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006131 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132
6133 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 ++recursive_cnt;
6135 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6136 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006137 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006138
6139 case VAR_FUNC:
6140 return (tv1->vval.v_string != NULL
6141 && tv2->vval.v_string != NULL
6142 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6143
6144 case VAR_NUMBER:
6145 return tv1->vval.v_number == tv2->vval.v_number;
6146
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006147#ifdef FEAT_FLOAT
6148 case VAR_FLOAT:
6149 return tv1->vval.v_float == tv2->vval.v_float;
6150#endif
6151
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152 case VAR_STRING:
6153 s1 = get_tv_string_buf(tv1, buf1);
6154 s2 = get_tv_string_buf(tv2, buf2);
6155 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157
6158 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006159 return TRUE;
6160}
6161
6162/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 * Locate item with index "n" in list "l" and return it.
6164 * A negative index is counted from the end; -1 is the last item.
6165 * Returns NULL when "n" is out of range.
6166 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006167 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170 long n;
6171{
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 long idx;
6174
6175 if (l == NULL)
6176 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006177
6178 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 n = l->lv_len + n;
6181
6182 /* Check for index out of range. */
6183 if (n < 0 || n >= l->lv_len)
6184 return NULL;
6185
6186 /* When there is a cached index may start search from there. */
6187 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006189 if (n < l->lv_idx / 2)
6190 {
6191 /* closest to the start of the list */
6192 item = l->lv_first;
6193 idx = 0;
6194 }
6195 else if (n > (l->lv_idx + l->lv_len) / 2)
6196 {
6197 /* closest to the end of the list */
6198 item = l->lv_last;
6199 idx = l->lv_len - 1;
6200 }
6201 else
6202 {
6203 /* closest to the cached index */
6204 item = l->lv_idx_item;
6205 idx = l->lv_idx;
6206 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207 }
6208 else
6209 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006210 if (n < l->lv_len / 2)
6211 {
6212 /* closest to the start of the list */
6213 item = l->lv_first;
6214 idx = 0;
6215 }
6216 else
6217 {
6218 /* closest to the end of the list */
6219 item = l->lv_last;
6220 idx = l->lv_len - 1;
6221 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006223
6224 while (n > idx)
6225 {
6226 /* search forward */
6227 item = item->li_next;
6228 ++idx;
6229 }
6230 while (n < idx)
6231 {
6232 /* search backward */
6233 item = item->li_prev;
6234 --idx;
6235 }
6236
6237 /* cache the used index */
6238 l->lv_idx = idx;
6239 l->lv_idx_item = item;
6240
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 return item;
6242}
6243
6244/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006245 * Get list item "l[idx]" as a number.
6246 */
6247 static long
6248list_find_nr(l, idx, errorp)
6249 list_T *l;
6250 long idx;
6251 int *errorp; /* set to TRUE when something wrong */
6252{
6253 listitem_T *li;
6254
6255 li = list_find(l, idx);
6256 if (li == NULL)
6257 {
6258 if (errorp != NULL)
6259 *errorp = TRUE;
6260 return -1L;
6261 }
6262 return get_tv_number_chk(&li->li_tv, errorp);
6263}
6264
6265/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006266 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6267 */
6268 char_u *
6269list_find_str(l, idx)
6270 list_T *l;
6271 long idx;
6272{
6273 listitem_T *li;
6274
6275 li = list_find(l, idx - 1);
6276 if (li == NULL)
6277 {
6278 EMSGN(_(e_listidx), idx);
6279 return NULL;
6280 }
6281 return get_tv_string(&li->li_tv);
6282}
6283
6284/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006285 * Locate "item" list "l" and return its index.
6286 * Returns -1 when "item" is not in the list.
6287 */
6288 static long
6289list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 list_T *l;
6291 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006292{
6293 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006295
6296 if (l == NULL)
6297 return -1;
6298 idx = 0;
6299 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6300 ++idx;
6301 if (li == NULL)
6302 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006303 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006304}
6305
6306/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 * Append item "item" to the end of list "l".
6308 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006309 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 list_T *l;
6312 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313{
6314 if (l->lv_last == NULL)
6315 {
6316 /* empty list */
6317 l->lv_first = item;
6318 l->lv_last = item;
6319 item->li_prev = NULL;
6320 }
6321 else
6322 {
6323 l->lv_last->li_next = item;
6324 item->li_prev = l->lv_last;
6325 l->lv_last = item;
6326 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 item->li_next = NULL;
6329}
6330
6331/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006335 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 list_T *l;
6338 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006340 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341
Bram Moolenaar05159a02005-02-26 23:04:13 +00006342 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006344 copy_tv(tv, &li->li_tv);
6345 list_append(l, li);
6346 return OK;
6347}
6348
6349/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006350 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006351 * Return FAIL when out of memory.
6352 */
6353 int
6354list_append_dict(list, dict)
6355 list_T *list;
6356 dict_T *dict;
6357{
6358 listitem_T *li = listitem_alloc();
6359
6360 if (li == NULL)
6361 return FAIL;
6362 li->li_tv.v_type = VAR_DICT;
6363 li->li_tv.v_lock = 0;
6364 li->li_tv.vval.v_dict = dict;
6365 list_append(list, li);
6366 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006367 return OK;
6368}
6369
6370/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006371 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006372 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006373 * Returns FAIL when out of memory.
6374 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006375 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006376list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006377 list_T *l;
6378 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006379 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006380{
6381 listitem_T *li = listitem_alloc();
6382
6383 if (li == NULL)
6384 return FAIL;
6385 list_append(l, li);
6386 li->li_tv.v_type = VAR_STRING;
6387 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006388 if (str == NULL)
6389 li->li_tv.vval.v_string = NULL;
6390 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006391 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006392 return FAIL;
6393 return OK;
6394}
6395
6396/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006397 * Append "n" to list "l".
6398 * Returns FAIL when out of memory.
6399 */
6400 static int
6401list_append_number(l, n)
6402 list_T *l;
6403 varnumber_T n;
6404{
6405 listitem_T *li;
6406
6407 li = listitem_alloc();
6408 if (li == NULL)
6409 return FAIL;
6410 li->li_tv.v_type = VAR_NUMBER;
6411 li->li_tv.v_lock = 0;
6412 li->li_tv.vval.v_number = n;
6413 list_append(l, li);
6414 return OK;
6415}
6416
6417/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 * If "item" is NULL append at the end.
6420 * Return FAIL when out of memory.
6421 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006422 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 list_T *l;
6425 typval_T *tv;
6426 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427{
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429
6430 if (ni == NULL)
6431 return FAIL;
6432 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006433 list_insert(l, ni, item);
6434 return OK;
6435}
6436
6437 void
6438list_insert(l, ni, item)
6439 list_T *l;
6440 listitem_T *ni;
6441 listitem_T *item;
6442{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 if (item == NULL)
6444 /* Append new item at end of list. */
6445 list_append(l, ni);
6446 else
6447 {
6448 /* Insert new item before existing item. */
6449 ni->li_prev = item->li_prev;
6450 ni->li_next = item;
6451 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 ++l->lv_idx;
6455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 l->lv_idx_item = NULL;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464}
6465
6466/*
6467 * Extend "l1" with "l2".
6468 * If "bef" is NULL append at the end, otherwise insert before this item.
6469 * Returns FAIL when out of memory.
6470 */
6471 static int
6472list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 list_T *l1;
6474 list_T *l2;
6475 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476{
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006478 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006480 /* We also quit the loop when we have inserted the original item count of
6481 * the list, avoid a hang when we extend a list with itself. */
6482 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6484 return FAIL;
6485 return OK;
6486}
6487
6488/*
6489 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6490 * Return FAIL when out of memory.
6491 */
6492 static int
6493list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 list_T *l1;
6495 list_T *l2;
6496 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497{
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006500 if (l1 == NULL || l2 == NULL)
6501 return FAIL;
6502
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 if (l == NULL)
6506 return FAIL;
6507 tv->v_type = VAR_LIST;
6508 tv->vval.v_list = l;
6509
6510 /* append all items from the second list */
6511 return list_extend(l, l2, NULL);
6512}
6513
6514/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 * Returns NULL when out of memory.
6519 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525{
Bram Moolenaar33570922005-01-25 22:26:29 +00006526 list_T *copy;
6527 listitem_T *item;
6528 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529
6530 if (orig == NULL)
6531 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532
6533 copy = list_alloc();
6534 if (copy != NULL)
6535 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 if (copyID != 0)
6537 {
6538 /* Do this before adding the items, because one of the items may
6539 * refer back to this list. */
6540 orig->lv_copyID = copyID;
6541 orig->lv_copylist = copy;
6542 }
6543 for (item = orig->lv_first; item != NULL && !got_int;
6544 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 {
6546 ni = listitem_alloc();
6547 if (ni == NULL)
6548 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006550 {
6551 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6552 {
6553 vim_free(ni);
6554 break;
6555 }
6556 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006558 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 list_append(copy, ni);
6560 }
6561 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 if (item != NULL)
6563 {
6564 list_unref(copy);
6565 copy = NULL;
6566 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 }
6568
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 return copy;
6570}
6571
6572/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006574 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006576 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006577list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *l;
6579 listitem_T *item;
6580 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581{
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 /* notify watchers */
6585 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006587 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588 list_fix_watch(l, ip);
6589 if (ip == item2)
6590 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592
6593 if (item2->li_next == NULL)
6594 l->lv_last = item->li_prev;
6595 else
6596 item2->li_next->li_prev = item->li_prev;
6597 if (item->li_prev == NULL)
6598 l->lv_first = item2->li_next;
6599 else
6600 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006601 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602}
6603
6604/*
6605 * Return an allocated string with the string representation of a list.
6606 * May return NULL.
6607 */
6608 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006609list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006611 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612{
6613 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614
6615 if (tv->vval.v_list == NULL)
6616 return NULL;
6617 ga_init2(&ga, (int)sizeof(char), 80);
6618 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006619 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 {
6621 vim_free(ga.ga_data);
6622 return NULL;
6623 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 ga_append(&ga, ']');
6625 ga_append(&ga, NUL);
6626 return (char_u *)ga.ga_data;
6627}
6628
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006629typedef struct join_S {
6630 char_u *s;
6631 char_u *tofree;
6632} join_T;
6633
6634 static int
6635list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6636 garray_T *gap; /* to store the result in */
6637 list_T *l;
6638 char_u *sep;
6639 int echo_style;
6640 int copyID;
6641 garray_T *join_gap; /* to keep each list item string */
6642{
6643 int i;
6644 join_T *p;
6645 int len;
6646 int sumlen = 0;
6647 int first = TRUE;
6648 char_u *tofree;
6649 char_u numbuf[NUMBUFLEN];
6650 listitem_T *item;
6651 char_u *s;
6652
6653 /* Stringify each item in the list. */
6654 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6655 {
6656 if (echo_style)
6657 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6658 else
6659 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6660 if (s == NULL)
6661 return FAIL;
6662
6663 len = (int)STRLEN(s);
6664 sumlen += len;
6665
6666 ga_grow(join_gap, 1);
6667 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6668 if (tofree != NULL || s != numbuf)
6669 {
6670 p->s = s;
6671 p->tofree = tofree;
6672 }
6673 else
6674 {
6675 p->s = vim_strnsave(s, len);
6676 p->tofree = p->s;
6677 }
6678
6679 line_breakcheck();
6680 }
6681
6682 /* Allocate result buffer with its total size, avoid re-allocation and
6683 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6684 if (join_gap->ga_len >= 2)
6685 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6686 if (ga_grow(gap, sumlen + 2) == FAIL)
6687 return FAIL;
6688
6689 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6690 {
6691 if (first)
6692 first = FALSE;
6693 else
6694 ga_concat(gap, sep);
6695 p = ((join_T *)join_gap->ga_data) + i;
6696
6697 if (p->s != NULL)
6698 ga_concat(gap, p->s);
6699 line_breakcheck();
6700 }
6701
6702 return OK;
6703}
6704
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006707 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006708 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006710 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006711list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006713 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006715 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006716 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 garray_T join_ga;
6719 int retval;
6720 join_T *p;
6721 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6724 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6725
6726 /* Dispose each item in join_ga. */
6727 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006729 p = (join_T *)join_ga.ga_data;
6730 for (i = 0; i < join_ga.ga_len; ++i)
6731 {
6732 vim_free(p->tofree);
6733 ++p;
6734 }
6735 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006736 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737
6738 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739}
6740
6741/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 * Garbage collection for lists and dictionaries.
6743 *
6744 * We use reference counts to be able to free most items right away when they
6745 * are no longer used. But for composite items it's possible that it becomes
6746 * unused while the reference count is > 0: When there is a recursive
6747 * reference. Example:
6748 * :let l = [1, 2, 3]
6749 * :let d = {9: l}
6750 * :let l[1] = d
6751 *
6752 * Since this is quite unusual we handle this with garbage collection: every
6753 * once in a while find out which lists and dicts are not referenced from any
6754 * variable.
6755 *
6756 * Here is a good reference text about garbage collection (refers to Python
6757 * but it applies to all reference-counting mechanisms):
6758 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760
6761/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 * Do garbage collection for lists and dicts.
6763 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 int
6766garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 buf_T *buf;
6770 win_T *wp;
6771 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006772 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int did_free;
6774 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006775#ifdef FEAT_WINDOWS
6776 tabpage_T *tp;
6777#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006779 /* Only do this once. */
6780 want_garbage_collect = FALSE;
6781 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006782 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006783
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006784 /* We advance by two because we add one for items referenced through
6785 * previous_funccal. */
6786 current_copyID += COPYID_INC;
6787 copyID = current_copyID;
6788
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789 /*
6790 * 1. Go through all accessible variables and mark all lists and dicts
6791 * with copyID.
6792 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006793
6794 /* Don't free variables in the previous_funccal list unless they are only
6795 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006796 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006797 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6798 {
6799 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6800 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6801 }
6802
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803 /* script-local variables */
6804 for (i = 1; i <= ga_scripts.ga_len; ++i)
6805 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6806
6807 /* buffer-local variables */
6808 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006809 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810
6811 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006812 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006813 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006814#ifdef FEAT_AUTOCMD
6815 if (aucmd_win != NULL)
6816 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6817#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819#ifdef FEAT_WINDOWS
6820 /* tabpage-local variables */
6821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006822 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006823#endif
6824
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 /* global variables */
6826 set_ref_in_ht(&globvarht, copyID);
6827
6828 /* function-local variables */
6829 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6830 {
6831 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6832 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6833 }
6834
Bram Moolenaard812df62008-11-09 12:46:09 +00006835 /* v: vars */
6836 set_ref_in_ht(&vimvarht, copyID);
6837
Bram Moolenaar1dced572012-04-05 16:54:08 +02006838#ifdef FEAT_LUA
6839 set_ref_in_lua(copyID);
6840#endif
6841
Bram Moolenaardb913952012-06-29 12:54:53 +02006842#ifdef FEAT_PYTHON
6843 set_ref_in_python(copyID);
6844#endif
6845
6846#ifdef FEAT_PYTHON3
6847 set_ref_in_python3(copyID);
6848#endif
6849
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 /*
6851 * 2. Free lists and dictionaries that are not referenced.
6852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 did_free = free_unref_items(copyID);
6854
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006855 /*
6856 * 3. Check if any funccal can be freed now.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 for (pfc = &previous_funccal; *pfc != NULL; )
6859 {
6860 if (can_free_funccal(*pfc, copyID))
6861 {
6862 fc = *pfc;
6863 *pfc = fc->caller;
6864 free_funccal(fc, TRUE);
6865 did_free = TRUE;
6866 did_free_funccal = TRUE;
6867 }
6868 else
6869 pfc = &(*pfc)->caller;
6870 }
6871 if (did_free_funccal)
6872 /* When a funccal was freed some more items might be garbage
6873 * collected, so run again. */
6874 (void)garbage_collect();
6875
6876 return did_free;
6877}
6878
6879/*
6880 * Free lists and dictionaries that are no longer referenced.
6881 */
6882 static int
6883free_unref_items(copyID)
6884 int copyID;
6885{
6886 dict_T *dd;
6887 list_T *ll;
6888 int did_free = FALSE;
6889
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 */
6893 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006896 /* Free the Dictionary and ordinary items it contains, but don't
6897 * recurse into Lists and Dictionaries, they will be in the list
6898 * of dicts or list of lists. */
6899 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 did_free = TRUE;
6901
6902 /* restart, next dict may also have been freed */
6903 dd = first_dict;
6904 }
6905 else
6906 dd = dd->dv_used_next;
6907
6908 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 * Go through the list of lists and free items without the copyID.
6910 * But don't free a list that has a watcher (used in a for loop), these
6911 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 */
6913 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6915 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006917 /* Free the List and ordinary items it contains, but don't recurse
6918 * into Lists and Dictionaries, they will be in the list of dicts
6919 * or list of lists. */
6920 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 did_free = TRUE;
6922
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006923 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 ll = first_list;
6925 }
6926 else
6927 ll = ll->lv_used_next;
6928
6929 return did_free;
6930}
6931
6932/*
6933 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6934 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006935 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936set_ref_in_ht(ht, copyID)
6937 hashtab_T *ht;
6938 int copyID;
6939{
6940 int todo;
6941 hashitem_T *hi;
6942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006943 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 for (hi = ht->ht_array; todo > 0; ++hi)
6945 if (!HASHITEM_EMPTY(hi))
6946 {
6947 --todo;
6948 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6949 }
6950}
6951
6952/*
6953 * Mark all lists and dicts referenced through list "l" with "copyID".
6954 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006955 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956set_ref_in_list(l, copyID)
6957 list_T *l;
6958 int copyID;
6959{
6960 listitem_T *li;
6961
6962 for (li = l->lv_first; li != NULL; li = li->li_next)
6963 set_ref_in_item(&li->li_tv, copyID);
6964}
6965
6966/*
6967 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6968 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006969 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970set_ref_in_item(tv, copyID)
6971 typval_T *tv;
6972 int copyID;
6973{
6974 dict_T *dd;
6975 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006976
6977 switch (tv->v_type)
6978 {
6979 case VAR_DICT:
6980 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006981 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006982 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 /* Didn't see this dict yet. */
6984 dd->dv_copyID = copyID;
6985 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006988
6989 case VAR_LIST:
6990 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006991 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006992 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 /* Didn't see this list yet. */
6994 ll->lv_copyID = copyID;
6995 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006996 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006998 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007000}
7001
7002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 * Allocate an empty header for a dictionary.
7004 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007005 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006dict_alloc()
7007{
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007009
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007011 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007012 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007013 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 if (first_dict != NULL)
7015 first_dict->dv_used_prev = d;
7016 d->dv_used_next = first_dict;
7017 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007018 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007021 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007022 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007023 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007024 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007026 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027}
7028
7029/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007030 * Allocate an empty dict for a return value.
7031 * Returns OK or FAIL.
7032 */
7033 static int
7034rettv_dict_alloc(rettv)
7035 typval_T *rettv;
7036{
7037 dict_T *d = dict_alloc();
7038
7039 if (d == NULL)
7040 return FAIL;
7041
7042 rettv->vval.v_dict = d;
7043 rettv->v_type = VAR_DICT;
7044 ++d->dv_refcount;
7045 return OK;
7046}
7047
7048
7049/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050 * Unreference a Dictionary: decrement the reference count and free it when it
7051 * becomes zero.
7052 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007053 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007055 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007057 if (d != NULL && --d->dv_refcount <= 0)
7058 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059}
7060
7061/*
7062 * Free a Dictionary, including all items it contains.
7063 * Ignores the reference count.
7064 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007065 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007066dict_free(d, recurse)
7067 dict_T *d;
7068 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007069{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007072 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 /* Remove the dict from the list of dicts for garbage collection. */
7075 if (d->dv_used_prev == NULL)
7076 first_dict = d->dv_used_next;
7077 else
7078 d->dv_used_prev->dv_used_next = d->dv_used_next;
7079 if (d->dv_used_next != NULL)
7080 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7081
7082 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007083 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007084 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 if (!HASHITEM_EMPTY(hi))
7088 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007089 /* Remove the item before deleting it, just in case there is
7090 * something recursive causing trouble. */
7091 di = HI2DI(hi);
7092 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007093 if (recurse || (di->di_tv.v_type != VAR_LIST
7094 && di->di_tv.v_type != VAR_DICT))
7095 clear_tv(&di->di_tv);
7096 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097 --todo;
7098 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 vim_free(d);
7102}
7103
7104/*
7105 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106 * The "key" is copied to the new item.
7107 * Note that the value of the item "di_tv" still needs to be initialized!
7108 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007110 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111dictitem_alloc(key)
7112 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113{
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007116 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007120 di->di_flags = 0;
7121 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123}
7124
7125/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126 * Make a copy of a Dictionary item.
7127 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131{
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007134 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7135 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136 if (di != NULL)
7137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007139 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 copy_tv(&org->di_tv, &di->di_tv);
7141 }
7142 return di;
7143}
7144
7145/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 * Remove item "item" from Dictionary "dict" and free it.
7147 */
7148 static void
7149dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 dict_T *dict;
7151 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007152{
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007156 if (HASHITEM_EMPTY(hi))
7157 EMSG2(_(e_intern2), "dictitem_remove()");
7158 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160 dictitem_free(item);
7161}
7162
7163/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164 * Free a dict item. Also clears the value.
7165 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007166 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 clear_tv(&item->di_tv);
7171 vim_free(item);
7172}
7173
7174/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7176 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007177 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 * Returns NULL when out of memory.
7179 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007181dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185{
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 dict_T *copy;
7187 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190
7191 if (orig == NULL)
7192 return NULL;
7193
7194 copy = dict_alloc();
7195 if (copy != NULL)
7196 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 if (copyID != 0)
7198 {
7199 orig->dv_copyID = copyID;
7200 orig->dv_copydict = copy;
7201 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007202 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 --todo;
7208
7209 di = dictitem_alloc(hi->hi_key);
7210 if (di == NULL)
7211 break;
7212 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 {
7214 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7215 copyID) == FAIL)
7216 {
7217 vim_free(di);
7218 break;
7219 }
7220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 else
7222 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7223 if (dict_add(copy, di) == FAIL)
7224 {
7225 dictitem_free(di);
7226 break;
7227 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007230
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007232 if (todo > 0)
7233 {
7234 dict_unref(copy);
7235 copy = NULL;
7236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 }
7238
7239 return copy;
7240}
7241
7242/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007244 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007246 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 dict_T *d;
7249 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250{
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252}
7253
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007255 * Add a number or string entry to dictionary "d".
7256 * When "str" is NULL use number "nr", otherwise use "str".
7257 * Returns FAIL when out of memory and when key already exists.
7258 */
7259 int
7260dict_add_nr_str(d, key, nr, str)
7261 dict_T *d;
7262 char *key;
7263 long nr;
7264 char_u *str;
7265{
7266 dictitem_T *item;
7267
7268 item = dictitem_alloc((char_u *)key);
7269 if (item == NULL)
7270 return FAIL;
7271 item->di_tv.v_lock = 0;
7272 if (str == NULL)
7273 {
7274 item->di_tv.v_type = VAR_NUMBER;
7275 item->di_tv.vval.v_number = nr;
7276 }
7277 else
7278 {
7279 item->di_tv.v_type = VAR_STRING;
7280 item->di_tv.vval.v_string = vim_strsave(str);
7281 }
7282 if (dict_add(d, item) == FAIL)
7283 {
7284 dictitem_free(item);
7285 return FAIL;
7286 }
7287 return OK;
7288}
7289
7290/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007291 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007292 * Returns FAIL when out of memory and when key already exists.
7293 */
7294 int
7295dict_add_list(d, key, list)
7296 dict_T *d;
7297 char *key;
7298 list_T *list;
7299{
7300 dictitem_T *item;
7301
7302 item = dictitem_alloc((char_u *)key);
7303 if (item == NULL)
7304 return FAIL;
7305 item->di_tv.v_lock = 0;
7306 item->di_tv.v_type = VAR_LIST;
7307 item->di_tv.vval.v_list = list;
7308 if (dict_add(d, item) == FAIL)
7309 {
7310 dictitem_free(item);
7311 return FAIL;
7312 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007313 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007314 return OK;
7315}
7316
7317/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Get the number of items in a Dictionary.
7319 */
7320 static long
7321dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 if (d == NULL)
7325 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007326 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327}
7328
7329/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 * Find item "key[len]" in Dictionary "d".
7331 * If "len" is negative use strlen(key).
7332 * Returns NULL when not found.
7333 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007334 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 char_u *key;
7338 int len;
7339{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340#define AKEYLEN 200
7341 char_u buf[AKEYLEN];
7342 char_u *akey;
7343 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 if (len < 0)
7347 akey = key;
7348 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350 tofree = akey = vim_strnsave(key, len);
7351 if (akey == NULL)
7352 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007353 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 else
7355 {
7356 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007357 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 akey = buf;
7359 }
7360
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 vim_free(tofree);
7363 if (HASHITEM_EMPTY(hi))
7364 return NULL;
7365 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366}
7367
7368/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007369 * Get a string item from a dictionary.
7370 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007371 * Returns NULL if the entry doesn't exist or out of memory.
7372 */
7373 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007374get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007375 dict_T *d;
7376 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007377 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007378{
7379 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007381
7382 di = dict_find(d, key, -1);
7383 if (di == NULL)
7384 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 s = get_tv_string(&di->di_tv);
7386 if (save && s != NULL)
7387 s = vim_strsave(s);
7388 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007389}
7390
7391/*
7392 * Get a number item from a dictionary.
7393 * Returns 0 if the entry doesn't exist or out of memory.
7394 */
7395 long
7396get_dict_number(d, key)
7397 dict_T *d;
7398 char_u *key;
7399{
7400 dictitem_T *di;
7401
7402 di = dict_find(d, key, -1);
7403 if (di == NULL)
7404 return 0;
7405 return get_tv_number(&di->di_tv);
7406}
7407
7408/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 * Return an allocated string with the string representation of a Dictionary.
7410 * May return NULL.
7411 */
7412 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416{
7417 garray_T ga;
7418 int first = TRUE;
7419 char_u *tofree;
7420 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 return NULL;
7428 ga_init2(&ga, (int)sizeof(char), 80);
7429 ga_append(&ga, '{');
7430
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007431 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 --todo;
7437
7438 if (first)
7439 first = FALSE;
7440 else
7441 ga_concat(&ga, (char_u *)", ");
7442
7443 tofree = string_quote(hi->hi_key, FALSE);
7444 if (tofree != NULL)
7445 {
7446 ga_concat(&ga, tofree);
7447 vim_free(tofree);
7448 }
7449 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007450 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007451 if (s != NULL)
7452 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 if (s == NULL)
7455 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007458 if (todo > 0)
7459 {
7460 vim_free(ga.ga_data);
7461 return NULL;
7462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 ga_append(&ga, '}');
7465 ga_append(&ga, NUL);
7466 return (char_u *)ga.ga_data;
7467}
7468
7469/*
7470 * Allocate a variable for a Dictionary and fill it from "*arg".
7471 * Return OK or FAIL. Returns NOTDONE for {expr}.
7472 */
7473 static int
7474get_dict_tv(arg, rettv, evaluate)
7475 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 int evaluate;
7478{
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 dict_T *d = NULL;
7480 typval_T tvkey;
7481 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007482 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486
7487 /*
7488 * First check if it's not a curly-braces thing: {expr}.
7489 * Must do this without evaluating, otherwise a function may be called
7490 * twice. Unfortunately this means we need to call eval1() twice for the
7491 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 if (*start != '}')
7495 {
7496 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7497 return FAIL;
7498 if (*start == '}')
7499 return NOTDONE;
7500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
7502 if (evaluate)
7503 {
7504 d = dict_alloc();
7505 if (d == NULL)
7506 return FAIL;
7507 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 tvkey.v_type = VAR_UNKNOWN;
7509 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510
7511 *arg = skipwhite(*arg + 1);
7512 while (**arg != '}' && **arg != NUL)
7513 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 if (**arg != ':')
7517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007518 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 goto failret;
7521 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007522 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007524 key = get_tv_string_buf_chk(&tvkey, buf);
7525 if (key == NULL || *key == NUL)
7526 {
7527 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7528 if (key != NULL)
7529 EMSG(_(e_emptykey));
7530 clear_tv(&tvkey);
7531 goto failret;
7532 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534
7535 *arg = skipwhite(*arg + 1);
7536 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7537 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007538 if (evaluate)
7539 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 goto failret;
7541 }
7542 if (evaluate)
7543 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 if (item != NULL)
7546 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007547 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549 clear_tv(&tv);
7550 goto failret;
7551 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 item = dictitem_alloc(key);
7553 clear_tv(&tvkey);
7554 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007557 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 if (dict_add(d, item) == FAIL)
7559 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 }
7561 }
7562
7563 if (**arg == '}')
7564 break;
7565 if (**arg != ',')
7566 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007567 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 goto failret;
7569 }
7570 *arg = skipwhite(*arg + 1);
7571 }
7572
7573 if (**arg != '}')
7574 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007575 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576failret:
7577 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007578 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 return FAIL;
7580 }
7581
7582 *arg = skipwhite(*arg + 1);
7583 if (evaluate)
7584 {
7585 rettv->v_type = VAR_DICT;
7586 rettv->vval.v_dict = d;
7587 ++d->dv_refcount;
7588 }
7589
7590 return OK;
7591}
7592
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 * Return a string with the string representation of a variable.
7595 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007596 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007597 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007599 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 */
7601 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007604 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007605 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007608 static int recurse = 0;
7609 char_u *r = NULL;
7610
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007613 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 *tofree = NULL;
7615 return NULL;
7616 }
7617 ++recurse;
7618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007619 switch (tv->v_type)
7620 {
7621 case VAR_FUNC:
7622 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007623 r = tv->vval.v_string;
7624 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007627 if (tv->vval.v_list == NULL)
7628 {
7629 *tofree = NULL;
7630 r = NULL;
7631 }
7632 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7633 {
7634 *tofree = NULL;
7635 r = (char_u *)"[...]";
7636 }
7637 else
7638 {
7639 tv->vval.v_list->lv_copyID = copyID;
7640 *tofree = list2string(tv, copyID);
7641 r = *tofree;
7642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646 if (tv->vval.v_dict == NULL)
7647 {
7648 *tofree = NULL;
7649 r = NULL;
7650 }
7651 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7652 {
7653 *tofree = NULL;
7654 r = (char_u *)"{...}";
7655 }
7656 else
7657 {
7658 tv->vval.v_dict->dv_copyID = copyID;
7659 *tofree = dict2string(tv, copyID);
7660 r = *tofree;
7661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 case VAR_STRING:
7665 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 *tofree = NULL;
7667 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007668 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 case VAR_FLOAT:
7672 *tofree = NULL;
7673 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7674 r = numbuf;
7675 break;
7676#endif
7677
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007680 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682
7683 --recurse;
7684 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007685}
7686
7687/*
7688 * Return a string with the string representation of a variable.
7689 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7690 * "numbuf" is used for a number.
7691 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007692 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 */
7694 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007695tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697 char_u **tofree;
7698 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007699 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700{
7701 switch (tv->v_type)
7702 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 case VAR_FUNC:
7704 *tofree = string_quote(tv->vval.v_string, TRUE);
7705 return *tofree;
7706 case VAR_STRING:
7707 *tofree = string_quote(tv->vval.v_string, FALSE);
7708 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709#ifdef FEAT_FLOAT
7710 case VAR_FLOAT:
7711 *tofree = NULL;
7712 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7713 return numbuf;
7714#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007716 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007718 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007721 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007722 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723}
7724
7725/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007726 * Return string "str" in ' quotes, doubling ' characters.
7727 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 */
7730 static char_u *
7731string_quote(str, function)
7732 char_u *str;
7733 int function;
7734{
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 char_u *p, *r, *s;
7737
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 len = (function ? 13 : 3);
7739 if (str != NULL)
7740 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007741 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 for (p = str; *p != NUL; mb_ptr_adv(p))
7743 if (*p == '\'')
7744 ++len;
7745 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007746 s = r = alloc(len);
7747 if (r != NULL)
7748 {
7749 if (function)
7750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752 r += 10;
7753 }
7754 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 if (str != NULL)
7757 for (p = str; *p != NUL; )
7758 {
7759 if (*p == '\'')
7760 *r++ = '\'';
7761 MB_COPY_CHAR(p, r);
7762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007764 if (function)
7765 *r++ = ')';
7766 *r++ = NUL;
7767 }
7768 return s;
7769}
7770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007771#ifdef FEAT_FLOAT
7772/*
7773 * Convert the string "text" to a floating point number.
7774 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7775 * this always uses a decimal point.
7776 * Returns the length of the text that was consumed.
7777 */
7778 static int
7779string2float(text, value)
7780 char_u *text;
7781 float_T *value; /* result stored here */
7782{
7783 char *s = (char *)text;
7784 float_T f;
7785
7786 f = strtod(s, &s);
7787 *value = f;
7788 return (int)((char_u *)s - text);
7789}
7790#endif
7791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 * Get the value of an environment variable.
7794 * "arg" is pointing to the '$'. It is advanced to after the name.
7795 * If the environment variable was not set, silently assume it is empty.
7796 * Always return OK.
7797 */
7798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 int evaluate;
7803{
7804 char_u *string = NULL;
7805 int len;
7806 int cc;
7807 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007808 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810 ++*arg;
7811 name = *arg;
7812 len = get_env_len(arg);
7813 if (evaluate)
7814 {
7815 if (len != 0)
7816 {
7817 cc = name[len];
7818 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007819 /* first try vim_getenv(), fast for normal environment vars */
7820 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007822 {
7823 if (!mustfree)
7824 string = vim_strsave(string);
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 else
7827 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007828 if (mustfree)
7829 vim_free(string);
7830
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 /* next try expanding things like $VIM and ${HOME} */
7832 string = expand_env_save(name - 1);
7833 if (string != NULL && *string == '$')
7834 {
7835 vim_free(string);
7836 string = NULL;
7837 }
7838 }
7839 name[len] = cc;
7840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->v_type = VAR_STRING;
7842 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844
7845 return OK;
7846}
7847
7848/*
7849 * Array with names and number of arguments of all internal functions
7850 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7851 */
7852static struct fst
7853{
7854 char *f_name; /* function name */
7855 char f_min_argc; /* minimal number of arguments */
7856 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007858 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859} functions[] =
7860{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861#ifdef FEAT_FLOAT
7862 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007863 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007865 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007866 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"append", 2, 2, f_append},
7868 {"argc", 0, 0, f_argc},
7869 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007870 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007872 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007877 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"bufexists", 1, 1, f_bufexists},
7879 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7880 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7881 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7882 {"buflisted", 1, 1, f_buflisted},
7883 {"bufloaded", 1, 1, f_bufloaded},
7884 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007885 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"bufwinnr", 1, 1, f_bufwinnr},
7887 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007888 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007889 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007891#ifdef FEAT_FLOAT
7892 {"ceil", 1, 1, f_ceil},
7893#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007894 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007895 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007897 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007899#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007900 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007901 {"complete_add", 1, 1, f_complete_add},
7902 {"complete_check", 0, 0, f_complete_check},
7903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#ifdef FEAT_FLOAT
7907 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007908 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007912 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007913 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"delete", 1, 1, f_delete},
7915 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007916 {"diff_filler", 1, 1, f_diff_filler},
7917 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007918 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"eventhandler", 0, 0, f_eventhandler},
7922 {"executable", 1, 1, f_executable},
7923 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007924#ifdef FEAT_FLOAT
7925 {"exp", 1, 1, f_exp},
7926#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007927 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007928 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007929 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7931 {"filereadable", 1, 1, f_filereadable},
7932 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007934 {"finddir", 1, 3, f_finddir},
7935 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007936#ifdef FEAT_FLOAT
7937 {"float2nr", 1, 1, f_float2nr},
7938 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007939 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007940#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007941 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"fnamemodify", 2, 2, f_fnamemodify},
7943 {"foldclosed", 1, 1, f_foldclosed},
7944 {"foldclosedend", 1, 1, f_foldclosedend},
7945 {"foldlevel", 1, 1, f_foldlevel},
7946 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007947 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007950 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007951 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007952 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007953 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getchar", 0, 1, f_getchar},
7955 {"getcharmod", 0, 0, f_getcharmod},
7956 {"getcmdline", 0, 0, f_getcmdline},
7957 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007958 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007960 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007961 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"getfsize", 1, 1, f_getfsize},
7963 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007964 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007965 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007966 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007967 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007968 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007969 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007970 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007971 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007973 {"gettabvar", 2, 3, f_gettabvar},
7974 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"getwinposx", 0, 0, f_getwinposx},
7976 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007977 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007978 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007979 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007982 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007983 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7985 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7986 {"histadd", 2, 2, f_histadd},
7987 {"histdel", 1, 2, f_histdel},
7988 {"histget", 1, 2, f_histget},
7989 {"histnr", 1, 1, f_histnr},
7990 {"hlID", 1, 1, f_hlID},
7991 {"hlexists", 1, 1, f_hlexists},
7992 {"hostname", 0, 0, f_hostname},
7993 {"iconv", 3, 3, f_iconv},
7994 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007996 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007998 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"inputrestore", 0, 0, f_inputrestore},
8000 {"inputsave", 0, 0, f_inputsave},
8001 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008003 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008005 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"libcall", 3, 3, f_libcall},
8012 {"libcallnr", 3, 3, f_libcallnr},
8013 {"line", 1, 1, f_line},
8014 {"line2byte", 1, 1, f_line2byte},
8015 {"lispindent", 1, 1, f_lispindent},
8016 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008018 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008019 {"log10", 1, 1, f_log10},
8020#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008021#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008022 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008023#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008025 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008026 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008027 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008028 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008029 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008030 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008031 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008032 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008034 {"max", 1, 1, f_max},
8035 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008036#ifdef vim_mkdir
8037 {"mkdir", 1, 3, f_mkdir},
8038#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008040#ifdef FEAT_MZSCHEME
8041 {"mzeval", 1, 1, f_mzeval},
8042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008044 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008045 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008046 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047#ifdef FEAT_FLOAT
8048 {"pow", 2, 2, f_pow},
8049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008051 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008052 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008053#ifdef FEAT_PYTHON3
8054 {"py3eval", 1, 1, f_py3eval},
8055#endif
8056#ifdef FEAT_PYTHON
8057 {"pyeval", 1, 1, f_pyeval},
8058#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008059 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008060 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008061 {"reltime", 0, 2, f_reltime},
8062 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"remote_expr", 2, 3, f_remote_expr},
8064 {"remote_foreground", 1, 1, f_remote_foreground},
8065 {"remote_peek", 1, 2, f_remote_peek},
8066 {"remote_read", 1, 1, f_remote_read},
8067 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008070 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008072 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#ifdef FEAT_FLOAT
8074 {"round", 1, 1, f_round},
8075#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008076 {"screenattr", 2, 2, f_screenattr},
8077 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008078 {"screencol", 0, 0, f_screencol},
8079 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008080 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008081 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008082 {"searchpair", 3, 7, f_searchpair},
8083 {"searchpairpos", 3, 7, f_searchpairpos},
8084 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"server2client", 2, 2, f_server2client},
8086 {"serverlist", 0, 0, f_serverlist},
8087 {"setbufvar", 3, 3, f_setbufvar},
8088 {"setcmdpos", 1, 1, f_setcmdpos},
8089 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008090 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008091 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008092 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008093 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008095 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008096 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008098#ifdef FEAT_CRYPT
8099 {"sha256", 1, 1, f_sha256},
8100#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008101 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008102 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008104#ifdef FEAT_FLOAT
8105 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008106 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008108 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008109 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008110 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008111 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008112 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"sqrt", 1, 1, f_sqrt},
8115 {"str2float", 1, 1, f_str2float},
8116#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008117 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008118 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008119 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120#ifdef HAVE_STRFTIME
8121 {"strftime", 1, 2, f_strftime},
8122#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008123 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"strlen", 1, 1, f_strlen},
8126 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008127 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008129 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"submatch", 1, 1, f_submatch},
8131 {"substitute", 4, 4, f_substitute},
8132 {"synID", 3, 3, f_synID},
8133 {"synIDattr", 2, 3, f_synIDattr},
8134 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008135 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008136 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008137 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008138 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008139 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008140 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008141 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008142 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008143#ifdef FEAT_FLOAT
8144 {"tan", 1, 1, f_tan},
8145 {"tanh", 1, 1, f_tanh},
8146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008148 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"tolower", 1, 1, f_tolower},
8150 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008151 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152#ifdef FEAT_FLOAT
8153 {"trunc", 1, 1, f_trunc},
8154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008156 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008157 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008158 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008159 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"virtcol", 1, 1, f_virtcol},
8161 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008162 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"winbufnr", 1, 1, f_winbufnr},
8164 {"wincol", 0, 0, f_wincol},
8165 {"winheight", 1, 1, f_winheight},
8166 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008167 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008169 {"winrestview", 1, 1, f_winrestview},
8170 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008172 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008173 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174};
8175
8176#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8177
8178/*
8179 * Function given to ExpandGeneric() to obtain the list of internal
8180 * or user defined function names.
8181 */
8182 char_u *
8183get_function_name(xp, idx)
8184 expand_T *xp;
8185 int idx;
8186{
8187 static int intidx = -1;
8188 char_u *name;
8189
8190 if (idx == 0)
8191 intidx = -1;
8192 if (intidx < 0)
8193 {
8194 name = get_user_func_name(xp, idx);
8195 if (name != NULL)
8196 return name;
8197 }
8198 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8199 {
8200 STRCPY(IObuff, functions[intidx].f_name);
8201 STRCAT(IObuff, "(");
8202 if (functions[intidx].f_max_argc == 0)
8203 STRCAT(IObuff, ")");
8204 return IObuff;
8205 }
8206
8207 return NULL;
8208}
8209
8210/*
8211 * Function given to ExpandGeneric() to obtain the list of internal or
8212 * user defined variable or function names.
8213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 char_u *
8215get_expr_name(xp, idx)
8216 expand_T *xp;
8217 int idx;
8218{
8219 static int intidx = -1;
8220 char_u *name;
8221
8222 if (idx == 0)
8223 intidx = -1;
8224 if (intidx < 0)
8225 {
8226 name = get_function_name(xp, idx);
8227 if (name != NULL)
8228 return name;
8229 }
8230 return get_user_var_name(xp, ++intidx);
8231}
8232
8233#endif /* FEAT_CMDL_COMPL */
8234
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008235#if defined(EBCDIC) || defined(PROTO)
8236/*
8237 * Compare struct fst by function name.
8238 */
8239 static int
8240compare_func_name(s1, s2)
8241 const void *s1;
8242 const void *s2;
8243{
8244 struct fst *p1 = (struct fst *)s1;
8245 struct fst *p2 = (struct fst *)s2;
8246
8247 return STRCMP(p1->f_name, p2->f_name);
8248}
8249
8250/*
8251 * Sort the function table by function name.
8252 * The sorting of the table above is ASCII dependant.
8253 * On machines using EBCDIC we have to sort it.
8254 */
8255 static void
8256sortFunctions()
8257{
8258 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8259
8260 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8261}
8262#endif
8263
8264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265/*
8266 * Find internal function in table above.
8267 * Return index, or -1 if not found
8268 */
8269 static int
8270find_internal_func(name)
8271 char_u *name; /* name of the function */
8272{
8273 int first = 0;
8274 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8275 int cmp;
8276 int x;
8277
8278 /*
8279 * Find the function name in the table. Binary search.
8280 */
8281 while (first <= last)
8282 {
8283 x = first + ((unsigned)(last - first) >> 1);
8284 cmp = STRCMP(name, functions[x].f_name);
8285 if (cmp < 0)
8286 last = x - 1;
8287 else if (cmp > 0)
8288 first = x + 1;
8289 else
8290 return x;
8291 }
8292 return -1;
8293}
8294
8295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008296 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8297 * name it contains, otherwise return "name".
8298 */
8299 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008300deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008301 char_u *name;
8302 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008303 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008304{
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306 int cc;
8307
8308 cc = name[*lenp];
8309 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 {
8316 *lenp = 0;
8317 return (char_u *)""; /* just in case */
8318 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008319 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 }
8322
8323 return name;
8324}
8325
8326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 * Allocate a variable for the result of a function.
8328 * Return OK or FAIL.
8329 */
8330 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8332 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 char_u *name; /* name of the function */
8334 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 char_u **arg; /* argument, pointing to the '(' */
8337 linenr_T firstline; /* first line of range */
8338 linenr_T lastline; /* last line of range */
8339 int *doesrange; /* return: function handled range */
8340 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 char_u *argp;
8344 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008345 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 int argcount = 0; /* number of arguments found */
8347
8348 /*
8349 * Get the arguments.
8350 */
8351 argp = *arg;
8352 while (argcount < MAX_FUNC_ARGS)
8353 {
8354 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8355 if (*argp == ')' || *argp == ',' || *argp == NUL)
8356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8358 {
8359 ret = FAIL;
8360 break;
8361 }
8362 ++argcount;
8363 if (*argp != ',')
8364 break;
8365 }
8366 if (*argp == ')')
8367 ++argp;
8368 else
8369 ret = FAIL;
8370
8371 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008373 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 {
8376 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008377 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008379 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
8385 *arg = skipwhite(argp);
8386 return ret;
8387}
8388
8389
8390/*
8391 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008392 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008393 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 */
8395 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008396call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008397 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008398 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008402 typval_T *argvars; /* vars for arguments, must have "argcount"
8403 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 linenr_T firstline; /* first line of range */
8405 linenr_T lastline; /* last line of range */
8406 int *doesrange; /* return: function handled range */
8407 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008408 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#define ERROR_UNKNOWN 0
8412#define ERROR_TOOMANY 1
8413#define ERROR_TOOFEW 2
8414#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415#define ERROR_DICT 4
8416#define ERROR_NONE 5
8417#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 int error = ERROR_NONE;
8419 int i;
8420 int llen;
8421 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422#define FLEN_FIXED 40
8423 char_u fname_buf[FLEN_FIXED + 1];
8424 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008425 char_u *name;
8426
8427 /* Make a copy of the name, if it comes from a funcref variable it could
8428 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008429 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008430 if (name == NULL)
8431 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433 /*
8434 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8435 * Change <SNR>123_name() to K_SNR 123_name().
8436 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 llen = eval_fname_script(name);
8439 if (llen > 0)
8440 {
8441 fname_buf[0] = K_SPECIAL;
8442 fname_buf[1] = KS_EXTRA;
8443 fname_buf[2] = (int)KE_SNR;
8444 i = 3;
8445 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8446 {
8447 if (current_SID <= 0)
8448 error = ERROR_SCRIPT;
8449 else
8450 {
8451 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8452 i = (int)STRLEN(fname_buf);
8453 }
8454 }
8455 if (i + STRLEN(name + llen) < FLEN_FIXED)
8456 {
8457 STRCPY(fname_buf + i, name + llen);
8458 fname = fname_buf;
8459 }
8460 else
8461 {
8462 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8463 if (fname == NULL)
8464 error = ERROR_OTHER;
8465 else
8466 {
8467 mch_memmove(fname, fname_buf, (size_t)i);
8468 STRCPY(fname + i, name + llen);
8469 }
8470 }
8471 }
8472 else
8473 fname = name;
8474
8475 *doesrange = FALSE;
8476
8477
8478 /* execute the function if no errors detected and executing */
8479 if (evaluate && error == ERROR_NONE)
8480 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008481 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8482 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 error = ERROR_UNKNOWN;
8484
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008485 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {
8487 /*
8488 * User defined function.
8489 */
8490 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493 /* Trigger FuncUndefined event, may load the function. */
8494 if (fp == NULL
8495 && apply_autocmds(EVENT_FUNCUNDEFINED,
8496 fname, fname, TRUE, NULL)
8497 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008499 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 fp = find_func(fname);
8501 }
8502#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008503 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008504 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008505 {
8506 /* loaded a package, search for the function again */
8507 fp = find_func(fname);
8508 }
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (fp != NULL)
8511 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008512 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008514 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008518 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 else
8521 {
8522 /*
8523 * Call the user function.
8524 * Save and restore search patterns, script variables and
8525 * redo buffer.
8526 */
8527 save_search_patterns();
8528 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008529 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8533 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8534 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008535 /* Function was unreferenced while being used, free it
8536 * now. */
8537 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 restoreRedobuff();
8539 restore_search_patterns();
8540 error = ERROR_NONE;
8541 }
8542 }
8543 }
8544 else
8545 {
8546 /*
8547 * Find the function name in the table, call its implementation.
8548 */
8549 i = find_internal_func(fname);
8550 if (i >= 0)
8551 {
8552 if (argcount < functions[i].f_min_argc)
8553 error = ERROR_TOOFEW;
8554 else if (argcount > functions[i].f_max_argc)
8555 error = ERROR_TOOMANY;
8556 else
8557 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 error = ERROR_NONE;
8561 }
8562 }
8563 }
8564 /*
8565 * The function call (or "FuncUndefined" autocommand sequence) might
8566 * have been aborted by an error, an interrupt, or an explicitly thrown
8567 * exception that has not been caught so far. This situation can be
8568 * tested for by calling aborting(). For an error in an internal
8569 * function or for the "E132" error in call_user_func(), however, the
8570 * throw point at which the "force_abort" flag (temporarily reset by
8571 * emsg()) is normally updated has not been reached yet. We need to
8572 * update that flag first to make aborting() reliable.
8573 */
8574 update_force_abort();
8575 }
8576 if (error == ERROR_NONE)
8577 ret = OK;
8578
8579 /*
8580 * Report an error unless the argument evaluation or function call has been
8581 * cancelled due to an aborting error, an interrupt, or an exception.
8582 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008583 if (!aborting())
8584 {
8585 switch (error)
8586 {
8587 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008588 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008589 break;
8590 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008592 break;
8593 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008594 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008595 name);
8596 break;
8597 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008598 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 name);
8600 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008601 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008602 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 name);
8604 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008605 }
8606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (fname != name && fname != fname_buf)
8609 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008610 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
8612 return ret;
8613}
8614
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008615/*
8616 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008617 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008618 */
8619 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008620emsg_funcname(ermsg, name)
8621 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008622 char_u *name;
8623{
8624 char_u *p;
8625
8626 if (*name == K_SPECIAL)
8627 p = concat_str((char_u *)"<SNR>", name + 3);
8628 else
8629 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008630 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008631 if (p != name)
8632 vim_free(p);
8633}
8634
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008635/*
8636 * Return TRUE for a non-zero Number and a non-empty String.
8637 */
8638 static int
8639non_zero_arg(argvars)
8640 typval_T *argvars;
8641{
8642 return ((argvars[0].v_type == VAR_NUMBER
8643 && argvars[0].vval.v_number != 0)
8644 || (argvars[0].v_type == VAR_STRING
8645 && argvars[0].vval.v_string != NULL
8646 && *argvars[0].vval.v_string != NUL));
8647}
8648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649/*********************************************
8650 * Implementation of the built-in functions
8651 */
8652
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008653#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008654static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8655
8656/*
8657 * Get the float value of "argvars[0]" into "f".
8658 * Returns FAIL when the argument is not a Number or Float.
8659 */
8660 static int
8661get_float_arg(argvars, f)
8662 typval_T *argvars;
8663 float_T *f;
8664{
8665 if (argvars[0].v_type == VAR_FLOAT)
8666 {
8667 *f = argvars[0].vval.v_float;
8668 return OK;
8669 }
8670 if (argvars[0].v_type == VAR_NUMBER)
8671 {
8672 *f = (float_T)argvars[0].vval.v_number;
8673 return OK;
8674 }
8675 EMSG(_("E808: Number or Float required"));
8676 return FAIL;
8677}
8678
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008679/*
8680 * "abs(expr)" function
8681 */
8682 static void
8683f_abs(argvars, rettv)
8684 typval_T *argvars;
8685 typval_T *rettv;
8686{
8687 if (argvars[0].v_type == VAR_FLOAT)
8688 {
8689 rettv->v_type = VAR_FLOAT;
8690 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8691 }
8692 else
8693 {
8694 varnumber_T n;
8695 int error = FALSE;
8696
8697 n = get_tv_number_chk(&argvars[0], &error);
8698 if (error)
8699 rettv->vval.v_number = -1;
8700 else if (n > 0)
8701 rettv->vval.v_number = n;
8702 else
8703 rettv->vval.v_number = -n;
8704 }
8705}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008706
8707/*
8708 * "acos()" function
8709 */
8710 static void
8711f_acos(argvars, rettv)
8712 typval_T *argvars;
8713 typval_T *rettv;
8714{
8715 float_T f;
8716
8717 rettv->v_type = VAR_FLOAT;
8718 if (get_float_arg(argvars, &f) == OK)
8719 rettv->vval.v_float = acos(f);
8720 else
8721 rettv->vval.v_float = 0.0;
8722}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008723#endif
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 */
8728 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008738 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008739 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008740 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 }
8743 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 EMSG(_(e_listreq));
8745}
8746
8747/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008748 * "and(expr, expr)" function
8749 */
8750 static void
8751f_and(argvars, rettv)
8752 typval_T *argvars;
8753 typval_T *rettv;
8754{
8755 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8756 & get_tv_number_chk(&argvars[1], NULL);
8757}
8758
8759/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 * "append(lnum, string/list)" function
8761 */
8762 static void
8763f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *argvars;
8765 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766{
8767 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 list_T *l = NULL;
8770 listitem_T *li = NULL;
8771 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008772 long added = 0;
8773
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008774 /* When coming here from Insert mode, sync undo, so that this can be
8775 * undone separately from what was previously inserted. */
8776 if (u_sync_once == 2)
8777 {
8778 u_sync_once = 1; /* notify that u_sync() was called */
8779 u_sync(TRUE);
8780 }
8781
Bram Moolenaar0d660222005-01-07 21:51:51 +00008782 lnum = get_tv_lnum(argvars);
8783 if (lnum >= 0
8784 && lnum <= curbuf->b_ml.ml_line_count
8785 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008786 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008787 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008789 l = argvars[1].vval.v_list;
8790 if (l == NULL)
8791 return;
8792 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008793 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008794 for (;;)
8795 {
8796 if (l == NULL)
8797 tv = &argvars[1]; /* append a string */
8798 else if (li == NULL)
8799 break; /* end of list */
8800 else
8801 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008802 line = get_tv_string_chk(tv);
8803 if (line == NULL) /* type error */
8804 {
8805 rettv->vval.v_number = 1; /* Failed */
8806 break;
8807 }
8808 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008809 ++added;
8810 if (l == NULL)
8811 break;
8812 li = li->li_next;
8813 }
8814
8815 appended_lines_mark(lnum, added);
8816 if (curwin->w_cursor.lnum > lnum)
8817 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 else
8820 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
8822
8823/*
8824 * "argc()" function
8825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
8835 * "argidx()" function
8836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843}
8844
8845/*
8846 * "argv(nr)" function
8847 */
8848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *argvars;
8851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 int idx;
8854
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008855 if (argvars[0].v_type != VAR_UNKNOWN)
8856 {
8857 idx = get_tv_number_chk(&argvars[0], NULL);
8858 if (idx >= 0 && idx < ARGCOUNT)
8859 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8860 else
8861 rettv->vval.v_string = NULL;
8862 rettv->v_type = VAR_STRING;
8863 }
8864 else if (rettv_list_alloc(rettv) == OK)
8865 for (idx = 0; idx < ARGCOUNT; ++idx)
8866 list_append_string(rettv->vval.v_list,
8867 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868}
8869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008871/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008872 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008873 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008874 static void
8875f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008876 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008877 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879 float_T f;
8880
8881 rettv->v_type = VAR_FLOAT;
8882 if (get_float_arg(argvars, &f) == OK)
8883 rettv->vval.v_float = asin(f);
8884 else
8885 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886}
8887
8888/*
8889 * "atan()" function
8890 */
8891 static void
8892f_atan(argvars, rettv)
8893 typval_T *argvars;
8894 typval_T *rettv;
8895{
8896 float_T f;
8897
8898 rettv->v_type = VAR_FLOAT;
8899 if (get_float_arg(argvars, &f) == OK)
8900 rettv->vval.v_float = atan(f);
8901 else
8902 rettv->vval.v_float = 0.0;
8903}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008904
8905/*
8906 * "atan2()" function
8907 */
8908 static void
8909f_atan2(argvars, rettv)
8910 typval_T *argvars;
8911 typval_T *rettv;
8912{
8913 float_T fx, fy;
8914
8915 rettv->v_type = VAR_FLOAT;
8916 if (get_float_arg(argvars, &fx) == OK
8917 && get_float_arg(&argvars[1], &fy) == OK)
8918 rettv->vval.v_float = atan2(fx, fy);
8919 else
8920 rettv->vval.v_float = 0.0;
8921}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008922#endif
8923
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924/*
8925 * "browse(save, title, initdir, default)" function
8926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932#ifdef FEAT_BROWSE
8933 int save;
8934 char_u *title;
8935 char_u *initdir;
8936 char_u *defname;
8937 char_u buf[NUMBUFLEN];
8938 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 save = get_tv_number_chk(&argvars[0], &error);
8942 title = get_tv_string_chk(&argvars[1]);
8943 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8944 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 if (error || title == NULL || initdir == NULL || defname == NULL)
8947 rettv->vval.v_string = NULL;
8948 else
8949 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950 do_browse(save ? BROWSE_SAVE : 0,
8951 title, defname, NULL, initdir, NULL, curbuf);
8952#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008954#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008956}
8957
8958/*
8959 * "browsedir(title, initdir)" function
8960 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008964 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008965{
8966#ifdef FEAT_BROWSE
8967 char_u *title;
8968 char_u *initdir;
8969 char_u buf[NUMBUFLEN];
8970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 title = get_tv_string_chk(&argvars[0]);
8972 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 if (title == NULL || initdir == NULL)
8975 rettv->vval.v_string = NULL;
8976 else
8977 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008978 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984
Bram Moolenaar33570922005-01-25 22:26:29 +00008985static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987/*
8988 * Find a buffer by number or exact name.
8989 */
8990 static buf_T *
8991find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 if (avar->v_type == VAR_NUMBER)
8997 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008998 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009001 if (buf == NULL)
9002 {
9003 /* No full path name match, try a match with a URL or a "nofile"
9004 * buffer, these don't use the full path. */
9005 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9006 if (buf->b_fname != NULL
9007 && (path_with_url(buf->b_fname)
9008#ifdef FEAT_QUICKFIX
9009 || bt_nofile(buf)
9010#endif
9011 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009013 break;
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 }
9016 return buf;
9017}
9018
9019/*
9020 * "bufexists(expr)" function
9021 */
9022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009024 typval_T *argvars;
9025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
9030/*
9031 * "buflisted(expr)" function
9032 */
9033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009035 typval_T *argvars;
9036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037{
9038 buf_T *buf;
9039
9040 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042}
9043
9044/*
9045 * "bufloaded(expr)" function
9046 */
9047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T *argvars;
9050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 buf_T *buf;
9053
9054 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056}
9057
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009058static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060/*
9061 * Get buffer by number or pattern.
9062 */
9063 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009064get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009066 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 int save_magic;
9070 char_u *save_cpo;
9071 buf_T *buf;
9072
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 if (tv->v_type == VAR_NUMBER)
9074 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009075 if (tv->v_type != VAR_STRING)
9076 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if (name == NULL || *name == NUL)
9078 return curbuf;
9079 if (name[0] == '$' && name[1] == NUL)
9080 return lastbuf;
9081
9082 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9083 save_magic = p_magic;
9084 p_magic = TRUE;
9085 save_cpo = p_cpo;
9086 p_cpo = (char_u *)"";
9087
9088 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009089 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
9091 p_magic = save_magic;
9092 p_cpo = save_cpo;
9093
9094 /* If not found, try expanding the name, like done for bufexists(). */
9095 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
9098 return buf;
9099}
9100
9101/*
9102 * "bufname(expr)" function
9103 */
9104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *argvars;
9107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 buf_T *buf;
9110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009111 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009113 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 --emsg_off;
9120}
9121
9122/*
9123 * "bufnr(expr)" function
9124 */
9125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009127 typval_T *argvars;
9128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009131 int error = FALSE;
9132 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009136 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009137 --emsg_off;
9138
9139 /* If the buffer isn't found and the second argument is not zero create a
9140 * new buffer. */
9141 if (buf == NULL
9142 && argvars[1].v_type != VAR_UNKNOWN
9143 && get_tv_number_chk(&argvars[1], &error) != 0
9144 && !error
9145 && (name = get_tv_string_chk(&argvars[0])) != NULL
9146 && !error)
9147 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153}
9154
9155/*
9156 * "bufwinnr(nr)" function
9157 */
9158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009160 typval_T *argvars;
9161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163#ifdef FEAT_WINDOWS
9164 win_T *wp;
9165 int winnr = 0;
9166#endif
9167 buf_T *buf;
9168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009169 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009171 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_WINDOWS
9173 for (wp = firstwin; wp; wp = wp->w_next)
9174 {
9175 ++winnr;
9176 if (wp->w_buffer == buf)
9177 break;
9178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#endif
9183 --emsg_off;
9184}
9185
9186/*
9187 * "byte2line(byte)" function
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009190f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#else
9197 long boff = 0;
9198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 (linenr_T)0, &boff);
9205#endif
9206}
9207
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009208 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009209byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009212 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009213{
9214#ifdef FEAT_MBYTE
9215 char_u *t;
9216#endif
9217 char_u *str;
9218 long idx;
9219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 str = get_tv_string_chk(&argvars[0]);
9221 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009222 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009224 return;
9225
9226#ifdef FEAT_MBYTE
9227 t = str;
9228 for ( ; idx > 0; idx--)
9229 {
9230 if (*t == NUL) /* EOL reached */
9231 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009232 if (enc_utf8 && comp)
9233 t += utf_ptr2len(t);
9234 else
9235 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009236 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009237 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009239 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009241#endif
9242}
9243
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009244/*
9245 * "byteidx()" function
9246 */
9247 static void
9248f_byteidx(argvars, rettv)
9249 typval_T *argvars;
9250 typval_T *rettv;
9251{
9252 byteidx(argvars, rettv, FALSE);
9253}
9254
9255/*
9256 * "byteidxcomp()" function
9257 */
9258 static void
9259f_byteidxcomp(argvars, rettv)
9260 typval_T *argvars;
9261 typval_T *rettv;
9262{
9263 byteidx(argvars, rettv, TRUE);
9264}
9265
Bram Moolenaardb913952012-06-29 12:54:53 +02009266 int
9267func_call(name, args, selfdict, rettv)
9268 char_u *name;
9269 typval_T *args;
9270 dict_T *selfdict;
9271 typval_T *rettv;
9272{
9273 listitem_T *item;
9274 typval_T argv[MAX_FUNC_ARGS + 1];
9275 int argc = 0;
9276 int dummy;
9277 int r = 0;
9278
9279 for (item = args->vval.v_list->lv_first; item != NULL;
9280 item = item->li_next)
9281 {
9282 if (argc == MAX_FUNC_ARGS)
9283 {
9284 EMSG(_("E699: Too many arguments"));
9285 break;
9286 }
9287 /* Make a copy of each argument. This is needed to be able to set
9288 * v_lock to VAR_FIXED in the copy without changing the original list.
9289 */
9290 copy_tv(&item->li_tv, &argv[argc++]);
9291 }
9292
9293 if (item == NULL)
9294 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9295 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9296 &dummy, TRUE, selfdict);
9297
9298 /* Free the arguments. */
9299 while (argc > 0)
9300 clear_tv(&argv[--argc]);
9301
9302 return r;
9303}
9304
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009305/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009306 * "call(func, arglist)" function
9307 */
9308 static void
9309f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 typval_T *argvars;
9311 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009312{
9313 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009315
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009316 if (argvars[1].v_type != VAR_LIST)
9317 {
9318 EMSG(_(e_listreq));
9319 return;
9320 }
9321 if (argvars[1].vval.v_list == NULL)
9322 return;
9323
9324 if (argvars[0].v_type == VAR_FUNC)
9325 func = argvars[0].vval.v_string;
9326 else
9327 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 if (*func == NUL)
9329 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009330
Bram Moolenaare9a41262005-01-15 22:18:47 +00009331 if (argvars[2].v_type != VAR_UNKNOWN)
9332 {
9333 if (argvars[2].v_type != VAR_DICT)
9334 {
9335 EMSG(_(e_dictreq));
9336 return;
9337 }
9338 selfdict = argvars[2].vval.v_dict;
9339 }
9340
Bram Moolenaardb913952012-06-29 12:54:53 +02009341 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009342}
9343
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009344#ifdef FEAT_FLOAT
9345/*
9346 * "ceil({float})" function
9347 */
9348 static void
9349f_ceil(argvars, rettv)
9350 typval_T *argvars;
9351 typval_T *rettv;
9352{
9353 float_T f;
9354
9355 rettv->v_type = VAR_FLOAT;
9356 if (get_float_arg(argvars, &f) == OK)
9357 rettv->vval.v_float = ceil(f);
9358 else
9359 rettv->vval.v_float = 0.0;
9360}
9361#endif
9362
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009363/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009364 * "changenr()" function
9365 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009366 static void
9367f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009368 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009369 typval_T *rettv;
9370{
9371 rettv->vval.v_number = curbuf->b_u_seq_cur;
9372}
9373
9374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 * "char2nr(string)" function
9376 */
9377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 typval_T *argvars;
9380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382#ifdef FEAT_MBYTE
9383 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009384 {
9385 int utf8 = 0;
9386
9387 if (argvars[1].v_type != VAR_UNKNOWN)
9388 utf8 = get_tv_number_chk(&argvars[1], NULL);
9389
9390 if (utf8)
9391 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9392 else
9393 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 else
9396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
9400/*
9401 * "cindent(lnum)" function
9402 */
9403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408#ifdef FEAT_CINDENT
9409 pos_T pos;
9410 linenr_T lnum;
9411
9412 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9415 {
9416 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 curwin->w_cursor = pos;
9419 }
9420 else
9421#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423}
9424
9425/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009426 * "clearmatches()" function
9427 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009428 static void
9429f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009430 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009431 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009432{
9433#ifdef FEAT_SEARCH_EXTRA
9434 clear_matches(curwin);
9435#endif
9436}
9437
9438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 * "col(string)" function
9440 */
9441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *argvars;
9444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 colnr_T col = 0;
9447 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009448 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 fp = var2fpos(&argvars[0], FALSE, &fnum);
9451 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 {
9453 if (fp->col == MAXCOL)
9454 {
9455 /* '> can be MAXCOL, get the length of the line then */
9456 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009457 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 else
9459 col = MAXCOL;
9460 }
9461 else
9462 {
9463 col = fp->col + 1;
9464#ifdef FEAT_VIRTUALEDIT
9465 /* col(".") when the cursor is on the NUL at the end of the line
9466 * because of "coladd" can be seen as an extra column. */
9467 if (virtual_active() && fp == &curwin->w_cursor)
9468 {
9469 char_u *p = ml_get_cursor();
9470
9471 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9472 curwin->w_virtcol - curwin->w_cursor.coladd))
9473 {
9474# ifdef FEAT_MBYTE
9475 int l;
9476
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009477 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 col += l;
9479# else
9480 if (*p != NUL && p[1] == NUL)
9481 ++col;
9482# endif
9483 }
9484 }
9485#endif
9486 }
9487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489}
9490
Bram Moolenaar572cb562005-08-05 21:35:02 +00009491#if defined(FEAT_INS_EXPAND)
9492/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009493 * "complete()" function
9494 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009495 static void
9496f_complete(argvars, rettv)
9497 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009498 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009499{
9500 int startcol;
9501
9502 if ((State & INSERT) == 0)
9503 {
9504 EMSG(_("E785: complete() can only be used in Insert mode"));
9505 return;
9506 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009507
9508 /* Check for undo allowed here, because if something was already inserted
9509 * the line was already saved for undo and this check isn't done. */
9510 if (!undo_allowed())
9511 return;
9512
Bram Moolenaarade00832006-03-10 21:46:58 +00009513 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9514 {
9515 EMSG(_(e_invarg));
9516 return;
9517 }
9518
9519 startcol = get_tv_number_chk(&argvars[0], NULL);
9520 if (startcol <= 0)
9521 return;
9522
9523 set_completion(startcol - 1, argvars[1].vval.v_list);
9524}
9525
9526/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009527 * "complete_add()" function
9528 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529 static void
9530f_complete_add(argvars, rettv)
9531 typval_T *argvars;
9532 typval_T *rettv;
9533{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009534 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009535}
9536
9537/*
9538 * "complete_check()" function
9539 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009540 static void
9541f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009542 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 typval_T *rettv;
9544{
9545 int saved = RedrawingDisabled;
9546
9547 RedrawingDisabled = 0;
9548 ins_compl_check_keys(0);
9549 rettv->vval.v_number = compl_interrupted;
9550 RedrawingDisabled = saved;
9551}
9552#endif
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554/*
9555 * "confirm(message, buttons[, default [, type]])" function
9556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009559 typval_T *argvars UNUSED;
9560 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9563 char_u *message;
9564 char_u *buttons = NULL;
9565 char_u buf[NUMBUFLEN];
9566 char_u buf2[NUMBUFLEN];
9567 int def = 1;
9568 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 char_u *typestr;
9570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 message = get_tv_string_chk(&argvars[0]);
9573 if (message == NULL)
9574 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9578 if (buttons == NULL)
9579 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9586 if (typestr == NULL)
9587 error = TRUE;
9588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 switch (TOUPPER_ASC(*typestr))
9591 {
9592 case 'E': type = VIM_ERROR; break;
9593 case 'Q': type = VIM_QUESTION; break;
9594 case 'I': type = VIM_INFO; break;
9595 case 'W': type = VIM_WARNING; break;
9596 case 'G': type = VIM_GENERIC; break;
9597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 }
9599 }
9600 }
9601 }
9602
9603 if (buttons == NULL || *buttons == NUL)
9604 buttons = (char_u *)_("&Ok");
9605
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009606 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009607 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009608 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609#endif
9610}
9611
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612/*
9613 * "copy()" function
9614 */
9615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 typval_T *argvars;
9618 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009620 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009623#ifdef FEAT_FLOAT
9624/*
9625 * "cos()" function
9626 */
9627 static void
9628f_cos(argvars, rettv)
9629 typval_T *argvars;
9630 typval_T *rettv;
9631{
9632 float_T f;
9633
9634 rettv->v_type = VAR_FLOAT;
9635 if (get_float_arg(argvars, &f) == OK)
9636 rettv->vval.v_float = cos(f);
9637 else
9638 rettv->vval.v_float = 0.0;
9639}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009640
9641/*
9642 * "cosh()" function
9643 */
9644 static void
9645f_cosh(argvars, rettv)
9646 typval_T *argvars;
9647 typval_T *rettv;
9648{
9649 float_T f;
9650
9651 rettv->v_type = VAR_FLOAT;
9652 if (get_float_arg(argvars, &f) == OK)
9653 rettv->vval.v_float = cosh(f);
9654 else
9655 rettv->vval.v_float = 0.0;
9656}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009657#endif
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660 * "count()" function
9661 */
9662 static void
9663f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009664 typval_T *argvars;
9665 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009666{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 long n = 0;
9668 int ic = FALSE;
9669
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009672 listitem_T *li;
9673 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009675
Bram Moolenaare9a41262005-01-15 22:18:47 +00009676 if ((l = argvars[0].vval.v_list) != NULL)
9677 {
9678 li = l->lv_first;
9679 if (argvars[2].v_type != VAR_UNKNOWN)
9680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009681 int error = FALSE;
9682
9683 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009684 if (argvars[3].v_type != VAR_UNKNOWN)
9685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009686 idx = get_tv_number_chk(&argvars[3], &error);
9687 if (!error)
9688 {
9689 li = list_find(l, idx);
9690 if (li == NULL)
9691 EMSGN(_(e_listidx), idx);
9692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 if (error)
9695 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009696 }
9697
9698 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009699 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 ++n;
9701 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009702 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009703 else if (argvars[0].v_type == VAR_DICT)
9704 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 int todo;
9706 dict_T *d;
9707 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009708
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009709 if ((d = argvars[0].vval.v_dict) != NULL)
9710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 int error = FALSE;
9712
Bram Moolenaare9a41262005-01-15 22:18:47 +00009713 if (argvars[2].v_type != VAR_UNKNOWN)
9714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 if (argvars[3].v_type != VAR_UNKNOWN)
9717 EMSG(_(e_invarg));
9718 }
9719
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009720 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009722 {
9723 if (!HASHITEM_EMPTY(hi))
9724 {
9725 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009726 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009727 ++n;
9728 }
9729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009730 }
9731 }
9732 else
9733 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009734 rettv->vval.v_number = n;
9735}
9736
9737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9739 *
9740 * Checks the existence of a cscope connection.
9741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009744 typval_T *argvars UNUSED;
9745 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747#ifdef FEAT_CSCOPE
9748 int num = 0;
9749 char_u *dbpath = NULL;
9750 char_u *prepend = NULL;
9751 char_u buf[NUMBUFLEN];
9752
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009753 if (argvars[0].v_type != VAR_UNKNOWN
9754 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 num = (int)get_tv_number(&argvars[0]);
9757 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009758 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 }
9761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764}
9765
9766/*
9767 * "cursor(lnum, col)" function
9768 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009769 * Moves the cursor to the specified line and column.
9770 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 typval_T *argvars;
9775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009778#ifdef FEAT_VIRTUALEDIT
9779 long coladd = 0;
9780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009782 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009783 if (argvars[1].v_type == VAR_UNKNOWN)
9784 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009785 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009786
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009787 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009788 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009789 line = pos.lnum;
9790 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009791#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009792 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009793#endif
9794 }
9795 else
9796 {
9797 line = get_tv_lnum(argvars);
9798 col = get_tv_number_chk(&argvars[1], NULL);
9799#ifdef FEAT_VIRTUALEDIT
9800 if (argvars[2].v_type != VAR_UNKNOWN)
9801 coladd = get_tv_number_chk(&argvars[2], NULL);
9802#endif
9803 }
9804 if (line < 0 || col < 0
9805#ifdef FEAT_VIRTUALEDIT
9806 || coladd < 0
9807#endif
9808 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 if (line > 0)
9811 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (col > 0)
9813 curwin->w_cursor.col = col - 1;
9814#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009815 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816#endif
9817
9818 /* Make sure the cursor is in a valid position. */
9819 check_cursor();
9820#ifdef FEAT_MBYTE
9821 /* Correct cursor for multi-byte character. */
9822 if (has_mbyte)
9823 mb_adjust_cursor();
9824#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009825
9826 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828}
9829
9830/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009831 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 */
9833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 typval_T *argvars;
9836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009838 int noref = 0;
9839
9840 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009842 if (noref < 0 || noref > 1)
9843 EMSG(_(e_invarg));
9844 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009845 {
9846 current_copyID += COPYID_INC;
9847 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849}
9850
9851/*
9852 * "delete()" function
9853 */
9854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009856 typval_T *argvars;
9857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858{
9859 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863}
9864
9865/*
9866 * "did_filetype()" function
9867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009870 typval_T *argvars UNUSED;
9871 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875#endif
9876}
9877
9878/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879 * "diff_filler()" function
9880 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009883 typval_T *argvars UNUSED;
9884 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009885{
9886#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888#endif
9889}
9890
9891/*
9892 * "diff_hlID()" function
9893 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009896 typval_T *argvars UNUSED;
9897 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009898{
9899#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 static linenr_T prev_lnum = 0;
9902 static int changedtick = 0;
9903 static int fnum = 0;
9904 static int change_start = 0;
9905 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009906 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009907 int filler_lines;
9908 int col;
9909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 if (lnum < 0) /* ignore type error in {lnum} arg */
9911 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009912 if (lnum != prev_lnum
9913 || changedtick != curbuf->b_changedtick
9914 || fnum != curbuf->b_fnum)
9915 {
9916 /* New line, buffer, change: need to get the values. */
9917 filler_lines = diff_check(curwin, lnum);
9918 if (filler_lines < 0)
9919 {
9920 if (filler_lines == -1)
9921 {
9922 change_start = MAXCOL;
9923 change_end = -1;
9924 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9925 hlID = HLF_ADD; /* added line */
9926 else
9927 hlID = HLF_CHD; /* changed line */
9928 }
9929 else
9930 hlID = HLF_ADD; /* added line */
9931 }
9932 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009933 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009934 prev_lnum = lnum;
9935 changedtick = curbuf->b_changedtick;
9936 fnum = curbuf->b_fnum;
9937 }
9938
9939 if (hlID == HLF_CHD || hlID == HLF_TXD)
9940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009941 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009942 if (col >= change_start && col <= change_end)
9943 hlID = HLF_TXD; /* changed text */
9944 else
9945 hlID = HLF_CHD; /* changed line */
9946 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009947 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009948#endif
9949}
9950
9951/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009952 * "empty({expr})" function
9953 */
9954 static void
9955f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 typval_T *argvars;
9957 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009958{
9959 int n;
9960
9961 switch (argvars[0].v_type)
9962 {
9963 case VAR_STRING:
9964 case VAR_FUNC:
9965 n = argvars[0].vval.v_string == NULL
9966 || *argvars[0].vval.v_string == NUL;
9967 break;
9968 case VAR_NUMBER:
9969 n = argvars[0].vval.v_number == 0;
9970 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009971#ifdef FEAT_FLOAT
9972 case VAR_FLOAT:
9973 n = argvars[0].vval.v_float == 0.0;
9974 break;
9975#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009976 case VAR_LIST:
9977 n = argvars[0].vval.v_list == NULL
9978 || argvars[0].vval.v_list->lv_first == NULL;
9979 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 case VAR_DICT:
9981 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009982 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009984 default:
9985 EMSG2(_(e_intern2), "f_empty()");
9986 n = 0;
9987 }
9988
9989 rettv->vval.v_number = n;
9990}
9991
9992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 * "escape({string}, {chars})" function
9994 */
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 char_u buf[NUMBUFLEN];
10001
Bram Moolenaar758711c2005-02-02 23:11:38 +000010002 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10003 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005}
10006
10007/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008 * "eval()" function
10009 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010010 static void
10011f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 typval_T *argvars;
10013 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010014{
10015 char_u *s;
10016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010017 s = get_tv_string_chk(&argvars[0]);
10018 if (s != NULL)
10019 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10022 {
10023 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010026 else if (*s != NUL)
10027 EMSG(_(e_trailing));
10028}
10029
10030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 * "eventhandler()" function
10032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039}
10040
10041/*
10042 * "executable()" function
10043 */
10044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 typval_T *argvars;
10047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050}
10051
10052/*
10053 * "exists()" function
10054 */
10055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010057 typval_T *argvars;
10058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 char_u *p;
10061 char_u *name;
10062 int n = FALSE;
10063 int len = 0;
10064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if (*p == '$') /* environment variable */
10067 {
10068 /* first try "normal" environment variables (fast) */
10069 if (mch_getenv(p + 1) != NULL)
10070 n = TRUE;
10071 else
10072 {
10073 /* try expanding things like $VIM and ${HOME} */
10074 p = expand_env_save(p);
10075 if (p != NULL && *p != '$')
10076 n = TRUE;
10077 vim_free(p);
10078 }
10079 }
10080 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010083 if (*skipwhite(p) != NUL)
10084 n = FALSE; /* trailing garbage */
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 else if (*p == '*') /* internal or user defined function */
10087 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 }
10090 else if (*p == ':')
10091 {
10092 n = cmd_exists(p + 1);
10093 }
10094 else if (*p == '#')
10095 {
10096#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010097 if (p[1] == '#')
10098 n = autocmd_supported(p + 2);
10099 else
10100 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101#endif
10102 }
10103 else /* internal variable */
10104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010105 char_u *tofree;
10106 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010108 /* get_name_len() takes care of expanding curly braces */
10109 name = p;
10110 len = get_name_len(&p, &tofree, TRUE, FALSE);
10111 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010113 if (tofree != NULL)
10114 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010115 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010116 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010118 /* handle d.key, l[idx], f(expr) */
10119 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10120 if (n)
10121 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010124 if (*p != NUL)
10125 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010127 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
10129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131}
10132
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010133#ifdef FEAT_FLOAT
10134/*
10135 * "exp()" function
10136 */
10137 static void
10138f_exp(argvars, rettv)
10139 typval_T *argvars;
10140 typval_T *rettv;
10141{
10142 float_T f;
10143
10144 rettv->v_type = VAR_FLOAT;
10145 if (get_float_arg(argvars, &f) == OK)
10146 rettv->vval.v_float = exp(f);
10147 else
10148 rettv->vval.v_float = 0.0;
10149}
10150#endif
10151
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152/*
10153 * "expand()" function
10154 */
10155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
10160 char_u *s;
10161 int len;
10162 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010163 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010166 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010169 if (argvars[1].v_type != VAR_UNKNOWN
10170 && argvars[2].v_type != VAR_UNKNOWN
10171 && get_tv_number_chk(&argvars[2], &error)
10172 && !error)
10173 {
10174 rettv->v_type = VAR_LIST;
10175 rettv->vval.v_list = NULL;
10176 }
10177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (*s == '%' || *s == '#' || *s == '<')
10180 {
10181 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010182 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010184 if (rettv->v_type == VAR_LIST)
10185 {
10186 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10187 list_append_string(rettv->vval.v_list, result, -1);
10188 else
10189 vim_free(result);
10190 }
10191 else
10192 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 }
10194 else
10195 {
10196 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010197 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 if (argvars[1].v_type != VAR_UNKNOWN
10199 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010200 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010201 if (!error)
10202 {
10203 ExpandInit(&xpc);
10204 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010205 if (p_wic)
10206 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010207 if (rettv->v_type == VAR_STRING)
10208 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10209 options, WILD_ALL);
10210 else if (rettv_list_alloc(rettv) != FAIL)
10211 {
10212 int i;
10213
10214 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10215 for (i = 0; i < xpc.xp_numfiles; i++)
10216 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10217 ExpandCleanup(&xpc);
10218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 }
10220 else
10221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 }
10223}
10224
10225/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010226 * Go over all entries in "d2" and add them to "d1".
10227 * When "action" is "error" then a duplicate key is an error.
10228 * When "action" is "force" then a duplicate key is overwritten.
10229 * Otherwise duplicate keys are ignored ("action" is "keep").
10230 */
10231 void
10232dict_extend(d1, d2, action)
10233 dict_T *d1;
10234 dict_T *d2;
10235 char_u *action;
10236{
10237 dictitem_T *di1;
10238 hashitem_T *hi2;
10239 int todo;
10240
10241 todo = (int)d2->dv_hashtab.ht_used;
10242 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10243 {
10244 if (!HASHITEM_EMPTY(hi2))
10245 {
10246 --todo;
10247 di1 = dict_find(d1, hi2->hi_key, -1);
10248 if (d1->dv_scope != 0)
10249 {
10250 /* Disallow replacing a builtin function in l: and g:.
10251 * Check the key to be valid when adding to any
10252 * scope. */
10253 if (d1->dv_scope == VAR_DEF_SCOPE
10254 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10255 && var_check_func_name(hi2->hi_key,
10256 di1 == NULL))
10257 break;
10258 if (!valid_varname(hi2->hi_key))
10259 break;
10260 }
10261 if (di1 == NULL)
10262 {
10263 di1 = dictitem_copy(HI2DI(hi2));
10264 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10265 dictitem_free(di1);
10266 }
10267 else if (*action == 'e')
10268 {
10269 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10270 break;
10271 }
10272 else if (*action == 'f' && HI2DI(hi2) != di1)
10273 {
10274 clear_tv(&di1->di_tv);
10275 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10276 }
10277 }
10278 }
10279}
10280
10281/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010282 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010284 */
10285 static void
10286f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *argvars;
10288 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010289{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010290 char *arg_errmsg = N_("extend() argument");
10291
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010293 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010294 list_T *l1, *l2;
10295 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010298
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 l1 = argvars[0].vval.v_list;
10300 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010301 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010302 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303 {
10304 if (argvars[2].v_type != VAR_UNKNOWN)
10305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 before = get_tv_number_chk(&argvars[2], &error);
10307 if (error)
10308 return; /* type error; errmsg already given */
10309
Bram Moolenaar758711c2005-02-02 23:11:38 +000010310 if (before == l1->lv_len)
10311 item = NULL;
10312 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010314 item = list_find(l1, before);
10315 if (item == NULL)
10316 {
10317 EMSGN(_(e_listidx), before);
10318 return;
10319 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 }
10321 }
10322 else
10323 item = NULL;
10324 list_extend(l1, l2, item);
10325
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 copy_tv(&argvars[0], rettv);
10327 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010329 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10330 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010331 dict_T *d1, *d2;
10332 char_u *action;
10333 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010334
10335 d1 = argvars[0].vval.v_dict;
10336 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010337 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010338 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010339 {
10340 /* Check the third argument. */
10341 if (argvars[2].v_type != VAR_UNKNOWN)
10342 {
10343 static char *(av[]) = {"keep", "force", "error"};
10344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 action = get_tv_string_chk(&argvars[2]);
10346 if (action == NULL)
10347 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 for (i = 0; i < 3; ++i)
10349 if (STRCMP(action, av[i]) == 0)
10350 break;
10351 if (i == 3)
10352 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010353 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 return;
10355 }
10356 }
10357 else
10358 action = (char_u *)"force";
10359
Bram Moolenaara9922d62013-05-30 13:01:18 +020010360 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 copy_tv(&argvars[0], rettv);
10363 }
10364 }
10365 else
10366 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010367}
10368
10369/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370 * "feedkeys()" function
10371 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010372 static void
10373f_feedkeys(argvars, rettv)
10374 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010375 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010376{
10377 int remap = TRUE;
10378 char_u *keys, *flags;
10379 char_u nbuf[NUMBUFLEN];
10380 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010381 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010382
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010383 /* This is not allowed in the sandbox. If the commands would still be
10384 * executed in the sandbox it would be OK, but it probably happens later,
10385 * when "sandbox" is no longer set. */
10386 if (check_secure())
10387 return;
10388
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010389 keys = get_tv_string(&argvars[0]);
10390 if (*keys != NUL)
10391 {
10392 if (argvars[1].v_type != VAR_UNKNOWN)
10393 {
10394 flags = get_tv_string_buf(&argvars[1], nbuf);
10395 for ( ; *flags != NUL; ++flags)
10396 {
10397 switch (*flags)
10398 {
10399 case 'n': remap = FALSE; break;
10400 case 'm': remap = TRUE; break;
10401 case 't': typed = TRUE; break;
10402 }
10403 }
10404 }
10405
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010406 /* Need to escape K_SPECIAL and CSI before putting the string in the
10407 * typeahead buffer. */
10408 keys_esc = vim_strsave_escape_csi(keys);
10409 if (keys_esc != NULL)
10410 {
10411 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010412 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010413 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010414 if (vgetc_busy)
10415 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010416 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010417 }
10418}
10419
10420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 * "filereadable()" function
10422 */
10423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010428 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 char_u *p;
10430 int n;
10431
Bram Moolenaarc236c162008-07-13 17:41:49 +000010432#ifndef O_NONBLOCK
10433# define O_NONBLOCK 0
10434#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010436 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10437 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 {
10439 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010440 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 }
10442 else
10443 n = FALSE;
10444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446}
10447
10448/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010449 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 * rights to write into.
10451 */
10452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010454 typval_T *argvars;
10455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010457 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458}
10459
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010460static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461
10462 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010463findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010466 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010467{
10468#ifdef FEAT_SEARCHPATH
10469 char_u *fname;
10470 char_u *fresult = NULL;
10471 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10472 char_u *p;
10473 char_u pathbuf[NUMBUFLEN];
10474 int count = 1;
10475 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010476 int error = FALSE;
10477#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010479 rettv->vval.v_string = NULL;
10480 rettv->v_type = VAR_STRING;
10481
10482#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010485 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010487 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10488 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010489 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 else
10491 {
10492 if (*p != NUL)
10493 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010496 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498 }
10499
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010500 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10501 error = TRUE;
10502
10503 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 do
10506 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010507 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010508 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 fresult = find_file_in_path_option(first ? fname : NULL,
10510 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010511 0, first, path,
10512 find_what,
10513 curbuf->b_ffname,
10514 find_what == FINDFILE_DIR
10515 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010517
10518 if (fresult != NULL && rettv->v_type == VAR_LIST)
10519 list_append_string(rettv->vval.v_list, fresult, -1);
10520
10521 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010523
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010524 if (rettv->v_type == VAR_STRING)
10525 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010526#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010527}
10528
Bram Moolenaar33570922005-01-25 22:26:29 +000010529static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10530static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010531
10532/*
10533 * Implementation of map() and filter().
10534 */
10535 static void
10536filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539 int map;
10540{
10541 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010542 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 listitem_T *li, *nli;
10544 list_T *l = NULL;
10545 dictitem_T *di;
10546 hashtab_T *ht;
10547 hashitem_T *hi;
10548 dict_T *d = NULL;
10549 typval_T save_val;
10550 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010552 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010553 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10554 char *arg_errmsg = (map ? N_("map() argument")
10555 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010556 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010557 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010558
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010561 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010562 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 return;
10564 }
10565 else if (argvars[0].v_type == VAR_DICT)
10566 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010567 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010568 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569 return;
10570 }
10571 else
10572 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010573 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 return;
10575 }
10576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 expr = get_tv_string_buf_chk(&argvars[1], buf);
10578 /* On type errors, the preceding call has already displayed an error
10579 * message. Avoid a misleading error message for an empty string that
10580 * was not passed as argument. */
10581 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 prepare_vimvar(VV_VAL, &save_val);
10584 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010585
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010586 /* We reset "did_emsg" to be able to detect whether an error
10587 * occurred during evaluation of the expression. */
10588 save_did_emsg = did_emsg;
10589 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010590
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010591 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 vimvars[VV_KEY].vv_type = VAR_STRING;
10595
10596 ht = &d->dv_hashtab;
10597 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010598 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 if (!HASHITEM_EMPTY(hi))
10602 {
10603 --todo;
10604 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010605 if (tv_check_lock(di->di_tv.v_lock,
10606 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 break;
10608 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010609 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010610 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 break;
10612 if (!map && rem)
10613 dictitem_remove(d, di);
10614 clear_tv(&vimvars[VV_KEY].vv_tv);
10615 }
10616 }
10617 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 }
10619 else
10620 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010621 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 for (li = l->lv_first; li != NULL; li = nli)
10624 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010625 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010626 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010628 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010629 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010630 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010631 break;
10632 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010634 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010635 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010636 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010637
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010638 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010640
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010641 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643
10644 copy_tv(&argvars[0], rettv);
10645}
10646
10647 static int
10648filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 char_u *expr;
10651 int map;
10652 int *remp;
10653{
Bram Moolenaar33570922005-01-25 22:26:29 +000010654 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010656 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010657
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 s = expr;
10660 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010661 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010662 if (*s != NUL) /* check for trailing chars after expr */
10663 {
10664 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010665 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 }
10667 if (map)
10668 {
10669 /* map(): replace the list item value */
10670 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010671 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 *tv = rettv;
10673 }
10674 else
10675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010676 int error = FALSE;
10677
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010680 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 /* On type error, nothing has been removed; return FAIL to stop the
10682 * loop. The error message was given by get_tv_number_chk(). */
10683 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010684 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010686 retval = OK;
10687theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010688 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010689 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010690}
10691
10692/*
10693 * "filter()" function
10694 */
10695 static void
10696f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010699{
10700 filter_map(argvars, rettv, FALSE);
10701}
10702
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704 * "finddir({fname}[, {path}[, {count}]])" function
10705 */
10706 static void
10707f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010708 typval_T *argvars;
10709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010711 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010712}
10713
10714/*
10715 * "findfile({fname}[, {path}[, {count}]])" function
10716 */
10717 static void
10718f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010721{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010722 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010723}
10724
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010725#ifdef FEAT_FLOAT
10726/*
10727 * "float2nr({float})" function
10728 */
10729 static void
10730f_float2nr(argvars, rettv)
10731 typval_T *argvars;
10732 typval_T *rettv;
10733{
10734 float_T f;
10735
10736 if (get_float_arg(argvars, &f) == OK)
10737 {
10738 if (f < -0x7fffffff)
10739 rettv->vval.v_number = -0x7fffffff;
10740 else if (f > 0x7fffffff)
10741 rettv->vval.v_number = 0x7fffffff;
10742 else
10743 rettv->vval.v_number = (varnumber_T)f;
10744 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010745}
10746
10747/*
10748 * "floor({float})" function
10749 */
10750 static void
10751f_floor(argvars, rettv)
10752 typval_T *argvars;
10753 typval_T *rettv;
10754{
10755 float_T f;
10756
10757 rettv->v_type = VAR_FLOAT;
10758 if (get_float_arg(argvars, &f) == OK)
10759 rettv->vval.v_float = floor(f);
10760 else
10761 rettv->vval.v_float = 0.0;
10762}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010763
10764/*
10765 * "fmod()" function
10766 */
10767 static void
10768f_fmod(argvars, rettv)
10769 typval_T *argvars;
10770 typval_T *rettv;
10771{
10772 float_T fx, fy;
10773
10774 rettv->v_type = VAR_FLOAT;
10775 if (get_float_arg(argvars, &fx) == OK
10776 && get_float_arg(&argvars[1], &fy) == OK)
10777 rettv->vval.v_float = fmod(fx, fy);
10778 else
10779 rettv->vval.v_float = 0.0;
10780}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010781#endif
10782
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010784 * "fnameescape({string})" function
10785 */
10786 static void
10787f_fnameescape(argvars, rettv)
10788 typval_T *argvars;
10789 typval_T *rettv;
10790{
10791 rettv->vval.v_string = vim_strsave_fnameescape(
10792 get_tv_string(&argvars[0]), FALSE);
10793 rettv->v_type = VAR_STRING;
10794}
10795
10796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 * "fnamemodify({fname}, {mods})" function
10798 */
10799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803{
10804 char_u *fname;
10805 char_u *mods;
10806 int usedlen = 0;
10807 int len;
10808 char_u *fbuf = NULL;
10809 char_u buf[NUMBUFLEN];
10810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 fname = get_tv_string_chk(&argvars[0]);
10812 mods = get_tv_string_buf_chk(&argvars[1], buf);
10813 if (fname == NULL || mods == NULL)
10814 fname = NULL;
10815 else
10816 {
10817 len = (int)STRLEN(fname);
10818 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 vim_free(fbuf);
10827}
10828
Bram Moolenaar33570922005-01-25 22:26:29 +000010829static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830
10831/*
10832 * "foldclosed()" function
10833 */
10834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010836 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010837 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010838 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839{
10840#ifdef FEAT_FOLDING
10841 linenr_T lnum;
10842 linenr_T first, last;
10843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10846 {
10847 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10848 {
10849 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 return;
10854 }
10855 }
10856#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858}
10859
10860/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861 * "foldclosed()" function
10862 */
10863 static void
10864f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 typval_T *argvars;
10866 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010867{
10868 foldclosed_both(argvars, rettv, FALSE);
10869}
10870
10871/*
10872 * "foldclosedend()" function
10873 */
10874 static void
10875f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010876 typval_T *argvars;
10877 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878{
10879 foldclosed_both(argvars, rettv, TRUE);
10880}
10881
10882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 * "foldlevel()" function
10884 */
10885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010887 typval_T *argvars UNUSED;
10888 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
10890#ifdef FEAT_FOLDING
10891 linenr_T lnum;
10892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897}
10898
10899/*
10900 * "foldtext()" function
10901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907#ifdef FEAT_FOLDING
10908 linenr_T lnum;
10909 char_u *s;
10910 char_u *r;
10911 int len;
10912 char *txt;
10913#endif
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->v_type = VAR_STRING;
10916 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10919 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10920 <= curbuf->b_ml.ml_line_count
10921 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 {
10923 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10925 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 {
10927 if (!linewhite(lnum))
10928 break;
10929 ++lnum;
10930 }
10931
10932 /* Find interesting text in this line. */
10933 s = skipwhite(ml_get(lnum));
10934 /* skip C comment-start */
10935 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010938 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010940 {
10941 s = skipwhite(ml_get(lnum + 1));
10942 if (*s == '*')
10943 s = skipwhite(s + 1);
10944 }
10945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 txt = _("+-%s%3ld lines: ");
10947 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 + 20 /* for %3ld */
10950 + STRLEN(s))); /* concatenated */
10951 if (r != NULL)
10952 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10954 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10955 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 len = (int)STRLEN(r);
10957 STRCAT(r, s);
10958 /* remove 'foldmarker' and 'commentstring' */
10959 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 }
10962 }
10963#endif
10964}
10965
10966/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010967 * "foldtextresult(lnum)" function
10968 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010970f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010971 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010973{
10974#ifdef FEAT_FOLDING
10975 linenr_T lnum;
10976 char_u *text;
10977 char_u buf[51];
10978 foldinfo_T foldinfo;
10979 int fold_count;
10980#endif
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->v_type = VAR_STRING;
10983 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010984#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010986 /* treat illegal types and illegal string values for {lnum} the same */
10987 if (lnum < 0)
10988 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010989 fold_count = foldedCount(curwin, lnum, &foldinfo);
10990 if (fold_count > 0)
10991 {
10992 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10993 &foldinfo, buf);
10994 if (text == buf)
10995 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010997 }
10998#endif
10999}
11000
11001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 * "foreground()" function
11003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011006 typval_T *argvars UNUSED;
11007 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009#ifdef FEAT_GUI
11010 if (gui.in_use)
11011 gui_mch_set_foreground();
11012#else
11013# ifdef WIN32
11014 win32_set_foreground();
11015# endif
11016#endif
11017}
11018
11019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020 * "function()" function
11021 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011024 typval_T *argvars;
11025 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011026{
11027 char_u *s;
11028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011030 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011031 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011032 /* Don't check an autoload name for existence here. */
11033 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011034 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011035 else
11036 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011037 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011038 {
11039 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011040 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011041
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011042 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11043 * also be called from another script. Using trans_function_name()
11044 * would also work, but some plugins depend on the name being
11045 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011046 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011047 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011048 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011049 if (rettv->vval.v_string != NULL)
11050 {
11051 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011052 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011053 }
11054 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011055 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011056 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011058 }
11059}
11060
11061/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011062 * "garbagecollect()" function
11063 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011064 static void
11065f_garbagecollect(argvars, rettv)
11066 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011067 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011068{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011069 /* This is postponed until we are back at the toplevel, because we may be
11070 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11071 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011072
11073 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11074 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011075}
11076
11077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078 * "get()" function
11079 */
11080 static void
11081f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 typval_T *argvars;
11083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084{
Bram Moolenaar33570922005-01-25 22:26:29 +000011085 listitem_T *li;
11086 list_T *l;
11087 dictitem_T *di;
11088 dict_T *d;
11089 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011090
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011092 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 int error = FALSE;
11096
11097 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11098 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102 else if (argvars[0].v_type == VAR_DICT)
11103 {
11104 if ((d = argvars[0].vval.v_dict) != NULL)
11105 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011106 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 if (di != NULL)
11108 tv = &di->di_tv;
11109 }
11110 }
11111 else
11112 EMSG2(_(e_listdictarg), "get()");
11113
11114 if (tv == NULL)
11115 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011116 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 copy_tv(&argvars[2], rettv);
11118 }
11119 else
11120 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121}
11122
Bram Moolenaar342337a2005-07-21 21:11:17 +000011123static 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 +000011124
11125/*
11126 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011127 * Return a range (from start to end) of lines in rettv from the specified
11128 * buffer.
11129 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011130 */
11131 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011132get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011133 buf_T *buf;
11134 linenr_T start;
11135 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011136 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011137 typval_T *rettv;
11138{
11139 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011140
Bram Moolenaar959a1432013-12-14 12:17:38 +010011141 rettv->v_type = VAR_STRING;
11142 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011143 if (retlist && rettv_list_alloc(rettv) == FAIL)
11144 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145
11146 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11147 return;
11148
11149 if (!retlist)
11150 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011151 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11152 p = ml_get_buf(buf, start, FALSE);
11153 else
11154 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011155 rettv->vval.v_string = vim_strsave(p);
11156 }
11157 else
11158 {
11159 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011160 return;
11161
11162 if (start < 1)
11163 start = 1;
11164 if (end > buf->b_ml.ml_line_count)
11165 end = buf->b_ml.ml_line_count;
11166 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011167 if (list_append_string(rettv->vval.v_list,
11168 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011169 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011170 }
11171}
11172
11173/*
11174 * "getbufline()" function
11175 */
11176 static void
11177f_getbufline(argvars, rettv)
11178 typval_T *argvars;
11179 typval_T *rettv;
11180{
11181 linenr_T lnum;
11182 linenr_T end;
11183 buf_T *buf;
11184
11185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011187 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011188 --emsg_off;
11189
Bram Moolenaar661b1822005-07-28 22:36:45 +000011190 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011191 if (argvars[2].v_type == VAR_UNKNOWN)
11192 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011193 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011194 end = get_tv_lnum_buf(&argvars[2], buf);
11195
Bram Moolenaar342337a2005-07-21 21:11:17 +000011196 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011197}
11198
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199/*
11200 * "getbufvar()" function
11201 */
11202 static void
11203f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011204 typval_T *argvars;
11205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206{
11207 buf_T *buf;
11208 buf_T *save_curbuf;
11209 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011210 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011211 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11214 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011215 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011216 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011217
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011218 rettv->v_type = VAR_STRING;
11219 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011220
11221 if (buf != NULL && varname != NULL)
11222 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011223 /* set curbuf to be our buf, temporarily */
11224 save_curbuf = curbuf;
11225 curbuf = buf;
11226
Bram Moolenaar0d660222005-01-07 21:51:51 +000011227 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011228 {
11229 if (get_option_tv(&varname, rettv, TRUE) == OK)
11230 done = TRUE;
11231 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011232 else if (STRCMP(varname, "changedtick") == 0)
11233 {
11234 rettv->v_type = VAR_NUMBER;
11235 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011236 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011237 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238 else
11239 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011240 /* Look up the variable. */
11241 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11242 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11243 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011245 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011246 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011247 done = TRUE;
11248 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011250
11251 /* restore previous notion of curbuf */
11252 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011253 }
11254
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011255 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11256 /* use the default value */
11257 copy_tv(&argvars[2], rettv);
11258
Bram Moolenaar0d660222005-01-07 21:51:51 +000011259 --emsg_off;
11260}
11261
11262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 * "getchar()" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011273 /* Position the cursor. Needed after a message that ends in a space. */
11274 windgoto(msg_row, msg_col);
11275
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 ++no_mapping;
11277 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011278 for (;;)
11279 {
11280 if (argvars[0].v_type == VAR_UNKNOWN)
11281 /* getchar(): blocking wait. */
11282 n = safe_vgetc();
11283 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11284 /* getchar(1): only check if char avail */
11285 n = vpeekc();
11286 else if (error || vpeekc() == NUL)
11287 /* illegal argument or getchar(0) and no char avail: return zero */
11288 n = 0;
11289 else
11290 /* getchar(0) and char avail: return char */
11291 n = safe_vgetc();
11292 if (n == K_IGNORE)
11293 continue;
11294 break;
11295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 --no_mapping;
11297 --allow_keys;
11298
Bram Moolenaar219b8702006-11-01 14:32:36 +000011299 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11300 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11301 vimvars[VV_MOUSE_COL].vv_nr = 0;
11302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 if (IS_SPECIAL(n) || mod_mask != 0)
11305 {
11306 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11307 int i = 0;
11308
11309 /* Turn a special key into three bytes, plus modifier. */
11310 if (mod_mask != 0)
11311 {
11312 temp[i++] = K_SPECIAL;
11313 temp[i++] = KS_MODIFIER;
11314 temp[i++] = mod_mask;
11315 }
11316 if (IS_SPECIAL(n))
11317 {
11318 temp[i++] = K_SPECIAL;
11319 temp[i++] = K_SECOND(n);
11320 temp[i++] = K_THIRD(n);
11321 }
11322#ifdef FEAT_MBYTE
11323 else if (has_mbyte)
11324 i += (*mb_char2bytes)(n, temp + i);
11325#endif
11326 else
11327 temp[i++] = n;
11328 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->v_type = VAR_STRING;
11330 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011331
11332#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011333 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011334 {
11335 int row = mouse_row;
11336 int col = mouse_col;
11337 win_T *win;
11338 linenr_T lnum;
11339# ifdef FEAT_WINDOWS
11340 win_T *wp;
11341# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011342 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011343
11344 if (row >= 0 && col >= 0)
11345 {
11346 /* Find the window at the mouse coordinates and compute the
11347 * text position. */
11348 win = mouse_find_win(&row, &col);
11349 (void)mouse_comp_pos(win, &row, &col, &lnum);
11350# ifdef FEAT_WINDOWS
11351 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011352 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011353# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011354 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011355 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11356 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11357 }
11358 }
11359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 }
11361}
11362
11363/*
11364 * "getcharmod()" function
11365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011368 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372}
11373
11374/*
11375 * "getcmdline()" function
11376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011379 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 rettv->v_type = VAR_STRING;
11383 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384}
11385
11386/*
11387 * "getcmdpos()" function
11388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011391 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395}
11396
11397/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011398 * "getcmdtype()" function
11399 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011400 static void
11401f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011402 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011403 typval_T *rettv;
11404{
11405 rettv->v_type = VAR_STRING;
11406 rettv->vval.v_string = alloc(2);
11407 if (rettv->vval.v_string != NULL)
11408 {
11409 rettv->vval.v_string[0] = get_cmdline_type();
11410 rettv->vval.v_string[1] = NUL;
11411 }
11412}
11413
11414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 * "getcwd()" function
11416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011422 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011425 rettv->vval.v_string = NULL;
11426 cwd = alloc(MAXPATHL);
11427 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011429 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11430 {
11431 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011433 if (rettv->vval.v_string != NULL)
11434 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011436 }
11437 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 }
11439}
11440
11441/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011442 * "getfontname()" function
11443 */
11444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011446 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011447 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
11450 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011451#ifdef FEAT_GUI
11452 if (gui.in_use)
11453 {
11454 GuiFont font;
11455 char_u *name = NULL;
11456
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011457 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011458 {
11459 /* Get the "Normal" font. Either the name saved by
11460 * hl_set_font_name() or from the font ID. */
11461 font = gui.norm_font;
11462 name = hl_get_font_name();
11463 }
11464 else
11465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011467 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11468 return;
11469 font = gui_mch_get_font(name, FALSE);
11470 if (font == NOFONT)
11471 return; /* Invalid font name, return empty string. */
11472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011475 gui_mch_free_font(font);
11476 }
11477#endif
11478}
11479
11480/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481 * "getfperm({fname})" function
11482 */
11483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *argvars;
11486 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011487{
11488 char_u *fname;
11489 struct stat st;
11490 char_u *perm = NULL;
11491 char_u flags[] = "rwx";
11492 int i;
11493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011497 if (mch_stat((char *)fname, &st) >= 0)
11498 {
11499 perm = vim_strsave((char_u *)"---------");
11500 if (perm != NULL)
11501 {
11502 for (i = 0; i < 9; i++)
11503 {
11504 if (st.st_mode & (1 << (8 - i)))
11505 perm[i] = flags[i % 3];
11506 }
11507 }
11508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011510}
11511
11512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513 * "getfsize({fname})" function
11514 */
11515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519{
11520 char_u *fname;
11521 struct stat st;
11522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526
11527 if (mch_stat((char *)fname, &st) >= 0)
11528 {
11529 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011534
11535 /* non-perfect check for overflow */
11536 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11537 rettv->vval.v_number = -2;
11538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 }
11540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542}
11543
11544/*
11545 * "getftime({fname})" function
11546 */
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551{
11552 char_u *fname;
11553 struct stat st;
11554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556
11557 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561}
11562
11563/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564 * "getftype({fname})" function
11565 */
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011570{
11571 char_u *fname;
11572 struct stat st;
11573 char_u *type = NULL;
11574 char *t;
11575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011579 if (mch_lstat((char *)fname, &st) >= 0)
11580 {
11581#ifdef S_ISREG
11582 if (S_ISREG(st.st_mode))
11583 t = "file";
11584 else if (S_ISDIR(st.st_mode))
11585 t = "dir";
11586# ifdef S_ISLNK
11587 else if (S_ISLNK(st.st_mode))
11588 t = "link";
11589# endif
11590# ifdef S_ISBLK
11591 else if (S_ISBLK(st.st_mode))
11592 t = "bdev";
11593# endif
11594# ifdef S_ISCHR
11595 else if (S_ISCHR(st.st_mode))
11596 t = "cdev";
11597# endif
11598# ifdef S_ISFIFO
11599 else if (S_ISFIFO(st.st_mode))
11600 t = "fifo";
11601# endif
11602# ifdef S_ISSOCK
11603 else if (S_ISSOCK(st.st_mode))
11604 t = "fifo";
11605# endif
11606 else
11607 t = "other";
11608#else
11609# ifdef S_IFMT
11610 switch (st.st_mode & S_IFMT)
11611 {
11612 case S_IFREG: t = "file"; break;
11613 case S_IFDIR: t = "dir"; break;
11614# ifdef S_IFLNK
11615 case S_IFLNK: t = "link"; break;
11616# endif
11617# ifdef S_IFBLK
11618 case S_IFBLK: t = "bdev"; break;
11619# endif
11620# ifdef S_IFCHR
11621 case S_IFCHR: t = "cdev"; break;
11622# endif
11623# ifdef S_IFIFO
11624 case S_IFIFO: t = "fifo"; break;
11625# endif
11626# ifdef S_IFSOCK
11627 case S_IFSOCK: t = "socket"; break;
11628# endif
11629 default: t = "other";
11630 }
11631# else
11632 if (mch_isdir(fname))
11633 t = "dir";
11634 else
11635 t = "file";
11636# endif
11637#endif
11638 type = vim_strsave((char_u *)t);
11639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011641}
11642
11643/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011644 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 */
11646 static void
11647f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011650{
11651 linenr_T lnum;
11652 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011653 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011654
11655 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011656 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011657 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011658 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011659 retlist = FALSE;
11660 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011662 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011664 retlist = TRUE;
11665 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011666
Bram Moolenaar342337a2005-07-21 21:11:17 +000011667 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668}
11669
11670/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011671 * "getmatches()" function
11672 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011673 static void
11674f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011675 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011676 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011677{
11678#ifdef FEAT_SEARCH_EXTRA
11679 dict_T *dict;
11680 matchitem_T *cur = curwin->w_match_head;
11681
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011682 if (rettv_list_alloc(rettv) == OK)
11683 {
11684 while (cur != NULL)
11685 {
11686 dict = dict_alloc();
11687 if (dict == NULL)
11688 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011689 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11690 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11691 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11692 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11693 list_append_dict(rettv->vval.v_list, dict);
11694 cur = cur->next;
11695 }
11696 }
11697#endif
11698}
11699
11700/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011701 * "getpid()" function
11702 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011703 static void
11704f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011705 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011706 typval_T *rettv;
11707{
11708 rettv->vval.v_number = mch_get_pid();
11709}
11710
11711/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011712 * "getpos(string)" function
11713 */
11714 static void
11715f_getpos(argvars, rettv)
11716 typval_T *argvars;
11717 typval_T *rettv;
11718{
11719 pos_T *fp;
11720 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011721 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011722
11723 if (rettv_list_alloc(rettv) == OK)
11724 {
11725 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011726 fp = var2fpos(&argvars[0], TRUE, &fnum);
11727 if (fnum != -1)
11728 list_append_number(l, (varnumber_T)fnum);
11729 else
11730 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011731 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11732 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011733 list_append_number(l, (fp != NULL)
11734 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011735 : (varnumber_T)0);
11736 list_append_number(l,
11737#ifdef FEAT_VIRTUALEDIT
11738 (fp != NULL) ? (varnumber_T)fp->coladd :
11739#endif
11740 (varnumber_T)0);
11741 }
11742 else
11743 rettv->vval.v_number = FALSE;
11744}
11745
11746/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011747 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011748 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011749 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011750f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011751 typval_T *argvars UNUSED;
11752 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011753{
11754#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011755 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011756#endif
11757
Bram Moolenaar2641f772005-03-25 21:58:17 +000011758#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011759 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011760 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011761 wp = NULL;
11762 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11763 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011764 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011765 if (wp == NULL)
11766 return;
11767 }
11768
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011769 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011770 }
11771#endif
11772}
11773
11774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 * "getreg()" function
11776 */
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
11782 char_u *strregname;
11783 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011784 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011787 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 strregname = get_tv_string_chk(&argvars[0]);
11790 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011791 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011795 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 regname = (strregname == NULL ? '"' : *strregname);
11797 if (regname == 0)
11798 regname = '"';
11799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 rettv->vval.v_string = error ? NULL :
11802 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803}
11804
11805/*
11806 * "getregtype()" function
11807 */
11808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011810 typval_T *argvars;
11811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
11813 char_u *strregname;
11814 int regname;
11815 char_u buf[NUMBUFLEN + 2];
11816 long reglen = 0;
11817
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 {
11820 strregname = get_tv_string_chk(&argvars[0]);
11821 if (strregname == NULL) /* type error; errmsg already given */
11822 {
11823 rettv->v_type = VAR_STRING;
11824 rettv->vval.v_string = NULL;
11825 return;
11826 }
11827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 else
11829 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011830 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831
11832 regname = (strregname == NULL ? '"' : *strregname);
11833 if (regname == 0)
11834 regname = '"';
11835
11836 buf[0] = NUL;
11837 buf[1] = NUL;
11838 switch (get_reg_type(regname, &reglen))
11839 {
11840 case MLINE: buf[0] = 'V'; break;
11841 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 case MBLOCK:
11843 buf[0] = Ctrl_V;
11844 sprintf((char *)buf + 1, "%ld", reglen + 1);
11845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->v_type = VAR_STRING;
11848 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849}
11850
11851/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011852 * "gettabvar()" function
11853 */
11854 static void
11855f_gettabvar(argvars, rettv)
11856 typval_T *argvars;
11857 typval_T *rettv;
11858{
11859 tabpage_T *tp;
11860 dictitem_T *v;
11861 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011862 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011863
11864 rettv->v_type = VAR_STRING;
11865 rettv->vval.v_string = NULL;
11866
11867 varname = get_tv_string_chk(&argvars[1]);
11868 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11869 if (tp != NULL && varname != NULL)
11870 {
11871 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011872 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011873 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011874 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011875 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011876 done = TRUE;
11877 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011878 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011879
11880 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011881 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011882}
11883
11884/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011885 * "gettabwinvar()" function
11886 */
11887 static void
11888f_gettabwinvar(argvars, rettv)
11889 typval_T *argvars;
11890 typval_T *rettv;
11891{
11892 getwinvar(argvars, rettv, 1);
11893}
11894
11895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 * "getwinposx()" function
11897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904#ifdef FEAT_GUI
11905 if (gui.in_use)
11906 {
11907 int x, y;
11908
11909 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 }
11912#endif
11913}
11914
11915/*
11916 * "getwinposy()" function
11917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011920 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011923 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#ifdef FEAT_GUI
11925 if (gui.in_use)
11926 {
11927 int x, y;
11928
11929 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 }
11932#endif
11933}
11934
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011935/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011936 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011937 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011938 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011939find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011940 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011941 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011942{
11943#ifdef FEAT_WINDOWS
11944 win_T *wp;
11945#endif
11946 int nr;
11947
11948 nr = get_tv_number_chk(vp, NULL);
11949
11950#ifdef FEAT_WINDOWS
11951 if (nr < 0)
11952 return NULL;
11953 if (nr == 0)
11954 return curwin;
11955
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011956 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11957 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011958 if (--nr <= 0)
11959 break;
11960 return wp;
11961#else
11962 if (nr == 0 || nr == 1)
11963 return curwin;
11964 return NULL;
11965#endif
11966}
11967
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968/*
11969 * "getwinvar()" function
11970 */
11971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 typval_T *argvars;
11974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011976 getwinvar(argvars, rettv, 0);
11977}
11978
11979/*
11980 * getwinvar() and gettabwinvar()
11981 */
11982 static void
11983getwinvar(argvars, rettv, off)
11984 typval_T *argvars;
11985 typval_T *rettv;
11986 int off; /* 1 for gettabwinvar() */
11987{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 win_T *win, *oldcurwin;
11989 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011991 tabpage_T *tp = NULL;
11992 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011993 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011995#ifdef FEAT_WINDOWS
11996 if (off == 1)
11997 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11998 else
11999 tp = curtab;
12000#endif
12001 win = find_win_by_nr(&argvars[off], tp);
12002 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012005 rettv->v_type = VAR_STRING;
12006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007
12008 if (win != NULL && varname != NULL)
12009 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012010 /* Set curwin to be our win, temporarily. Also set the tabpage,
12011 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012012 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012013
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012015 {
12016 if (get_option_tv(&varname, rettv, 1) == OK)
12017 done = TRUE;
12018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 else
12020 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012021 /* Look up the variable. */
12022 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12023 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012026 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012027 done = TRUE;
12028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012030
12031 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012032 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 }
12034
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012035 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12036 /* use the default return value */
12037 copy_tv(&argvars[off + 2], rettv);
12038
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 --emsg_off;
12040}
12041
12042/*
12043 * "glob()" function
12044 */
12045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012047 typval_T *argvars;
12048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012050 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012052 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012054 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012055 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012057 if (argvars[1].v_type != VAR_UNKNOWN)
12058 {
12059 if (get_tv_number_chk(&argvars[1], &error))
12060 options |= WILD_KEEP_ALL;
12061 if (argvars[2].v_type != VAR_UNKNOWN
12062 && get_tv_number_chk(&argvars[2], &error))
12063 {
12064 rettv->v_type = VAR_LIST;
12065 rettv->vval.v_list = NULL;
12066 }
12067 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012068 if (!error)
12069 {
12070 ExpandInit(&xpc);
12071 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012072 if (p_wic)
12073 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012074 if (rettv->v_type == VAR_STRING)
12075 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012076 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012077 else if (rettv_list_alloc(rettv) != FAIL)
12078 {
12079 int i;
12080
12081 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12082 NULL, options, WILD_ALL_KEEP);
12083 for (i = 0; i < xpc.xp_numfiles; i++)
12084 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12085
12086 ExpandCleanup(&xpc);
12087 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012088 }
12089 else
12090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091}
12092
12093/*
12094 * "globpath()" function
12095 */
12096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 typval_T *argvars;
12099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012101 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012103 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012104 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012106 /* When the optional second argument is non-zero, don't remove matches
12107 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12108 if (argvars[2].v_type != VAR_UNKNOWN
12109 && get_tv_number_chk(&argvars[2], &error))
12110 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012112 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012113 rettv->vval.v_string = NULL;
12114 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012115 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12116 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
12119/*
12120 * "has()" function
12121 */
12122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126{
12127 int i;
12128 char_u *name;
12129 int n = FALSE;
12130 static char *(has_list[]) =
12131 {
12132#ifdef AMIGA
12133 "amiga",
12134# ifdef FEAT_ARP
12135 "arp",
12136# endif
12137#endif
12138#ifdef __BEOS__
12139 "beos",
12140#endif
12141#ifdef MSDOS
12142# ifdef DJGPP
12143 "dos32",
12144# else
12145 "dos16",
12146# endif
12147#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012148#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 "mac",
12150#endif
12151#if defined(MACOS_X_UNIX)
12152 "macunix",
12153#endif
12154#ifdef OS2
12155 "os2",
12156#endif
12157#ifdef __QNX__
12158 "qnx",
12159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160#ifdef UNIX
12161 "unix",
12162#endif
12163#ifdef VMS
12164 "vms",
12165#endif
12166#ifdef WIN16
12167 "win16",
12168#endif
12169#ifdef WIN32
12170 "win32",
12171#endif
12172#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12173 "win32unix",
12174#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012175#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 "win64",
12177#endif
12178#ifdef EBCDIC
12179 "ebcdic",
12180#endif
12181#ifndef CASE_INSENSITIVE_FILENAME
12182 "fname_case",
12183#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012184#ifdef HAVE_ACL
12185 "acl",
12186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187#ifdef FEAT_ARABIC
12188 "arabic",
12189#endif
12190#ifdef FEAT_AUTOCMD
12191 "autocmd",
12192#endif
12193#ifdef FEAT_BEVAL
12194 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012195# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12196 "balloon_multiline",
12197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198#endif
12199#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12200 "builtin_terms",
12201# ifdef ALL_BUILTIN_TCAPS
12202 "all_builtin_terms",
12203# endif
12204#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012205#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12206 || defined(FEAT_GUI_W32) \
12207 || defined(FEAT_GUI_MOTIF))
12208 "browsefilter",
12209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210#ifdef FEAT_BYTEOFF
12211 "byte_offset",
12212#endif
12213#ifdef FEAT_CINDENT
12214 "cindent",
12215#endif
12216#ifdef FEAT_CLIENTSERVER
12217 "clientserver",
12218#endif
12219#ifdef FEAT_CLIPBOARD
12220 "clipboard",
12221#endif
12222#ifdef FEAT_CMDL_COMPL
12223 "cmdline_compl",
12224#endif
12225#ifdef FEAT_CMDHIST
12226 "cmdline_hist",
12227#endif
12228#ifdef FEAT_COMMENTS
12229 "comments",
12230#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012231#ifdef FEAT_CONCEAL
12232 "conceal",
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef FEAT_CRYPT
12235 "cryptv",
12236#endif
12237#ifdef FEAT_CSCOPE
12238 "cscope",
12239#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012240#ifdef FEAT_CURSORBIND
12241 "cursorbind",
12242#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012243#ifdef CURSOR_SHAPE
12244 "cursorshape",
12245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246#ifdef DEBUG
12247 "debug",
12248#endif
12249#ifdef FEAT_CON_DIALOG
12250 "dialog_con",
12251#endif
12252#ifdef FEAT_GUI_DIALOG
12253 "dialog_gui",
12254#endif
12255#ifdef FEAT_DIFF
12256 "diff",
12257#endif
12258#ifdef FEAT_DIGRAPHS
12259 "digraphs",
12260#endif
12261#ifdef FEAT_DND
12262 "dnd",
12263#endif
12264#ifdef FEAT_EMACS_TAGS
12265 "emacs_tags",
12266#endif
12267 "eval", /* always present, of course! */
12268#ifdef FEAT_EX_EXTRA
12269 "ex_extra",
12270#endif
12271#ifdef FEAT_SEARCH_EXTRA
12272 "extra_search",
12273#endif
12274#ifdef FEAT_FKMAP
12275 "farsi",
12276#endif
12277#ifdef FEAT_SEARCHPATH
12278 "file_in_path",
12279#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012280#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012281 "filterpipe",
12282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef FEAT_FIND_ID
12284 "find_in_path",
12285#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012286#ifdef FEAT_FLOAT
12287 "float",
12288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289#ifdef FEAT_FOLDING
12290 "folding",
12291#endif
12292#ifdef FEAT_FOOTER
12293 "footer",
12294#endif
12295#if !defined(USE_SYSTEM) && defined(UNIX)
12296 "fork",
12297#endif
12298#ifdef FEAT_GETTEXT
12299 "gettext",
12300#endif
12301#ifdef FEAT_GUI
12302 "gui",
12303#endif
12304#ifdef FEAT_GUI_ATHENA
12305# ifdef FEAT_GUI_NEXTAW
12306 "gui_neXtaw",
12307# else
12308 "gui_athena",
12309# endif
12310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311#ifdef FEAT_GUI_GTK
12312 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012315#ifdef FEAT_GUI_GNOME
12316 "gui_gnome",
12317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318#ifdef FEAT_GUI_MAC
12319 "gui_mac",
12320#endif
12321#ifdef FEAT_GUI_MOTIF
12322 "gui_motif",
12323#endif
12324#ifdef FEAT_GUI_PHOTON
12325 "gui_photon",
12326#endif
12327#ifdef FEAT_GUI_W16
12328 "gui_win16",
12329#endif
12330#ifdef FEAT_GUI_W32
12331 "gui_win32",
12332#endif
12333#ifdef FEAT_HANGULIN
12334 "hangul_input",
12335#endif
12336#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12337 "iconv",
12338#endif
12339#ifdef FEAT_INS_EXPAND
12340 "insert_expand",
12341#endif
12342#ifdef FEAT_JUMPLIST
12343 "jumplist",
12344#endif
12345#ifdef FEAT_KEYMAP
12346 "keymap",
12347#endif
12348#ifdef FEAT_LANGMAP
12349 "langmap",
12350#endif
12351#ifdef FEAT_LIBCALL
12352 "libcall",
12353#endif
12354#ifdef FEAT_LINEBREAK
12355 "linebreak",
12356#endif
12357#ifdef FEAT_LISP
12358 "lispindent",
12359#endif
12360#ifdef FEAT_LISTCMDS
12361 "listcmds",
12362#endif
12363#ifdef FEAT_LOCALMAP
12364 "localmap",
12365#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012366#ifdef FEAT_LUA
12367# ifndef DYNAMIC_LUA
12368 "lua",
12369# endif
12370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371#ifdef FEAT_MENU
12372 "menu",
12373#endif
12374#ifdef FEAT_SESSION
12375 "mksession",
12376#endif
12377#ifdef FEAT_MODIFY_FNAME
12378 "modify_fname",
12379#endif
12380#ifdef FEAT_MOUSE
12381 "mouse",
12382#endif
12383#ifdef FEAT_MOUSESHAPE
12384 "mouseshape",
12385#endif
12386#if defined(UNIX) || defined(VMS)
12387# ifdef FEAT_MOUSE_DEC
12388 "mouse_dec",
12389# endif
12390# ifdef FEAT_MOUSE_GPM
12391 "mouse_gpm",
12392# endif
12393# ifdef FEAT_MOUSE_JSB
12394 "mouse_jsbterm",
12395# endif
12396# ifdef FEAT_MOUSE_NET
12397 "mouse_netterm",
12398# endif
12399# ifdef FEAT_MOUSE_PTERM
12400 "mouse_pterm",
12401# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012402# ifdef FEAT_MOUSE_SGR
12403 "mouse_sgr",
12404# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012405# ifdef FEAT_SYSMOUSE
12406 "mouse_sysmouse",
12407# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012408# ifdef FEAT_MOUSE_URXVT
12409 "mouse_urxvt",
12410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411# ifdef FEAT_MOUSE_XTERM
12412 "mouse_xterm",
12413# endif
12414#endif
12415#ifdef FEAT_MBYTE
12416 "multi_byte",
12417#endif
12418#ifdef FEAT_MBYTE_IME
12419 "multi_byte_ime",
12420#endif
12421#ifdef FEAT_MULTI_LANG
12422 "multi_lang",
12423#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012424#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012425#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012426 "mzscheme",
12427#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_OLE
12430 "ole",
12431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432#ifdef FEAT_PATH_EXTRA
12433 "path_extra",
12434#endif
12435#ifdef FEAT_PERL
12436#ifndef DYNAMIC_PERL
12437 "perl",
12438#endif
12439#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012440#ifdef FEAT_PERSISTENT_UNDO
12441 "persistent_undo",
12442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443#ifdef FEAT_PYTHON
12444#ifndef DYNAMIC_PYTHON
12445 "python",
12446#endif
12447#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012448#ifdef FEAT_PYTHON3
12449#ifndef DYNAMIC_PYTHON3
12450 "python3",
12451#endif
12452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453#ifdef FEAT_POSTSCRIPT
12454 "postscript",
12455#endif
12456#ifdef FEAT_PRINTER
12457 "printer",
12458#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012459#ifdef FEAT_PROFILE
12460 "profile",
12461#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012462#ifdef FEAT_RELTIME
12463 "reltime",
12464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465#ifdef FEAT_QUICKFIX
12466 "quickfix",
12467#endif
12468#ifdef FEAT_RIGHTLEFT
12469 "rightleft",
12470#endif
12471#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12472 "ruby",
12473#endif
12474#ifdef FEAT_SCROLLBIND
12475 "scrollbind",
12476#endif
12477#ifdef FEAT_CMDL_INFO
12478 "showcmd",
12479 "cmdline_info",
12480#endif
12481#ifdef FEAT_SIGNS
12482 "signs",
12483#endif
12484#ifdef FEAT_SMARTINDENT
12485 "smartindent",
12486#endif
12487#ifdef FEAT_SNIFF
12488 "sniff",
12489#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012490#ifdef STARTUPTIME
12491 "startuptime",
12492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493#ifdef FEAT_STL_OPT
12494 "statusline",
12495#endif
12496#ifdef FEAT_SUN_WORKSHOP
12497 "sun_workshop",
12498#endif
12499#ifdef FEAT_NETBEANS_INTG
12500 "netbeans_intg",
12501#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012502#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012503 "spell",
12504#endif
12505#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 "syntax",
12507#endif
12508#if defined(USE_SYSTEM) || !defined(UNIX)
12509 "system",
12510#endif
12511#ifdef FEAT_TAG_BINS
12512 "tag_binary",
12513#endif
12514#ifdef FEAT_TAG_OLDSTATIC
12515 "tag_old_static",
12516#endif
12517#ifdef FEAT_TAG_ANYWHITE
12518 "tag_any_white",
12519#endif
12520#ifdef FEAT_TCL
12521# ifndef DYNAMIC_TCL
12522 "tcl",
12523# endif
12524#endif
12525#ifdef TERMINFO
12526 "terminfo",
12527#endif
12528#ifdef FEAT_TERMRESPONSE
12529 "termresponse",
12530#endif
12531#ifdef FEAT_TEXTOBJ
12532 "textobjects",
12533#endif
12534#ifdef HAVE_TGETENT
12535 "tgetent",
12536#endif
12537#ifdef FEAT_TITLE
12538 "title",
12539#endif
12540#ifdef FEAT_TOOLBAR
12541 "toolbar",
12542#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012543#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12544 "unnamedplus",
12545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546#ifdef FEAT_USR_CMDS
12547 "user-commands", /* was accidentally included in 5.4 */
12548 "user_commands",
12549#endif
12550#ifdef FEAT_VIMINFO
12551 "viminfo",
12552#endif
12553#ifdef FEAT_VERTSPLIT
12554 "vertsplit",
12555#endif
12556#ifdef FEAT_VIRTUALEDIT
12557 "virtualedit",
12558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560#ifdef FEAT_VISUALEXTRA
12561 "visualextra",
12562#endif
12563#ifdef FEAT_VREPLACE
12564 "vreplace",
12565#endif
12566#ifdef FEAT_WILDIGN
12567 "wildignore",
12568#endif
12569#ifdef FEAT_WILDMENU
12570 "wildmenu",
12571#endif
12572#ifdef FEAT_WINDOWS
12573 "windows",
12574#endif
12575#ifdef FEAT_WAK
12576 "winaltkeys",
12577#endif
12578#ifdef FEAT_WRITEBACKUP
12579 "writebackup",
12580#endif
12581#ifdef FEAT_XIM
12582 "xim",
12583#endif
12584#ifdef FEAT_XFONTSET
12585 "xfontset",
12586#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012587#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012588 "xpm",
12589 "xpm_w32", /* for backward compatibility */
12590#else
12591# if defined(HAVE_XPM)
12592 "xpm",
12593# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595#ifdef USE_XSMP
12596 "xsmp",
12597#endif
12598#ifdef USE_XSMP_INTERACT
12599 "xsmp_interact",
12600#endif
12601#ifdef FEAT_XCLIPBOARD
12602 "xterm_clipboard",
12603#endif
12604#ifdef FEAT_XTERM_SAVE
12605 "xterm_save",
12606#endif
12607#if defined(UNIX) && defined(FEAT_X11)
12608 "X11",
12609#endif
12610 NULL
12611 };
12612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 for (i = 0; has_list[i] != NULL; ++i)
12615 if (STRICMP(name, has_list[i]) == 0)
12616 {
12617 n = TRUE;
12618 break;
12619 }
12620
12621 if (n == FALSE)
12622 {
12623 if (STRNICMP(name, "patch", 5) == 0)
12624 n = has_patch(atoi((char *)name + 5));
12625 else if (STRICMP(name, "vim_starting") == 0)
12626 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012627#ifdef FEAT_MBYTE
12628 else if (STRICMP(name, "multi_byte_encoding") == 0)
12629 n = has_mbyte;
12630#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012631#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12632 else if (STRICMP(name, "balloon_multiline") == 0)
12633 n = multiline_balloon_available();
12634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635#ifdef DYNAMIC_TCL
12636 else if (STRICMP(name, "tcl") == 0)
12637 n = tcl_enabled(FALSE);
12638#endif
12639#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12640 else if (STRICMP(name, "iconv") == 0)
12641 n = iconv_enabled(FALSE);
12642#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012643#ifdef DYNAMIC_LUA
12644 else if (STRICMP(name, "lua") == 0)
12645 n = lua_enabled(FALSE);
12646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012647#ifdef DYNAMIC_MZSCHEME
12648 else if (STRICMP(name, "mzscheme") == 0)
12649 n = mzscheme_enabled(FALSE);
12650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651#ifdef DYNAMIC_RUBY
12652 else if (STRICMP(name, "ruby") == 0)
12653 n = ruby_enabled(FALSE);
12654#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012655#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656#ifdef DYNAMIC_PYTHON
12657 else if (STRICMP(name, "python") == 0)
12658 n = python_enabled(FALSE);
12659#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012660#endif
12661#ifdef FEAT_PYTHON3
12662#ifdef DYNAMIC_PYTHON3
12663 else if (STRICMP(name, "python3") == 0)
12664 n = python3_enabled(FALSE);
12665#endif
12666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667#ifdef DYNAMIC_PERL
12668 else if (STRICMP(name, "perl") == 0)
12669 n = perl_enabled(FALSE);
12670#endif
12671#ifdef FEAT_GUI
12672 else if (STRICMP(name, "gui_running") == 0)
12673 n = (gui.in_use || gui.starting);
12674# ifdef FEAT_GUI_W32
12675 else if (STRICMP(name, "gui_win32s") == 0)
12676 n = gui_is_win32s();
12677# endif
12678# ifdef FEAT_BROWSE
12679 else if (STRICMP(name, "browse") == 0)
12680 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12681# endif
12682#endif
12683#ifdef FEAT_SYN_HL
12684 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012685 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686#endif
12687#if defined(WIN3264)
12688 else if (STRICMP(name, "win95") == 0)
12689 n = mch_windows95();
12690#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012691#ifdef FEAT_NETBEANS_INTG
12692 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012693 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 }
12696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698}
12699
12700/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701 * "has_key()" function
12702 */
12703 static void
12704f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 typval_T *argvars;
12706 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012708 if (argvars[0].v_type != VAR_DICT)
12709 {
12710 EMSG(_(e_dictreq));
12711 return;
12712 }
12713 if (argvars[0].vval.v_dict == NULL)
12714 return;
12715
12716 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012717 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012718}
12719
12720/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012721 * "haslocaldir()" function
12722 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012723 static void
12724f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012725 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012726 typval_T *rettv;
12727{
12728 rettv->vval.v_number = (curwin->w_localdir != NULL);
12729}
12730
12731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 * "hasmapto()" function
12733 */
12734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 typval_T *argvars;
12737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738{
12739 char_u *name;
12740 char_u *mode;
12741 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012742 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012745 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 mode = (char_u *)"nvo";
12747 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012750 if (argvars[2].v_type != VAR_UNKNOWN)
12751 abbr = get_tv_number(&argvars[2]);
12752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753
Bram Moolenaar2c932302006-03-18 21:42:09 +000012754 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758}
12759
12760/*
12761 * "histadd()" function
12762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012765 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767{
12768#ifdef FEAT_CMDHIST
12769 int histype;
12770 char_u *str;
12771 char_u buf[NUMBUFLEN];
12772#endif
12773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 if (check_restricted() || check_secure())
12776 return;
12777#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12779 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 if (histype >= 0)
12781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 if (*str != NUL)
12784 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012785 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 return;
12789 }
12790 }
12791#endif
12792}
12793
12794/*
12795 * "histdel()" function
12796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012799 typval_T *argvars UNUSED;
12800 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801{
12802#ifdef FEAT_CMDHIST
12803 int n;
12804 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12808 if (str == NULL)
12809 n = 0;
12810 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012812 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012813 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012815 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 else
12818 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012819 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 get_tv_string_buf(&argvars[1], buf));
12821 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822#endif
12823}
12824
12825/*
12826 * "histget()" function
12827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832{
12833#ifdef FEAT_CMDHIST
12834 int type;
12835 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012838 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12839 if (str == NULL)
12840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012842 {
12843 type = get_histtype(str);
12844 if (argvars[1].v_type == VAR_UNKNOWN)
12845 idx = get_history_idx(type);
12846 else
12847 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12848 /* -1 on type error */
12849 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855}
12856
12857/*
12858 * "histnr()" function
12859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
12865 int i;
12866
12867#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012868 char_u *history = get_tv_string_chk(&argvars[0]);
12869
12870 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 if (i >= HIST_CMD && i < HIST_COUNT)
12872 i = get_history_idx(i);
12873 else
12874#endif
12875 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877}
12878
12879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 * "highlightID(name)" function
12881 */
12882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012884 typval_T *argvars;
12885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888}
12889
12890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012891 * "highlight_exists()" function
12892 */
12893 static void
12894f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012895 typval_T *argvars;
12896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012897{
12898 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12899}
12900
12901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 * "hostname()" function
12903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
12909 char_u hostname[256];
12910
12911 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->v_type = VAR_STRING;
12913 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914}
12915
12916/*
12917 * iconv() function
12918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923{
12924#ifdef FEAT_MBYTE
12925 char_u buf1[NUMBUFLEN];
12926 char_u buf2[NUMBUFLEN];
12927 char_u *from, *to, *str;
12928 vimconv_T vimconv;
12929#endif
12930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->v_type = VAR_STRING;
12932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933
12934#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 str = get_tv_string(&argvars[0]);
12936 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12937 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 vimconv.vc_type = CONV_NONE;
12939 convert_setup(&vimconv, from, to);
12940
12941 /* If the encodings are equal, no conversion needed. */
12942 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
12947 convert_setup(&vimconv, NULL, NULL);
12948 vim_free(from);
12949 vim_free(to);
12950#endif
12951}
12952
12953/*
12954 * "indent()" function
12955 */
12956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 typval_T *argvars;
12959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960{
12961 linenr_T lnum;
12962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968}
12969
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012970/*
12971 * "index()" function
12972 */
12973 static void
12974f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012975 typval_T *argvars;
12976 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012977{
Bram Moolenaar33570922005-01-25 22:26:29 +000012978 list_T *l;
12979 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012980 long idx = 0;
12981 int ic = FALSE;
12982
12983 rettv->vval.v_number = -1;
12984 if (argvars[0].v_type != VAR_LIST)
12985 {
12986 EMSG(_(e_listreq));
12987 return;
12988 }
12989 l = argvars[0].vval.v_list;
12990 if (l != NULL)
12991 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012992 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995 int error = FALSE;
12996
Bram Moolenaar758711c2005-02-02 23:11:38 +000012997 /* Start at specified item. Use the cached index that list_find()
12998 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013000 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013001 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013002 ic = get_tv_number_chk(&argvars[3], &error);
13003 if (error)
13004 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013005 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013006
Bram Moolenaar758711c2005-02-02 23:11:38 +000013007 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013008 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013009 {
13010 rettv->vval.v_number = idx;
13011 break;
13012 }
13013 }
13014}
13015
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016static int inputsecret_flag = 0;
13017
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13019
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013021 * This function is used by f_input() and f_inputdialog() functions. The third
13022 * argument to f_input() specifies the type of completion to use at the
13023 * prompt. The third argument to f_inputdialog() specifies the value to return
13024 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 */
13026 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013027get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013028 typval_T *argvars;
13029 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013030 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 char_u *p = NULL;
13034 int c;
13035 char_u buf[NUMBUFLEN];
13036 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013038 int xp_type = EXPAND_NOTHING;
13039 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043
13044#ifdef NO_CONSOLE_INPUT
13045 /* While starting up, there is no place to enter text. */
13046 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048#endif
13049
13050 cmd_silent = FALSE; /* Want to see the prompt. */
13051 if (prompt != NULL)
13052 {
13053 /* Only the part of the message after the last NL is considered as
13054 * prompt for the command line */
13055 p = vim_strrchr(prompt, '\n');
13056 if (p == NULL)
13057 p = prompt;
13058 else
13059 {
13060 ++p;
13061 c = *p;
13062 *p = NUL;
13063 msg_start();
13064 msg_clr_eos();
13065 msg_puts_attr(prompt, echo_attr);
13066 msg_didout = FALSE;
13067 msg_starthere();
13068 *p = c;
13069 }
13070 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 if (argvars[1].v_type != VAR_UNKNOWN)
13073 {
13074 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13075 if (defstr != NULL)
13076 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013078 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013079 {
13080 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013081 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013082 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013083
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013084 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013085 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013086
Bram Moolenaar4463f292005-09-25 22:20:24 +000013087 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13088 if (xp_name == NULL)
13089 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013091 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013092
Bram Moolenaar4463f292005-09-25 22:20:24 +000013093 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13094 &xp_arg) == FAIL)
13095 return;
13096 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013097 }
13098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013100 {
13101# ifdef FEAT_EX_EXTRA
13102 int save_ex_normal_busy = ex_normal_busy;
13103 ex_normal_busy = 0;
13104# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013106 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13107 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013108# ifdef FEAT_EX_EXTRA
13109 ex_normal_busy = save_ex_normal_busy;
13110# endif
13111 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013112 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013113 && argvars[1].v_type != VAR_UNKNOWN
13114 && argvars[2].v_type != VAR_UNKNOWN)
13115 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13116 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013117
13118 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 /* since the user typed this, no need to wait for return */
13121 need_wait_return = FALSE;
13122 msg_didout = FALSE;
13123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 cmd_silent = cmd_silent_save;
13125}
13126
13127/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013128 * "input()" function
13129 * Also handles inputsecret() when inputsecret is set.
13130 */
13131 static void
13132f_input(argvars, rettv)
13133 typval_T *argvars;
13134 typval_T *rettv;
13135{
13136 get_user_input(argvars, rettv, FALSE);
13137}
13138
13139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 * "inputdialog()" function
13141 */
13142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *argvars;
13145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146{
13147#if defined(FEAT_GUI_TEXTDIALOG)
13148 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13149 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13150 {
13151 char_u *message;
13152 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 message = get_tv_string_chk(&argvars[0]);
13156 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013157 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013158 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 else
13160 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 if (message != NULL && defstr != NULL
13162 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013163 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 else
13166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013167 if (message != NULL && defstr != NULL
13168 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_string = vim_strsave(
13171 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 }
13177 else
13178#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013179 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180}
13181
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013182/*
13183 * "inputlist()" function
13184 */
13185 static void
13186f_inputlist(argvars, rettv)
13187 typval_T *argvars;
13188 typval_T *rettv;
13189{
13190 listitem_T *li;
13191 int selected;
13192 int mouse_used;
13193
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013194#ifdef NO_CONSOLE_INPUT
13195 /* While starting up, there is no place to enter text. */
13196 if (no_console_input())
13197 return;
13198#endif
13199 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13200 {
13201 EMSG2(_(e_listarg), "inputlist()");
13202 return;
13203 }
13204
13205 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013206 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013207 lines_left = Rows; /* avoid more prompt */
13208 msg_scroll = TRUE;
13209 msg_clr_eos();
13210
13211 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13212 {
13213 msg_puts(get_tv_string(&li->li_tv));
13214 msg_putchar('\n');
13215 }
13216
13217 /* Ask for choice. */
13218 selected = prompt_for_number(&mouse_used);
13219 if (mouse_used)
13220 selected -= lines_left;
13221
13222 rettv->vval.v_number = selected;
13223}
13224
13225
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13227
13228/*
13229 * "inputrestore()" function
13230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235{
13236 if (ga_userinput.ga_len > 0)
13237 {
13238 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13240 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013241 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 }
13243 else if (p_verbose > 1)
13244 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013245 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 }
13248}
13249
13250/*
13251 * "inputsave()" function
13252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013255 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013258 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 if (ga_grow(&ga_userinput, 1) == OK)
13260 {
13261 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13262 + ga_userinput.ga_len);
13263 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013264 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 }
13266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268}
13269
13270/*
13271 * "inputsecret()" function
13272 */
13273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
13276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
13278 ++cmdline_star;
13279 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 --cmdline_star;
13282 --inputsecret_flag;
13283}
13284
13285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286 * "insert()" function
13287 */
13288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *argvars;
13291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013292{
13293 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 listitem_T *item;
13295 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013297
13298 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013299 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013300 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013301 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013302 {
13303 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 before = get_tv_number_chk(&argvars[2], &error);
13305 if (error)
13306 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013307
Bram Moolenaar758711c2005-02-02 23:11:38 +000013308 if (before == l->lv_len)
13309 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013310 else
13311 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013312 item = list_find(l, before);
13313 if (item == NULL)
13314 {
13315 EMSGN(_(e_listidx), before);
13316 l = NULL;
13317 }
13318 }
13319 if (l != NULL)
13320 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013321 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013322 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013323 }
13324 }
13325}
13326
13327/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013328 * "invert(expr)" function
13329 */
13330 static void
13331f_invert(argvars, rettv)
13332 typval_T *argvars;
13333 typval_T *rettv;
13334{
13335 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13336}
13337
13338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 * "isdirectory()" function
13340 */
13341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013343 typval_T *argvars;
13344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347}
13348
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013349/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013350 * "islocked()" function
13351 */
13352 static void
13353f_islocked(argvars, rettv)
13354 typval_T *argvars;
13355 typval_T *rettv;
13356{
13357 lval_T lv;
13358 char_u *end;
13359 dictitem_T *di;
13360
13361 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013362 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13363 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013364 if (end != NULL && lv.ll_name != NULL)
13365 {
13366 if (*end != NUL)
13367 EMSG(_(e_trailing));
13368 else
13369 {
13370 if (lv.ll_tv == NULL)
13371 {
13372 if (check_changedtick(lv.ll_name))
13373 rettv->vval.v_number = 1; /* always locked */
13374 else
13375 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013376 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013377 if (di != NULL)
13378 {
13379 /* Consider a variable locked when:
13380 * 1. the variable itself is locked
13381 * 2. the value of the variable is locked.
13382 * 3. the List or Dict value is locked.
13383 */
13384 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13385 || tv_islocked(&di->di_tv));
13386 }
13387 }
13388 }
13389 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013390 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013391 else if (lv.ll_newkey != NULL)
13392 EMSG2(_(e_dictkey), lv.ll_newkey);
13393 else if (lv.ll_list != NULL)
13394 /* List item. */
13395 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13396 else
13397 /* Dictionary item. */
13398 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13399 }
13400 }
13401
13402 clear_lval(&lv);
13403}
13404
Bram Moolenaar33570922005-01-25 22:26:29 +000013405static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013406
13407/*
13408 * Turn a dict into a list:
13409 * "what" == 0: list of keys
13410 * "what" == 1: list of values
13411 * "what" == 2: list of items
13412 */
13413 static void
13414dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013415 typval_T *argvars;
13416 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013417 int what;
13418{
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 list_T *l2;
13420 dictitem_T *di;
13421 hashitem_T *hi;
13422 listitem_T *li;
13423 listitem_T *li2;
13424 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013425 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013426
Bram Moolenaar8c711452005-01-14 21:53:12 +000013427 if (argvars[0].v_type != VAR_DICT)
13428 {
13429 EMSG(_(e_dictreq));
13430 return;
13431 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013432 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433 return;
13434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013435 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013438 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013441 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013443 --todo;
13444 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013445
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013446 li = listitem_alloc();
13447 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013448 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013449 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013451 if (what == 0)
13452 {
13453 /* keys() */
13454 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013455 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013456 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13457 }
13458 else if (what == 1)
13459 {
13460 /* values() */
13461 copy_tv(&di->di_tv, &li->li_tv);
13462 }
13463 else
13464 {
13465 /* items() */
13466 l2 = list_alloc();
13467 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013468 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013469 li->li_tv.vval.v_list = l2;
13470 if (l2 == NULL)
13471 break;
13472 ++l2->lv_refcount;
13473
13474 li2 = listitem_alloc();
13475 if (li2 == NULL)
13476 break;
13477 list_append(l2, li2);
13478 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013479 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013480 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13481
13482 li2 = listitem_alloc();
13483 if (li2 == NULL)
13484 break;
13485 list_append(l2, li2);
13486 copy_tv(&di->di_tv, &li2->li_tv);
13487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013488 }
13489 }
13490}
13491
13492/*
13493 * "items(dict)" function
13494 */
13495 static void
13496f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013497 typval_T *argvars;
13498 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499{
13500 dict_list(argvars, rettv, 2);
13501}
13502
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013504 * "join()" function
13505 */
13506 static void
13507f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510{
13511 garray_T ga;
13512 char_u *sep;
13513
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514 if (argvars[0].v_type != VAR_LIST)
13515 {
13516 EMSG(_(e_listreq));
13517 return;
13518 }
13519 if (argvars[0].vval.v_list == NULL)
13520 return;
13521 if (argvars[1].v_type == VAR_UNKNOWN)
13522 sep = (char_u *)" ";
13523 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013525
13526 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013527
13528 if (sep != NULL)
13529 {
13530 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013531 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 ga_append(&ga, NUL);
13533 rettv->vval.v_string = (char_u *)ga.ga_data;
13534 }
13535 else
13536 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013537}
13538
13539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013540 * "keys()" function
13541 */
13542 static void
13543f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *argvars;
13545 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013546{
13547 dict_list(argvars, rettv, 0);
13548}
13549
13550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 * "last_buffer_nr()" function.
13552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013555 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557{
13558 int n = 0;
13559 buf_T *buf;
13560
13561 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13562 if (n < buf->b_fnum)
13563 n = buf->b_fnum;
13564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566}
13567
13568/*
13569 * "len()" function
13570 */
13571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *argvars;
13574 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575{
13576 switch (argvars[0].v_type)
13577 {
13578 case VAR_STRING:
13579 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 rettv->vval.v_number = (varnumber_T)STRLEN(
13581 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 break;
13583 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013585 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013586 case VAR_DICT:
13587 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13588 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013590 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013591 break;
13592 }
13593}
13594
Bram Moolenaar33570922005-01-25 22:26:29 +000013595static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013596
13597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013601 int type;
13602{
13603#ifdef FEAT_LIBCALL
13604 char_u *string_in;
13605 char_u **string_result;
13606 int nr_result;
13607#endif
13608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013610 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013612
13613 if (check_restricted() || check_secure())
13614 return;
13615
13616#ifdef FEAT_LIBCALL
13617 /* The first two args must be strings, otherwise its meaningless */
13618 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13619 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013620 string_in = NULL;
13621 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013622 string_in = argvars[2].vval.v_string;
13623 if (type == VAR_NUMBER)
13624 string_result = NULL;
13625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013627 if (mch_libcall(argvars[0].vval.v_string,
13628 argvars[1].vval.v_string,
13629 string_in,
13630 argvars[2].vval.v_number,
13631 string_result,
13632 &nr_result) == OK
13633 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013635 }
13636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
13639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013640 * "libcall()" function
13641 */
13642 static void
13643f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013644 typval_T *argvars;
13645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013646{
13647 libcall_common(argvars, rettv, VAR_STRING);
13648}
13649
13650/*
13651 * "libcallnr()" function
13652 */
13653 static void
13654f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *argvars;
13656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657{
13658 libcall_common(argvars, rettv, VAR_NUMBER);
13659}
13660
13661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 * "line(string)" function
13663 */
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668{
13669 linenr_T lnum = 0;
13670 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013671 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013673 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 if (fp != NULL)
13675 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677}
13678
13679/*
13680 * "line2byte(lnum)" function
13681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013683f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
13687#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#else
13690 linenr_T lnum;
13691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13697 if (rettv->vval.v_number >= 0)
13698 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#endif
13700}
13701
13702/*
13703 * "lispindent(lnum)" function
13704 */
13705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709{
13710#ifdef FEAT_LISP
13711 pos_T pos;
13712 linenr_T lnum;
13713
13714 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13717 {
13718 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 curwin->w_cursor = pos;
13721 }
13722 else
13723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725}
13726
13727/*
13728 * "localtime()" function
13729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736}
13737
Bram Moolenaar33570922005-01-25 22:26:29 +000013738static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739
13740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 typval_T *argvars;
13743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 int exact;
13745{
13746 char_u *keys;
13747 char_u *which;
13748 char_u buf[NUMBUFLEN];
13749 char_u *keys_buf = NULL;
13750 char_u *rhs;
13751 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013752 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013753 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013754 mapblock_T *mp;
13755 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756
13757 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 rettv->v_type = VAR_STRING;
13759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 if (*keys == NUL)
13763 return;
13764
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013765 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013769 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013770 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013771 if (argvars[3].v_type != VAR_UNKNOWN)
13772 get_dict = get_tv_number(&argvars[3]);
13773 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 else
13776 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 if (which == NULL)
13778 return;
13779
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 mode = get_map_mode(&which, 0);
13781
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013782 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013783 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013785
13786 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013788 /* Return a string. */
13789 if (rhs != NULL)
13790 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaarbd743252010-10-20 21:23:33 +020013792 }
13793 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13794 {
13795 /* Return a dictionary. */
13796 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13797 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13798 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
Bram Moolenaarbd743252010-10-20 21:23:33 +020013800 dict_add_nr_str(dict, "lhs", 0L, lhs);
13801 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13802 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13803 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13804 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13805 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13806 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013807 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013808 dict_add_nr_str(dict, "mode", 0L, mapmode);
13809
13810 vim_free(lhs);
13811 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 }
13813}
13814
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013815#ifdef FEAT_FLOAT
13816/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013817 * "log()" function
13818 */
13819 static void
13820f_log(argvars, rettv)
13821 typval_T *argvars;
13822 typval_T *rettv;
13823{
13824 float_T f;
13825
13826 rettv->v_type = VAR_FLOAT;
13827 if (get_float_arg(argvars, &f) == OK)
13828 rettv->vval.v_float = log(f);
13829 else
13830 rettv->vval.v_float = 0.0;
13831}
13832
13833/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013834 * "log10()" function
13835 */
13836 static void
13837f_log10(argvars, rettv)
13838 typval_T *argvars;
13839 typval_T *rettv;
13840{
13841 float_T f;
13842
13843 rettv->v_type = VAR_FLOAT;
13844 if (get_float_arg(argvars, &f) == OK)
13845 rettv->vval.v_float = log10(f);
13846 else
13847 rettv->vval.v_float = 0.0;
13848}
13849#endif
13850
Bram Moolenaar1dced572012-04-05 16:54:08 +020013851#ifdef FEAT_LUA
13852/*
13853 * "luaeval()" function
13854 */
13855 static void
13856f_luaeval(argvars, rettv)
13857 typval_T *argvars;
13858 typval_T *rettv;
13859{
13860 char_u *str;
13861 char_u buf[NUMBUFLEN];
13862
13863 str = get_tv_string_buf(&argvars[0], buf);
13864 do_luaeval(str, argvars + 1, rettv);
13865}
13866#endif
13867
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013869 * "map()" function
13870 */
13871 static void
13872f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013873 typval_T *argvars;
13874 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013875{
13876 filter_map(argvars, rettv, TRUE);
13877}
13878
13879/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013880 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 */
13882 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013883f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013884 typval_T *argvars;
13885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013887 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888}
13889
13890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 */
13893 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013894f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013895 typval_T *argvars;
13896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013898 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899}
13900
Bram Moolenaar33570922005-01-25 22:26:29 +000013901static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
13903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 typval_T *argvars;
13906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 int type;
13908{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013910 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013911 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 char_u *pat;
13913 regmatch_T regmatch;
13914 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 char_u *save_cpo;
13917 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013918 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013919 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013920 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013921 list_T *l = NULL;
13922 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013924 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925
13926 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13927 save_cpo = p_cpo;
13928 p_cpo = (char_u *)"";
13929
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 rettv->vval.v_number = -1;
13931 if (type == 3)
13932 {
13933 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013935 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013936 }
13937 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013939 rettv->v_type = VAR_STRING;
13940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013943 if (argvars[0].v_type == VAR_LIST)
13944 {
13945 if ((l = argvars[0].vval.v_list) == NULL)
13946 goto theend;
13947 li = l->lv_first;
13948 }
13949 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013950 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013952 len = (long)STRLEN(str);
13953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13956 if (pat == NULL)
13957 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013958
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013959 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 int error = FALSE;
13962
13963 start = get_tv_number_chk(&argvars[2], &error);
13964 if (error)
13965 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 if (l != NULL)
13967 {
13968 li = list_find(l, start);
13969 if (li == NULL)
13970 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013971 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013972 }
13973 else
13974 {
13975 if (start < 0)
13976 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013977 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013978 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013979 /* When "count" argument is there ignore matches before "start",
13980 * otherwise skip part of the string. Differs when pattern is "^"
13981 * or "\<". */
13982 if (argvars[3].v_type != VAR_UNKNOWN)
13983 startcol = start;
13984 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013985 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013986 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013987 len -= start;
13988 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013989 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013990
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 nth = get_tv_number_chk(&argvars[3], &error);
13993 if (error)
13994 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 }
13996
13997 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13998 if (regmatch.regprog != NULL)
13999 {
14000 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014001
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014002 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014003 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014004 if (l != NULL)
14005 {
14006 if (li == NULL)
14007 {
14008 match = FALSE;
14009 break;
14010 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014011 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014012 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014013 if (str == NULL)
14014 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014015 }
14016
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014017 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014018
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014020 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014021 if (l == NULL && !match)
14022 break;
14023
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014024 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025 if (l != NULL)
14026 {
14027 li = li->li_next;
14028 ++idx;
14029 }
14030 else
14031 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014032#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014033 startcol = (colnr_T)(regmatch.startp[0]
14034 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014035#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014036 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014037#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014038 if (startcol > (colnr_T)len
14039 || str + startcol <= regmatch.startp[0])
14040 {
14041 match = FALSE;
14042 break;
14043 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014045 }
14046
14047 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014049 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014051 int i;
14052
14053 /* return list with matched string and submatches */
14054 for (i = 0; i < NSUBEXP; ++i)
14055 {
14056 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014057 {
14058 if (list_append_string(rettv->vval.v_list,
14059 (char_u *)"", 0) == FAIL)
14060 break;
14061 }
14062 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014063 regmatch.startp[i],
14064 (int)(regmatch.endp[i] - regmatch.startp[i]))
14065 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014066 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014067 }
14068 }
14069 else if (type == 2)
14070 {
14071 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014072 if (l != NULL)
14073 copy_tv(&li->li_tv, rettv);
14074 else
14075 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077 }
14078 else if (l != NULL)
14079 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 else
14081 {
14082 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 (varnumber_T)(regmatch.startp[0] - str);
14085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014088 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 }
14090 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014091 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 }
14093
14094theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014095 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 p_cpo = save_cpo;
14097}
14098
14099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 * "match()" function
14101 */
14102 static void
14103f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014104 typval_T *argvars;
14105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106{
14107 find_some_match(argvars, rettv, 1);
14108}
14109
14110/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014111 * "matchadd()" function
14112 */
14113 static void
14114f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014115 typval_T *argvars UNUSED;
14116 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014117{
14118#ifdef FEAT_SEARCH_EXTRA
14119 char_u buf[NUMBUFLEN];
14120 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14121 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14122 int prio = 10; /* default priority */
14123 int id = -1;
14124 int error = FALSE;
14125
14126 rettv->vval.v_number = -1;
14127
14128 if (grp == NULL || pat == NULL)
14129 return;
14130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014131 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014132 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014133 if (argvars[3].v_type != VAR_UNKNOWN)
14134 id = get_tv_number_chk(&argvars[3], &error);
14135 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014136 if (error == TRUE)
14137 return;
14138 if (id >= 1 && id <= 3)
14139 {
14140 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14141 return;
14142 }
14143
14144 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14145#endif
14146}
14147
14148/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014149 * "matcharg()" function
14150 */
14151 static void
14152f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014153 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014154 typval_T *rettv;
14155{
14156 if (rettv_list_alloc(rettv) == OK)
14157 {
14158#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014159 int id = get_tv_number(&argvars[0]);
14160 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014161
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014162 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014163 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014164 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14165 {
14166 list_append_string(rettv->vval.v_list,
14167 syn_id2name(m->hlg_id), -1);
14168 list_append_string(rettv->vval.v_list, m->pattern, -1);
14169 }
14170 else
14171 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014172 list_append_string(rettv->vval.v_list, NULL, -1);
14173 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014174 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014175 }
14176#endif
14177 }
14178}
14179
14180/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014181 * "matchdelete()" function
14182 */
14183 static void
14184f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014185 typval_T *argvars UNUSED;
14186 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014187{
14188#ifdef FEAT_SEARCH_EXTRA
14189 rettv->vval.v_number = match_delete(curwin,
14190 (int)get_tv_number(&argvars[0]), TRUE);
14191#endif
14192}
14193
14194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195 * "matchend()" function
14196 */
14197 static void
14198f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 typval_T *argvars;
14200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014201{
14202 find_some_match(argvars, rettv, 0);
14203}
14204
14205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014206 * "matchlist()" function
14207 */
14208 static void
14209f_matchlist(argvars, rettv)
14210 typval_T *argvars;
14211 typval_T *rettv;
14212{
14213 find_some_match(argvars, rettv, 3);
14214}
14215
14216/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217 * "matchstr()" function
14218 */
14219 static void
14220f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014221 typval_T *argvars;
14222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223{
14224 find_some_match(argvars, rettv, 2);
14225}
14226
Bram Moolenaar33570922005-01-25 22:26:29 +000014227static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014228
14229 static void
14230max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *argvars;
14232 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014233 int domax;
14234{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014235 long n = 0;
14236 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014238
14239 if (argvars[0].v_type == VAR_LIST)
14240 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014241 list_T *l;
14242 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014243
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014244 l = argvars[0].vval.v_list;
14245 if (l != NULL)
14246 {
14247 li = l->lv_first;
14248 if (li != NULL)
14249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014251 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014252 {
14253 li = li->li_next;
14254 if (li == NULL)
14255 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014257 if (domax ? i > n : i < n)
14258 n = i;
14259 }
14260 }
14261 }
14262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014263 else if (argvars[0].v_type == VAR_DICT)
14264 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014265 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014266 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014267 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014268 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014269
14270 d = argvars[0].vval.v_dict;
14271 if (d != NULL)
14272 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014273 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014276 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014278 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014280 if (first)
14281 {
14282 n = i;
14283 first = FALSE;
14284 }
14285 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014286 n = i;
14287 }
14288 }
14289 }
14290 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014291 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014292 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014294}
14295
14296/*
14297 * "max()" function
14298 */
14299 static void
14300f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 typval_T *argvars;
14302 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014303{
14304 max_min(argvars, rettv, TRUE);
14305}
14306
14307/*
14308 * "min()" function
14309 */
14310 static void
14311f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014312 typval_T *argvars;
14313 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014314{
14315 max_min(argvars, rettv, FALSE);
14316}
14317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014318static int mkdir_recurse __ARGS((char_u *dir, int prot));
14319
14320/*
14321 * Create the directory in which "dir" is located, and higher levels when
14322 * needed.
14323 */
14324 static int
14325mkdir_recurse(dir, prot)
14326 char_u *dir;
14327 int prot;
14328{
14329 char_u *p;
14330 char_u *updir;
14331 int r = FAIL;
14332
14333 /* Get end of directory name in "dir".
14334 * We're done when it's "/" or "c:/". */
14335 p = gettail_sep(dir);
14336 if (p <= get_past_head(dir))
14337 return OK;
14338
14339 /* If the directory exists we're done. Otherwise: create it.*/
14340 updir = vim_strnsave(dir, (int)(p - dir));
14341 if (updir == NULL)
14342 return FAIL;
14343 if (mch_isdir(updir))
14344 r = OK;
14345 else if (mkdir_recurse(updir, prot) == OK)
14346 r = vim_mkdir_emsg(updir, prot);
14347 vim_free(updir);
14348 return r;
14349}
14350
14351#ifdef vim_mkdir
14352/*
14353 * "mkdir()" function
14354 */
14355 static void
14356f_mkdir(argvars, rettv)
14357 typval_T *argvars;
14358 typval_T *rettv;
14359{
14360 char_u *dir;
14361 char_u buf[NUMBUFLEN];
14362 int prot = 0755;
14363
14364 rettv->vval.v_number = FAIL;
14365 if (check_restricted() || check_secure())
14366 return;
14367
14368 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014369 if (*dir == NUL)
14370 rettv->vval.v_number = FAIL;
14371 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014372 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014373 if (*gettail(dir) == NUL)
14374 /* remove trailing slashes */
14375 *gettail_sep(dir) = NUL;
14376
14377 if (argvars[1].v_type != VAR_UNKNOWN)
14378 {
14379 if (argvars[2].v_type != VAR_UNKNOWN)
14380 prot = get_tv_number_chk(&argvars[2], NULL);
14381 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14382 mkdir_recurse(dir, prot);
14383 }
14384 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014385 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014386}
14387#endif
14388
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 * "mode()" function
14391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014394 typval_T *argvars;
14395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014397 char_u buf[3];
14398
14399 buf[1] = NUL;
14400 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 if (VIsual_active)
14403 {
14404 if (VIsual_select)
14405 buf[0] = VIsual_mode + 's' - 'v';
14406 else
14407 buf[0] = VIsual_mode;
14408 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014409 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014410 || State == CONFIRM)
14411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014413 if (State == ASKMORE)
14414 buf[1] = 'm';
14415 else if (State == CONFIRM)
14416 buf[1] = '?';
14417 }
14418 else if (State == EXTERNCMD)
14419 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 else if (State & INSERT)
14421 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014422#ifdef FEAT_VREPLACE
14423 if (State & VREPLACE_FLAG)
14424 {
14425 buf[0] = 'R';
14426 buf[1] = 'v';
14427 }
14428 else
14429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 if (State & REPLACE_FLAG)
14431 buf[0] = 'R';
14432 else
14433 buf[0] = 'i';
14434 }
14435 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014438 if (exmode_active)
14439 buf[1] = 'v';
14440 }
14441 else if (exmode_active)
14442 {
14443 buf[0] = 'c';
14444 buf[1] = 'e';
14445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014449 if (finish_op)
14450 buf[1] = 'o';
14451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014453 /* Clear out the minor mode when the argument is not a non-zero number or
14454 * non-empty string. */
14455 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014456 buf[1] = NUL;
14457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->vval.v_string = vim_strsave(buf);
14459 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460}
14461
Bram Moolenaar429fa852013-04-15 12:27:36 +020014462#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014463/*
14464 * "mzeval()" function
14465 */
14466 static void
14467f_mzeval(argvars, rettv)
14468 typval_T *argvars;
14469 typval_T *rettv;
14470{
14471 char_u *str;
14472 char_u buf[NUMBUFLEN];
14473
14474 str = get_tv_string_buf(&argvars[0], buf);
14475 do_mzeval(str, rettv);
14476}
Bram Moolenaar75676462013-01-30 14:55:42 +010014477
14478 void
14479mzscheme_call_vim(name, args, rettv)
14480 char_u *name;
14481 typval_T *args;
14482 typval_T *rettv;
14483{
14484 typval_T argvars[3];
14485
14486 argvars[0].v_type = VAR_STRING;
14487 argvars[0].vval.v_string = name;
14488 copy_tv(args, &argvars[1]);
14489 argvars[2].v_type = VAR_UNKNOWN;
14490 f_call(argvars, rettv);
14491 clear_tv(&argvars[1]);
14492}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014493#endif
14494
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 * "nextnonblank()" function
14497 */
14498 static void
14499f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *argvars;
14501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502{
14503 linenr_T lnum;
14504
14505 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014507 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014508 {
14509 lnum = 0;
14510 break;
14511 }
14512 if (*skipwhite(ml_get(lnum)) != NUL)
14513 break;
14514 }
14515 rettv->vval.v_number = lnum;
14516}
14517
14518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 * "nr2char()" function
14520 */
14521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *argvars;
14524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525{
14526 char_u buf[NUMBUFLEN];
14527
14528#ifdef FEAT_MBYTE
14529 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014530 {
14531 int utf8 = 0;
14532
14533 if (argvars[1].v_type != VAR_UNKNOWN)
14534 utf8 = get_tv_number_chk(&argvars[1], NULL);
14535 if (utf8)
14536 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14537 else
14538 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 else
14541#endif
14542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 buf[1] = NUL;
14545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->v_type = VAR_STRING;
14547 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014548}
14549
14550/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014551 * "or(expr, expr)" function
14552 */
14553 static void
14554f_or(argvars, rettv)
14555 typval_T *argvars;
14556 typval_T *rettv;
14557{
14558 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14559 | get_tv_number_chk(&argvars[1], NULL);
14560}
14561
14562/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014563 * "pathshorten()" function
14564 */
14565 static void
14566f_pathshorten(argvars, rettv)
14567 typval_T *argvars;
14568 typval_T *rettv;
14569{
14570 char_u *p;
14571
14572 rettv->v_type = VAR_STRING;
14573 p = get_tv_string_chk(&argvars[0]);
14574 if (p == NULL)
14575 rettv->vval.v_string = NULL;
14576 else
14577 {
14578 p = vim_strsave(p);
14579 rettv->vval.v_string = p;
14580 if (p != NULL)
14581 shorten_dir(p);
14582 }
14583}
14584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014585#ifdef FEAT_FLOAT
14586/*
14587 * "pow()" function
14588 */
14589 static void
14590f_pow(argvars, rettv)
14591 typval_T *argvars;
14592 typval_T *rettv;
14593{
14594 float_T fx, fy;
14595
14596 rettv->v_type = VAR_FLOAT;
14597 if (get_float_arg(argvars, &fx) == OK
14598 && get_float_arg(&argvars[1], &fy) == OK)
14599 rettv->vval.v_float = pow(fx, fy);
14600 else
14601 rettv->vval.v_float = 0.0;
14602}
14603#endif
14604
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014605/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 * "prevnonblank()" function
14607 */
14608 static void
14609f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612{
14613 linenr_T lnum;
14614
14615 lnum = get_tv_lnum(argvars);
14616 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14617 lnum = 0;
14618 else
14619 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14620 --lnum;
14621 rettv->vval.v_number = lnum;
14622}
14623
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014624#ifdef HAVE_STDARG_H
14625/* This dummy va_list is here because:
14626 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14627 * - locally in the function results in a "used before set" warning
14628 * - using va_start() to initialize it gives "function with fixed args" error */
14629static va_list ap;
14630#endif
14631
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014633 * "printf()" function
14634 */
14635 static void
14636f_printf(argvars, rettv)
14637 typval_T *argvars;
14638 typval_T *rettv;
14639{
14640 rettv->v_type = VAR_STRING;
14641 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014642#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014643 {
14644 char_u buf[NUMBUFLEN];
14645 int len;
14646 char_u *s;
14647 int saved_did_emsg = did_emsg;
14648 char *fmt;
14649
14650 /* Get the required length, allocate the buffer and do it for real. */
14651 did_emsg = FALSE;
14652 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014653 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014654 if (!did_emsg)
14655 {
14656 s = alloc(len + 1);
14657 if (s != NULL)
14658 {
14659 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014660 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014661 }
14662 }
14663 did_emsg |= saved_did_emsg;
14664 }
14665#endif
14666}
14667
14668/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014669 * "pumvisible()" function
14670 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014671 static void
14672f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014673 typval_T *argvars UNUSED;
14674 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014675{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014676#ifdef FEAT_INS_EXPAND
14677 if (pum_visible())
14678 rettv->vval.v_number = 1;
14679#endif
14680}
14681
Bram Moolenaardb913952012-06-29 12:54:53 +020014682#ifdef FEAT_PYTHON3
14683/*
14684 * "py3eval()" function
14685 */
14686 static void
14687f_py3eval(argvars, rettv)
14688 typval_T *argvars;
14689 typval_T *rettv;
14690{
14691 char_u *str;
14692 char_u buf[NUMBUFLEN];
14693
14694 str = get_tv_string_buf(&argvars[0], buf);
14695 do_py3eval(str, rettv);
14696}
14697#endif
14698
14699#ifdef FEAT_PYTHON
14700/*
14701 * "pyeval()" function
14702 */
14703 static void
14704f_pyeval(argvars, rettv)
14705 typval_T *argvars;
14706 typval_T *rettv;
14707{
14708 char_u *str;
14709 char_u buf[NUMBUFLEN];
14710
14711 str = get_tv_string_buf(&argvars[0], buf);
14712 do_pyeval(str, rettv);
14713}
14714#endif
14715
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717 * "range()" function
14718 */
14719 static void
14720f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *argvars;
14722 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014723{
14724 long start;
14725 long end;
14726 long stride = 1;
14727 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014730 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014731 if (argvars[1].v_type == VAR_UNKNOWN)
14732 {
14733 end = start - 1;
14734 start = 0;
14735 }
14736 else
14737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014741 }
14742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014743 if (error)
14744 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014745 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014746 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014747 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014748 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014749 else
14750 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014751 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014752 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014753 if (list_append_number(rettv->vval.v_list,
14754 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014755 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014756 }
14757}
14758
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759/*
14760 * "readfile()" function
14761 */
14762 static void
14763f_readfile(argvars, rettv)
14764 typval_T *argvars;
14765 typval_T *rettv;
14766{
14767 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769 char_u *fname;
14770 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014771 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14772 int io_size = sizeof(buf);
14773 int readlen; /* size of last fread() */
14774 char_u *prev = NULL; /* previously read bytes, if any */
14775 long prevlen = 0; /* length of data in prev */
14776 long prevsize = 0; /* size of prev buffer */
14777 long maxline = MAXLNUM;
14778 long cnt = 0;
14779 char_u *p; /* position in buf */
14780 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014782 if (argvars[1].v_type != VAR_UNKNOWN)
14783 {
14784 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14785 binary = TRUE;
14786 if (argvars[2].v_type != VAR_UNKNOWN)
14787 maxline = get_tv_number(&argvars[2]);
14788 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014790 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792
14793 /* Always open the file in binary mode, library functions have a mind of
14794 * their own about CR-LF conversion. */
14795 fname = get_tv_string(&argvars[0]);
14796 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14797 {
14798 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14799 return;
14800 }
14801
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014802 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014803 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014804 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014805
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 /* This for loop processes what was read, but is also entered at end
14807 * of file so that either:
14808 * - an incomplete line gets written
14809 * - a "binary" file gets an empty line at the end if it ends in a
14810 * newline. */
14811 for (p = buf, start = buf;
14812 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14813 ++p)
14814 {
14815 if (*p == '\n' || readlen <= 0)
14816 {
14817 listitem_T *li;
14818 char_u *s = NULL;
14819 long_u len = p - start;
14820
14821 /* Finished a line. Remove CRs before NL. */
14822 if (readlen > 0 && !binary)
14823 {
14824 while (len > 0 && start[len - 1] == '\r')
14825 --len;
14826 /* removal may cross back to the "prev" string */
14827 if (len == 0)
14828 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14829 --prevlen;
14830 }
14831 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014832 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014833 else
14834 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014835 /* Change "prev" buffer to be the right size. This way
14836 * the bytes are only copied once, and very long lines are
14837 * allocated only once. */
14838 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014842 prev = NULL; /* the list will own the string */
14843 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014844 }
14845 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014846 if (s == NULL)
14847 {
14848 do_outofmem_msg((long_u) prevlen + len + 1);
14849 failed = TRUE;
14850 break;
14851 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014852
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014853 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854 {
14855 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014856 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 break;
14858 }
14859 li->li_tv.v_type = VAR_STRING;
14860 li->li_tv.v_lock = 0;
14861 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014862 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014863
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014864 start = p + 1; /* step over newline */
14865 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014866 break;
14867 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014868 else if (*p == NUL)
14869 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014870#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014871 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14872 * when finding the BF and check the previous two bytes. */
14873 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014874 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014875 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14876 * + 1, these may be in the "prev" string. */
14877 char_u back1 = p >= buf + 1 ? p[-1]
14878 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14879 char_u back2 = p >= buf + 2 ? p[-2]
14880 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14881 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014882
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014883 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014884 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014885 char_u *dest = p - 2;
14886
14887 /* Usually a BOM is at the beginning of a file, and so at
14888 * the beginning of a line; then we can just step over it.
14889 */
14890 if (start == dest)
14891 start = p + 1;
14892 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014893 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014894 /* have to shuffle buf to close gap */
14895 int adjust_prevlen = 0;
14896
14897 if (dest < buf)
14898 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014899 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014900 dest = buf;
14901 }
14902 if (readlen > p - buf + 1)
14903 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14904 readlen -= 3 - adjust_prevlen;
14905 prevlen -= adjust_prevlen;
14906 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014907 }
14908 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014909 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014910#endif
14911 } /* for */
14912
14913 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14914 break;
14915 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014916 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014917 /* There's part of a line in buf, store it in "prev". */
14918 if (p - start + prevlen >= prevsize)
14919 {
14920 /* need bigger "prev" buffer */
14921 char_u *newprev;
14922
14923 /* A common use case is ordinary text files and "prev" gets a
14924 * fragment of a line, so the first allocation is made
14925 * small, to avoid repeatedly 'allocing' large and
14926 * 'reallocing' small. */
14927 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014928 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014929 else
14930 {
14931 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014932 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014933 prevsize = grow50pc > growmin ? grow50pc : growmin;
14934 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014935 newprev = prev == NULL ? alloc(prevsize)
14936 : vim_realloc(prev, prevsize);
14937 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014938 {
14939 do_outofmem_msg((long_u)prevsize);
14940 failed = TRUE;
14941 break;
14942 }
14943 prev = newprev;
14944 }
14945 /* Add the line part to end of "prev". */
14946 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014947 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014948 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014950
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014951 /*
14952 * For a negative line count use only the lines at the end of the file,
14953 * free the rest.
14954 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014955 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014956 while (cnt > -maxline)
14957 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014958 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014959 --cnt;
14960 }
14961
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014962 if (failed)
14963 {
14964 list_free(rettv->vval.v_list, TRUE);
14965 /* readfile doc says an empty list is returned on error */
14966 rettv->vval.v_list = list_alloc();
14967 }
14968
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014969 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014970 fclose(fd);
14971}
14972
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014973#if defined(FEAT_RELTIME)
14974static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14975
14976/*
14977 * Convert a List to proftime_T.
14978 * Return FAIL when there is something wrong.
14979 */
14980 static int
14981list2proftime(arg, tm)
14982 typval_T *arg;
14983 proftime_T *tm;
14984{
14985 long n1, n2;
14986 int error = FALSE;
14987
14988 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14989 || arg->vval.v_list->lv_len != 2)
14990 return FAIL;
14991 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14992 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14993# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014994 tm->HighPart = n1;
14995 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014996# else
14997 tm->tv_sec = n1;
14998 tm->tv_usec = n2;
14999# endif
15000 return error ? FAIL : OK;
15001}
15002#endif /* FEAT_RELTIME */
15003
15004/*
15005 * "reltime()" function
15006 */
15007 static void
15008f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015009 typval_T *argvars UNUSED;
15010 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015011{
15012#ifdef FEAT_RELTIME
15013 proftime_T res;
15014 proftime_T start;
15015
15016 if (argvars[0].v_type == VAR_UNKNOWN)
15017 {
15018 /* No arguments: get current time. */
15019 profile_start(&res);
15020 }
15021 else if (argvars[1].v_type == VAR_UNKNOWN)
15022 {
15023 if (list2proftime(&argvars[0], &res) == FAIL)
15024 return;
15025 profile_end(&res);
15026 }
15027 else
15028 {
15029 /* Two arguments: compute the difference. */
15030 if (list2proftime(&argvars[0], &start) == FAIL
15031 || list2proftime(&argvars[1], &res) == FAIL)
15032 return;
15033 profile_sub(&res, &start);
15034 }
15035
15036 if (rettv_list_alloc(rettv) == OK)
15037 {
15038 long n1, n2;
15039
15040# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015041 n1 = res.HighPart;
15042 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015043# else
15044 n1 = res.tv_sec;
15045 n2 = res.tv_usec;
15046# endif
15047 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15048 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15049 }
15050#endif
15051}
15052
15053/*
15054 * "reltimestr()" function
15055 */
15056 static void
15057f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015058 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015059 typval_T *rettv;
15060{
15061#ifdef FEAT_RELTIME
15062 proftime_T tm;
15063#endif
15064
15065 rettv->v_type = VAR_STRING;
15066 rettv->vval.v_string = NULL;
15067#ifdef FEAT_RELTIME
15068 if (list2proftime(&argvars[0], &tm) == OK)
15069 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15070#endif
15071}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015072
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15074static void make_connection __ARGS((void));
15075static int check_connection __ARGS((void));
15076
15077 static void
15078make_connection()
15079{
15080 if (X_DISPLAY == NULL
15081# ifdef FEAT_GUI
15082 && !gui.in_use
15083# endif
15084 )
15085 {
15086 x_force_connect = TRUE;
15087 setup_term_clip();
15088 x_force_connect = FALSE;
15089 }
15090}
15091
15092 static int
15093check_connection()
15094{
15095 make_connection();
15096 if (X_DISPLAY == NULL)
15097 {
15098 EMSG(_("E240: No connection to Vim server"));
15099 return FAIL;
15100 }
15101 return OK;
15102}
15103#endif
15104
15105#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015106static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107
15108 static void
15109remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015110 typval_T *argvars;
15111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 int expr;
15113{
15114 char_u *server_name;
15115 char_u *keys;
15116 char_u *r = NULL;
15117 char_u buf[NUMBUFLEN];
15118# ifdef WIN32
15119 HWND w;
15120# else
15121 Window w;
15122# endif
15123
15124 if (check_restricted() || check_secure())
15125 return;
15126
15127# ifdef FEAT_X11
15128 if (check_connection() == FAIL)
15129 return;
15130# endif
15131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 server_name = get_tv_string_chk(&argvars[0]);
15133 if (server_name == NULL)
15134 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 keys = get_tv_string_buf(&argvars[1], buf);
15136# ifdef WIN32
15137 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15138# else
15139 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15140 < 0)
15141# endif
15142 {
15143 if (r != NULL)
15144 EMSG(r); /* sending worked but evaluation failed */
15145 else
15146 EMSG2(_("E241: Unable to send to %s"), server_name);
15147 return;
15148 }
15149
15150 rettv->vval.v_string = r;
15151
15152 if (argvars[2].v_type != VAR_UNKNOWN)
15153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015154 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015155 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015156 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015158 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015159 v.di_tv.v_type = VAR_STRING;
15160 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 idvar = get_tv_string_chk(&argvars[2]);
15162 if (idvar != NULL)
15163 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015164 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 }
15166}
15167#endif
15168
15169/*
15170 * "remote_expr()" function
15171 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 static void
15173f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176{
15177 rettv->v_type = VAR_STRING;
15178 rettv->vval.v_string = NULL;
15179#ifdef FEAT_CLIENTSERVER
15180 remote_common(argvars, rettv, TRUE);
15181#endif
15182}
15183
15184/*
15185 * "remote_foreground()" function
15186 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187 static void
15188f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015189 typval_T *argvars UNUSED;
15190 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192#ifdef FEAT_CLIENTSERVER
15193# ifdef WIN32
15194 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195 {
15196 char_u *server_name = get_tv_string_chk(&argvars[0]);
15197
15198 if (server_name != NULL)
15199 serverForeground(server_name);
15200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201# else
15202 /* Send a foreground() expression to the server. */
15203 argvars[1].v_type = VAR_STRING;
15204 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15205 argvars[2].v_type = VAR_UNKNOWN;
15206 remote_common(argvars, rettv, TRUE);
15207 vim_free(argvars[1].vval.v_string);
15208# endif
15209#endif
15210}
15211
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212 static void
15213f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015214 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216{
15217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 char_u *s = NULL;
15220# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015221 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015223 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224
15225 if (check_restricted() || check_secure())
15226 {
15227 rettv->vval.v_number = -1;
15228 return;
15229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 serverid = get_tv_string_chk(&argvars[0]);
15231 if (serverid == NULL)
15232 {
15233 rettv->vval.v_number = -1;
15234 return; /* type error; errmsg already given */
15235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015237 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015238 if (n == 0)
15239 rettv->vval.v_number = -1;
15240 else
15241 {
15242 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15243 rettv->vval.v_number = (s != NULL);
15244 }
15245# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 if (check_connection() == FAIL)
15247 return;
15248
15249 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251# endif
15252
15253 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 char_u *retvar;
15256
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 v.di_tv.v_type = VAR_STRING;
15258 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015259 retvar = get_tv_string_chk(&argvars[1]);
15260 if (retvar != NULL)
15261 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 }
15264#else
15265 rettv->vval.v_number = -1;
15266#endif
15267}
15268
Bram Moolenaar0d660222005-01-07 21:51:51 +000015269 static void
15270f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015271 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015273{
15274 char_u *r = NULL;
15275
15276#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015277 char_u *serverid = get_tv_string_chk(&argvars[0]);
15278
15279 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 {
15281# ifdef WIN32
15282 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015283 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015285 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286 if (n != 0)
15287 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15288 if (r == NULL)
15289# else
15290 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292# endif
15293 EMSG(_("E277: Unable to read a server reply"));
15294 }
15295#endif
15296 rettv->v_type = VAR_STRING;
15297 rettv->vval.v_string = r;
15298}
15299
15300/*
15301 * "remote_send()" function
15302 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015303 static void
15304f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015305 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307{
15308 rettv->v_type = VAR_STRING;
15309 rettv->vval.v_string = NULL;
15310#ifdef FEAT_CLIENTSERVER
15311 remote_common(argvars, rettv, FALSE);
15312#endif
15313}
15314
15315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015317 */
15318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015320 typval_T *argvars;
15321 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015322{
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 list_T *l;
15324 listitem_T *item, *item2;
15325 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015326 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015327 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015328 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015329 dict_T *d;
15330 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015331 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015332
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 if (argvars[0].v_type == VAR_DICT)
15334 {
15335 if (argvars[2].v_type != VAR_UNKNOWN)
15336 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015337 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015338 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015340 key = get_tv_string_chk(&argvars[1]);
15341 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015343 di = dict_find(d, key, -1);
15344 if (di == NULL)
15345 EMSG2(_(e_dictkey), key);
15346 else
15347 {
15348 *rettv = di->di_tv;
15349 init_tv(&di->di_tv);
15350 dictitem_remove(d, di);
15351 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015353 }
15354 }
15355 else if (argvars[0].v_type != VAR_LIST)
15356 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015357 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015358 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 int error = FALSE;
15361
15362 idx = get_tv_number_chk(&argvars[1], &error);
15363 if (error)
15364 ; /* type error: do nothing, errmsg already given */
15365 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015366 EMSGN(_(e_listidx), idx);
15367 else
15368 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015369 if (argvars[2].v_type == VAR_UNKNOWN)
15370 {
15371 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015372 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015373 *rettv = item->li_tv;
15374 vim_free(item);
15375 }
15376 else
15377 {
15378 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015379 end = get_tv_number_chk(&argvars[2], &error);
15380 if (error)
15381 ; /* type error: do nothing */
15382 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015383 EMSGN(_(e_listidx), end);
15384 else
15385 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015386 int cnt = 0;
15387
15388 for (li = item; li != NULL; li = li->li_next)
15389 {
15390 ++cnt;
15391 if (li == item2)
15392 break;
15393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015394 if (li == NULL) /* didn't find "item2" after "item" */
15395 EMSG(_(e_invrange));
15396 else
15397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015398 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015399 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015400 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015401 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015402 l->lv_first = item;
15403 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015404 item->li_prev = NULL;
15405 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015406 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015407 }
15408 }
15409 }
15410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015411 }
15412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413}
15414
15415/*
15416 * "rename({from}, {to})" function
15417 */
15418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015420 typval_T *argvars;
15421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422{
15423 char_u buf[NUMBUFLEN];
15424
15425 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015428 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15429 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015433 * "repeat()" function
15434 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015435 static void
15436f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 typval_T *argvars;
15438 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015439{
15440 char_u *p;
15441 int n;
15442 int slen;
15443 int len;
15444 char_u *r;
15445 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015446
15447 n = get_tv_number(&argvars[1]);
15448 if (argvars[0].v_type == VAR_LIST)
15449 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015450 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015451 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015452 if (list_extend(rettv->vval.v_list,
15453 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015454 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015455 }
15456 else
15457 {
15458 p = get_tv_string(&argvars[0]);
15459 rettv->v_type = VAR_STRING;
15460 rettv->vval.v_string = NULL;
15461
15462 slen = (int)STRLEN(p);
15463 len = slen * n;
15464 if (len <= 0)
15465 return;
15466
15467 r = alloc(len + 1);
15468 if (r != NULL)
15469 {
15470 for (i = 0; i < n; i++)
15471 mch_memmove(r + i * slen, p, (size_t)slen);
15472 r[len] = NUL;
15473 }
15474
15475 rettv->vval.v_string = r;
15476 }
15477}
15478
15479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 * "resolve()" function
15481 */
15482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 typval_T *argvars;
15485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486{
15487 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015488#ifdef HAVE_READLINK
15489 char_u *buf = NULL;
15490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493#ifdef FEAT_SHORTCUT
15494 {
15495 char_u *v = NULL;
15496
15497 v = mch_resolve_shortcut(p);
15498 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 }
15503#else
15504# ifdef HAVE_READLINK
15505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 char_u *cpy;
15507 int len;
15508 char_u *remain = NULL;
15509 char_u *q;
15510 int is_relative_to_current = FALSE;
15511 int has_trailing_pathsep = FALSE;
15512 int limit = 100;
15513
15514 p = vim_strsave(p);
15515
15516 if (p[0] == '.' && (vim_ispathsep(p[1])
15517 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15518 is_relative_to_current = TRUE;
15519
15520 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015521 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015524 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526
15527 q = getnextcomp(p);
15528 if (*q != NUL)
15529 {
15530 /* Separate the first path component in "p", and keep the
15531 * remainder (beginning with the path separator). */
15532 remain = vim_strsave(q - 1);
15533 q[-1] = NUL;
15534 }
15535
Bram Moolenaard9462e32011-04-11 21:35:11 +020015536 buf = alloc(MAXPATHL + 1);
15537 if (buf == NULL)
15538 goto fail;
15539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 for (;;)
15541 {
15542 for (;;)
15543 {
15544 len = readlink((char *)p, (char *)buf, MAXPATHL);
15545 if (len <= 0)
15546 break;
15547 buf[len] = NUL;
15548
15549 if (limit-- == 0)
15550 {
15551 vim_free(p);
15552 vim_free(remain);
15553 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 goto fail;
15556 }
15557
15558 /* Ensure that the result will have a trailing path separator
15559 * if the argument has one. */
15560 if (remain == NULL && has_trailing_pathsep)
15561 add_pathsep(buf);
15562
15563 /* Separate the first path component in the link value and
15564 * concatenate the remainders. */
15565 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15566 if (*q != NUL)
15567 {
15568 if (remain == NULL)
15569 remain = vim_strsave(q - 1);
15570 else
15571 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015572 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 if (cpy != NULL)
15574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 vim_free(remain);
15576 remain = cpy;
15577 }
15578 }
15579 q[-1] = NUL;
15580 }
15581
15582 q = gettail(p);
15583 if (q > p && *q == NUL)
15584 {
15585 /* Ignore trailing path separator. */
15586 q[-1] = NUL;
15587 q = gettail(p);
15588 }
15589 if (q > p && !mch_isFullName(buf))
15590 {
15591 /* symlink is relative to directory of argument */
15592 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15593 if (cpy != NULL)
15594 {
15595 STRCPY(cpy, p);
15596 STRCPY(gettail(cpy), buf);
15597 vim_free(p);
15598 p = cpy;
15599 }
15600 }
15601 else
15602 {
15603 vim_free(p);
15604 p = vim_strsave(buf);
15605 }
15606 }
15607
15608 if (remain == NULL)
15609 break;
15610
15611 /* Append the first path component of "remain" to "p". */
15612 q = getnextcomp(remain + 1);
15613 len = q - remain - (*q != NUL);
15614 cpy = vim_strnsave(p, STRLEN(p) + len);
15615 if (cpy != NULL)
15616 {
15617 STRNCAT(cpy, remain, len);
15618 vim_free(p);
15619 p = cpy;
15620 }
15621 /* Shorten "remain". */
15622 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015623 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 else
15625 {
15626 vim_free(remain);
15627 remain = NULL;
15628 }
15629 }
15630
15631 /* If the result is a relative path name, make it explicitly relative to
15632 * the current directory if and only if the argument had this form. */
15633 if (!vim_ispathsep(*p))
15634 {
15635 if (is_relative_to_current
15636 && *p != NUL
15637 && !(p[0] == '.'
15638 && (p[1] == NUL
15639 || vim_ispathsep(p[1])
15640 || (p[1] == '.'
15641 && (p[2] == NUL
15642 || vim_ispathsep(p[2]))))))
15643 {
15644 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015645 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 if (cpy != NULL)
15647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 vim_free(p);
15649 p = cpy;
15650 }
15651 }
15652 else if (!is_relative_to_current)
15653 {
15654 /* Strip leading "./". */
15655 q = p;
15656 while (q[0] == '.' && vim_ispathsep(q[1]))
15657 q += 2;
15658 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015659 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 }
15661 }
15662
15663 /* Ensure that the result will have no trailing path separator
15664 * if the argument had none. But keep "/" or "//". */
15665 if (!has_trailing_pathsep)
15666 {
15667 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015668 if (after_pathsep(p, q))
15669 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 }
15671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 }
15674# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015675 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676# endif
15677#endif
15678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
15681#ifdef HAVE_READLINK
15682fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015683 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015685 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686}
15687
15688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 */
15691 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015693 typval_T *argvars;
15694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695{
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 list_T *l;
15697 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 if (argvars[0].v_type != VAR_LIST)
15700 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015701 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015702 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015703 {
15704 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015705 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015706 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707 while (li != NULL)
15708 {
15709 ni = li->li_prev;
15710 list_append(l, li);
15711 li = ni;
15712 }
15713 rettv->vval.v_list = l;
15714 rettv->v_type = VAR_LIST;
15715 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015716 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718}
15719
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015720#define SP_NOMOVE 0x01 /* don't move cursor */
15721#define SP_REPEAT 0x02 /* repeat to find outer pair */
15722#define SP_RETCOUNT 0x04 /* return matchcount */
15723#define SP_SETPCMARK 0x08 /* set previous context mark */
15724#define SP_START 0x10 /* accept match at start position */
15725#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15726#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015727
Bram Moolenaar33570922005-01-25 22:26:29 +000015728static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729
15730/*
15731 * Get flags for a search function.
15732 * Possibly sets "p_ws".
15733 * Returns BACKWARD, FORWARD or zero (for an error).
15734 */
15735 static int
15736get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738 int *flagsp;
15739{
15740 int dir = FORWARD;
15741 char_u *flags;
15742 char_u nbuf[NUMBUFLEN];
15743 int mask;
15744
15745 if (varp->v_type != VAR_UNKNOWN)
15746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015747 flags = get_tv_string_buf_chk(varp, nbuf);
15748 if (flags == NULL)
15749 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750 while (*flags != NUL)
15751 {
15752 switch (*flags)
15753 {
15754 case 'b': dir = BACKWARD; break;
15755 case 'w': p_ws = TRUE; break;
15756 case 'W': p_ws = FALSE; break;
15757 default: mask = 0;
15758 if (flagsp != NULL)
15759 switch (*flags)
15760 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015761 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015762 case 'e': mask = SP_END; break;
15763 case 'm': mask = SP_RETCOUNT; break;
15764 case 'n': mask = SP_NOMOVE; break;
15765 case 'p': mask = SP_SUBPAT; break;
15766 case 'r': mask = SP_REPEAT; break;
15767 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 }
15769 if (mask == 0)
15770 {
15771 EMSG2(_(e_invarg2), flags);
15772 dir = 0;
15773 }
15774 else
15775 *flagsp |= mask;
15776 }
15777 if (dir == 0)
15778 break;
15779 ++flags;
15780 }
15781 }
15782 return dir;
15783}
15784
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015786 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015788 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015789search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015791 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015792 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015794 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 char_u *pat;
15796 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015797 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 int save_p_ws = p_ws;
15799 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015800 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015801 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015802 proftime_T tm;
15803#ifdef FEAT_RELTIME
15804 long time_limit = 0;
15805#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015806 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015807 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015810 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015811 if (dir == 0)
15812 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015813 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015814 if (flags & SP_START)
15815 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015816 if (flags & SP_END)
15817 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818
Bram Moolenaar76929292008-01-06 19:07:36 +000015819 /* Optional arguments: line number to stop searching and timeout. */
15820 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015821 {
15822 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15823 if (lnum_stop < 0)
15824 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015825#ifdef FEAT_RELTIME
15826 if (argvars[3].v_type != VAR_UNKNOWN)
15827 {
15828 time_limit = get_tv_number_chk(&argvars[3], NULL);
15829 if (time_limit < 0)
15830 goto theend;
15831 }
15832#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015833 }
15834
Bram Moolenaar76929292008-01-06 19:07:36 +000015835#ifdef FEAT_RELTIME
15836 /* Set the time limit, if there is one. */
15837 profile_setlimit(time_limit, &tm);
15838#endif
15839
Bram Moolenaar231334e2005-07-25 20:46:57 +000015840 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015841 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015842 * Check to make sure only those flags are set.
15843 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15844 * flags cannot be set. Check for that condition also.
15845 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015846 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015847 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015849 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015850 goto theend;
15851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015853 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015854 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015855 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015856 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015858 if (flags & SP_SUBPAT)
15859 retval = subpatnum;
15860 else
15861 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015862 if (flags & SP_SETPCMARK)
15863 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015865 if (match_pos != NULL)
15866 {
15867 /* Store the match cursor position */
15868 match_pos->lnum = pos.lnum;
15869 match_pos->col = pos.col + 1;
15870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 /* "/$" will put the cursor after the end of the line, may need to
15872 * correct that here */
15873 check_cursor();
15874 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015875
15876 /* If 'n' flag is used: restore cursor position. */
15877 if (flags & SP_NOMOVE)
15878 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015879 else
15880 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015881theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015883
15884 return retval;
15885}
15886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015887#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015888
15889/*
15890 * round() is not in C90, use ceil() or floor() instead.
15891 */
15892 float_T
15893vim_round(f)
15894 float_T f;
15895{
15896 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15897}
15898
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015899/*
15900 * "round({float})" function
15901 */
15902 static void
15903f_round(argvars, rettv)
15904 typval_T *argvars;
15905 typval_T *rettv;
15906{
15907 float_T f;
15908
15909 rettv->v_type = VAR_FLOAT;
15910 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015911 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015912 else
15913 rettv->vval.v_float = 0.0;
15914}
15915#endif
15916
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015917/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015918 * "screenattr()" function
15919 */
15920 static void
15921f_screenattr(argvars, rettv)
15922 typval_T *argvars UNUSED;
15923 typval_T *rettv;
15924{
15925 int row;
15926 int col;
15927 int c;
15928
15929 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15930 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15931 if (row < 0 || row >= screen_Rows
15932 || col < 0 || col >= screen_Columns)
15933 c = -1;
15934 else
15935 c = ScreenAttrs[LineOffset[row] + col];
15936 rettv->vval.v_number = c;
15937}
15938
15939/*
15940 * "screenchar()" function
15941 */
15942 static void
15943f_screenchar(argvars, rettv)
15944 typval_T *argvars UNUSED;
15945 typval_T *rettv;
15946{
15947 int row;
15948 int col;
15949 int off;
15950 int c;
15951
15952 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15953 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15954 if (row < 0 || row >= screen_Rows
15955 || col < 0 || col >= screen_Columns)
15956 c = -1;
15957 else
15958 {
15959 off = LineOffset[row] + col;
15960#ifdef FEAT_MBYTE
15961 if (enc_utf8 && ScreenLinesUC[off] != 0)
15962 c = ScreenLinesUC[off];
15963 else
15964#endif
15965 c = ScreenLines[off];
15966 }
15967 rettv->vval.v_number = c;
15968}
15969
15970/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015971 * "screencol()" function
15972 *
15973 * First column is 1 to be consistent with virtcol().
15974 */
15975 static void
15976f_screencol(argvars, rettv)
15977 typval_T *argvars UNUSED;
15978 typval_T *rettv;
15979{
15980 rettv->vval.v_number = screen_screencol() + 1;
15981}
15982
15983/*
15984 * "screenrow()" function
15985 */
15986 static void
15987f_screenrow(argvars, rettv)
15988 typval_T *argvars UNUSED;
15989 typval_T *rettv;
15990{
15991 rettv->vval.v_number = screen_screenrow() + 1;
15992}
15993
15994/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015995 * "search()" function
15996 */
15997 static void
15998f_search(argvars, rettv)
15999 typval_T *argvars;
16000 typval_T *rettv;
16001{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016002 int flags = 0;
16003
16004 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005}
16006
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016008 * "searchdecl()" function
16009 */
16010 static void
16011f_searchdecl(argvars, rettv)
16012 typval_T *argvars;
16013 typval_T *rettv;
16014{
16015 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016016 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016017 int error = FALSE;
16018 char_u *name;
16019
16020 rettv->vval.v_number = 1; /* default: FAIL */
16021
16022 name = get_tv_string_chk(&argvars[0]);
16023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016024 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016025 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016026 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16027 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16028 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016029 if (!error && name != NULL)
16030 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016031 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016032}
16033
16034/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016035 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016037 static int
16038searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016040 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041{
16042 char_u *spat, *mpat, *epat;
16043 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 int dir;
16046 int flags = 0;
16047 char_u nbuf1[NUMBUFLEN];
16048 char_u nbuf2[NUMBUFLEN];
16049 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016052 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016055 spat = get_tv_string_chk(&argvars[0]);
16056 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16057 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16058 if (spat == NULL || mpat == NULL || epat == NULL)
16059 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 /* Handle the optional fourth argument: flags */
16062 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016063 if (dir == 0)
16064 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016065
16066 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016067 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16068 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016069 if ((flags & (SP_END | SP_SUBPAT)) != 0
16070 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016071 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016072 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016073 goto theend;
16074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016076 /* Using 'r' implies 'W', otherwise it doesn't work. */
16077 if (flags & SP_REPEAT)
16078 p_ws = FALSE;
16079
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016080 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016081 if (argvars[3].v_type == VAR_UNKNOWN
16082 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 skip = (char_u *)"";
16084 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 if (argvars[5].v_type != VAR_UNKNOWN)
16088 {
16089 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16090 if (lnum_stop < 0)
16091 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016092#ifdef FEAT_RELTIME
16093 if (argvars[6].v_type != VAR_UNKNOWN)
16094 {
16095 time_limit = get_tv_number_chk(&argvars[6], NULL);
16096 if (time_limit < 0)
16097 goto theend;
16098 }
16099#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016100 }
16101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016102 if (skip == NULL)
16103 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016105 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016106 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016107
16108theend:
16109 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016110
16111 return retval;
16112}
16113
16114/*
16115 * "searchpair()" function
16116 */
16117 static void
16118f_searchpair(argvars, rettv)
16119 typval_T *argvars;
16120 typval_T *rettv;
16121{
16122 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16123}
16124
16125/*
16126 * "searchpairpos()" function
16127 */
16128 static void
16129f_searchpairpos(argvars, rettv)
16130 typval_T *argvars;
16131 typval_T *rettv;
16132{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133 pos_T match_pos;
16134 int lnum = 0;
16135 int col = 0;
16136
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016137 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016138 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139
16140 if (searchpair_cmn(argvars, &match_pos) > 0)
16141 {
16142 lnum = match_pos.lnum;
16143 col = match_pos.col;
16144 }
16145
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016146 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16147 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016148}
16149
16150/*
16151 * Search for a start/middle/end thing.
16152 * Used by searchpair(), see its documentation for the details.
16153 * Returns 0 or -1 for no match,
16154 */
16155 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016156do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16157 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016158 char_u *spat; /* start pattern */
16159 char_u *mpat; /* middle pattern */
16160 char_u *epat; /* end pattern */
16161 int dir; /* BACKWARD or FORWARD */
16162 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016163 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016164 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016165 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016166 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016167{
16168 char_u *save_cpo;
16169 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16170 long retval = 0;
16171 pos_T pos;
16172 pos_T firstpos;
16173 pos_T foundpos;
16174 pos_T save_cursor;
16175 pos_T save_pos;
16176 int n;
16177 int r;
16178 int nest = 1;
16179 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016180 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016181 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016182
16183 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16184 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016185 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016186
Bram Moolenaar76929292008-01-06 19:07:36 +000016187#ifdef FEAT_RELTIME
16188 /* Set the time limit, if there is one. */
16189 profile_setlimit(time_limit, &tm);
16190#endif
16191
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016192 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16193 * start/middle/end (pat3, for the top pair). */
16194 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16195 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16196 if (pat2 == NULL || pat3 == NULL)
16197 goto theend;
16198 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16199 if (*mpat == NUL)
16200 STRCPY(pat3, pat2);
16201 else
16202 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16203 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016204 if (flags & SP_START)
16205 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016206
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 save_cursor = curwin->w_cursor;
16208 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016209 clearpos(&firstpos);
16210 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 pat = pat3;
16212 for (;;)
16213 {
16214 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016215 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16217 /* didn't find it or found the first match again: FAIL */
16218 break;
16219
16220 if (firstpos.lnum == 0)
16221 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016222 if (equalpos(pos, foundpos))
16223 {
16224 /* Found the same position again. Can happen with a pattern that
16225 * has "\zs" at the end and searching backwards. Advance one
16226 * character and try again. */
16227 if (dir == BACKWARD)
16228 decl(&pos);
16229 else
16230 incl(&pos);
16231 }
16232 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016234 /* clear the start flag to avoid getting stuck here */
16235 options &= ~SEARCH_START;
16236
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 /* If the skip pattern matches, ignore this match. */
16238 if (*skip != NUL)
16239 {
16240 save_pos = curwin->w_cursor;
16241 curwin->w_cursor = pos;
16242 r = eval_to_bool(skip, &err, NULL, FALSE);
16243 curwin->w_cursor = save_pos;
16244 if (err)
16245 {
16246 /* Evaluating {skip} caused an error, break here. */
16247 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016248 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 break;
16250 }
16251 if (r)
16252 continue;
16253 }
16254
16255 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16256 {
16257 /* Found end when searching backwards or start when searching
16258 * forward: nested pair. */
16259 ++nest;
16260 pat = pat2; /* nested, don't search for middle */
16261 }
16262 else
16263 {
16264 /* Found end when searching forward or start when searching
16265 * backward: end of (nested) pair; or found middle in outer pair. */
16266 if (--nest == 1)
16267 pat = pat3; /* outer level, search for middle */
16268 }
16269
16270 if (nest == 0)
16271 {
16272 /* Found the match: return matchcount or line number. */
16273 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016274 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016276 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016277 if (flags & SP_SETPCMARK)
16278 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 curwin->w_cursor = pos;
16280 if (!(flags & SP_REPEAT))
16281 break;
16282 nest = 1; /* search for next unmatched */
16283 }
16284 }
16285
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016286 if (match_pos != NULL)
16287 {
16288 /* Store the match cursor position */
16289 match_pos->lnum = curwin->w_cursor.lnum;
16290 match_pos->col = curwin->w_cursor.col + 1;
16291 }
16292
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016294 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 curwin->w_cursor = save_cursor;
16296
16297theend:
16298 vim_free(pat2);
16299 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016300 if (p_cpo == empty_option)
16301 p_cpo = save_cpo;
16302 else
16303 /* Darn, evaluating the {skip} expression changed the value. */
16304 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016305
16306 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307}
16308
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016309/*
16310 * "searchpos()" function
16311 */
16312 static void
16313f_searchpos(argvars, rettv)
16314 typval_T *argvars;
16315 typval_T *rettv;
16316{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016317 pos_T match_pos;
16318 int lnum = 0;
16319 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016320 int n;
16321 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016322
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016323 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016324 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016326 n = search_cmn(argvars, &match_pos, &flags);
16327 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016328 {
16329 lnum = match_pos.lnum;
16330 col = match_pos.col;
16331 }
16332
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016333 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16334 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016335 if (flags & SP_SUBPAT)
16336 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016337}
16338
16339
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 static void
16341f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016342 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345#ifdef FEAT_CLIENTSERVER
16346 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 char_u *server = get_tv_string_chk(&argvars[0]);
16348 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 if (server == NULL || reply == NULL)
16352 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 if (check_restricted() || check_secure())
16354 return;
16355# ifdef FEAT_X11
16356 if (check_connection() == FAIL)
16357 return;
16358# endif
16359
16360 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362 EMSG(_("E258: Unable to send to client"));
16363 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 rettv->vval.v_number = 0;
16366#else
16367 rettv->vval.v_number = -1;
16368#endif
16369}
16370
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371 static void
16372f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375{
16376 char_u *r = NULL;
16377
16378#ifdef FEAT_CLIENTSERVER
16379# ifdef WIN32
16380 r = serverGetVimNames();
16381# else
16382 make_connection();
16383 if (X_DISPLAY != NULL)
16384 r = serverGetVimNames(X_DISPLAY);
16385# endif
16386#endif
16387 rettv->v_type = VAR_STRING;
16388 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389}
16390
16391/*
16392 * "setbufvar()" function
16393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016395f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016397 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398{
16399 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 char_u nbuf[NUMBUFLEN];
16404
16405 if (check_restricted() || check_secure())
16406 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16408 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016409 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 varp = &argvars[2];
16411
16412 if (buf != NULL && varname != NULL && varp != NULL)
16413 {
16414 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416
16417 if (*varname == '&')
16418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 long numval;
16420 char_u *strval;
16421 int error = FALSE;
16422
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 numval = get_tv_number_chk(varp, &error);
16425 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016426 if (!error && strval != NULL)
16427 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 }
16429 else
16430 {
16431 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16432 if (bufvarname != NULL)
16433 {
16434 STRCPY(bufvarname, "b:");
16435 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016436 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 vim_free(bufvarname);
16438 }
16439 }
16440
16441 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444}
16445
16446/*
16447 * "setcmdpos()" function
16448 */
16449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
16452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 int pos = (int)get_tv_number(&argvars[0]) - 1;
16455
16456 if (pos >= 0)
16457 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458}
16459
16460/*
16461 * "setline()" function
16462 */
16463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016465 typval_T *argvars;
16466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467{
16468 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016469 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016470 list_T *l = NULL;
16471 listitem_T *li = NULL;
16472 long added = 0;
16473 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016475 lnum = get_tv_lnum(&argvars[0]);
16476 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016478 l = argvars[1].vval.v_list;
16479 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016481 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016482 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016483
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016484 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016485 for (;;)
16486 {
16487 if (l != NULL)
16488 {
16489 /* list argument, get next string */
16490 if (li == NULL)
16491 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016493 li = li->li_next;
16494 }
16495
16496 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016498 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016499
16500 /* When coming here from Insert mode, sync undo, so that this can be
16501 * undone separately from what was previously inserted. */
16502 if (u_sync_once == 2)
16503 {
16504 u_sync_once = 1; /* notify that u_sync() was called */
16505 u_sync(TRUE);
16506 }
16507
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016508 if (lnum <= curbuf->b_ml.ml_line_count)
16509 {
16510 /* existing line, replace it */
16511 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16512 {
16513 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016514 if (lnum == curwin->w_cursor.lnum)
16515 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016516 rettv->vval.v_number = 0; /* OK */
16517 }
16518 }
16519 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16520 {
16521 /* lnum is one past the last line, append the line */
16522 ++added;
16523 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16524 rettv->vval.v_number = 0; /* OK */
16525 }
16526
16527 if (l == NULL) /* only one string argument */
16528 break;
16529 ++lnum;
16530 }
16531
16532 if (added > 0)
16533 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534}
16535
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016536static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16537
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016539 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016540 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016541 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016542set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016543 win_T *wp UNUSED;
16544 typval_T *list_arg UNUSED;
16545 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016546 typval_T *rettv;
16547{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016548#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016549 char_u *act;
16550 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016551#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016552
Bram Moolenaar2641f772005-03-25 21:58:17 +000016553 rettv->vval.v_number = -1;
16554
16555#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016556 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016557 EMSG(_(e_listreq));
16558 else
16559 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016560 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016561
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016562 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016563 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016564 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 if (act == NULL)
16566 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016567 if (*act == 'a' || *act == 'r')
16568 action = *act;
16569 }
16570
Bram Moolenaar81484f42012-12-05 15:16:47 +010016571 if (l != NULL && set_errorlist(wp, l, action,
16572 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016573 rettv->vval.v_number = 0;
16574 }
16575#endif
16576}
16577
16578/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016579 * "setloclist()" function
16580 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016581 static void
16582f_setloclist(argvars, rettv)
16583 typval_T *argvars;
16584 typval_T *rettv;
16585{
16586 win_T *win;
16587
16588 rettv->vval.v_number = -1;
16589
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016590 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016591 if (win != NULL)
16592 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16593}
16594
16595/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016596 * "setmatches()" function
16597 */
16598 static void
16599f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016600 typval_T *argvars UNUSED;
16601 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016602{
16603#ifdef FEAT_SEARCH_EXTRA
16604 list_T *l;
16605 listitem_T *li;
16606 dict_T *d;
16607
16608 rettv->vval.v_number = -1;
16609 if (argvars[0].v_type != VAR_LIST)
16610 {
16611 EMSG(_(e_listreq));
16612 return;
16613 }
16614 if ((l = argvars[0].vval.v_list) != NULL)
16615 {
16616
16617 /* To some extent make sure that we are dealing with a list from
16618 * "getmatches()". */
16619 li = l->lv_first;
16620 while (li != NULL)
16621 {
16622 if (li->li_tv.v_type != VAR_DICT
16623 || (d = li->li_tv.vval.v_dict) == NULL)
16624 {
16625 EMSG(_(e_invarg));
16626 return;
16627 }
16628 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16629 && dict_find(d, (char_u *)"pattern", -1) != NULL
16630 && dict_find(d, (char_u *)"priority", -1) != NULL
16631 && dict_find(d, (char_u *)"id", -1) != NULL))
16632 {
16633 EMSG(_(e_invarg));
16634 return;
16635 }
16636 li = li->li_next;
16637 }
16638
16639 clear_matches(curwin);
16640 li = l->lv_first;
16641 while (li != NULL)
16642 {
16643 d = li->li_tv.vval.v_dict;
16644 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16645 get_dict_string(d, (char_u *)"pattern", FALSE),
16646 (int)get_dict_number(d, (char_u *)"priority"),
16647 (int)get_dict_number(d, (char_u *)"id"));
16648 li = li->li_next;
16649 }
16650 rettv->vval.v_number = 0;
16651 }
16652#endif
16653}
16654
16655/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016656 * "setpos()" function
16657 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016658 static void
16659f_setpos(argvars, rettv)
16660 typval_T *argvars;
16661 typval_T *rettv;
16662{
16663 pos_T pos;
16664 int fnum;
16665 char_u *name;
16666
Bram Moolenaar08250432008-02-13 11:42:46 +000016667 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016668 name = get_tv_string_chk(argvars);
16669 if (name != NULL)
16670 {
16671 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16672 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016673 if (--pos.col < 0)
16674 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016675 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016676 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016677 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016678 if (fnum == curbuf->b_fnum)
16679 {
16680 curwin->w_cursor = pos;
16681 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016682 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016683 }
16684 else
16685 EMSG(_(e_invarg));
16686 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016687 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16688 {
16689 /* set mark */
16690 if (setmark_pos(name[1], &pos, fnum) == OK)
16691 rettv->vval.v_number = 0;
16692 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016693 else
16694 EMSG(_(e_invarg));
16695 }
16696 }
16697}
16698
16699/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016700 * "setqflist()" function
16701 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016702 static void
16703f_setqflist(argvars, rettv)
16704 typval_T *argvars;
16705 typval_T *rettv;
16706{
16707 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16708}
16709
16710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 * "setreg()" function
16712 */
16713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016715 typval_T *argvars;
16716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717{
16718 int regname;
16719 char_u *strregname;
16720 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 int append;
16723 char_u yank_type;
16724 long block_len;
16725
16726 block_len = -1;
16727 yank_type = MAUTO;
16728 append = FALSE;
16729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016731 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016733 if (strregname == NULL)
16734 return; /* type error; errmsg already given */
16735 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 if (regname == 0 || regname == '@')
16737 regname = '"';
16738 else if (regname == '=')
16739 return;
16740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016743 stropt = get_tv_string_chk(&argvars[2]);
16744 if (stropt == NULL)
16745 return; /* type error */
16746 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747 switch (*stropt)
16748 {
16749 case 'a': case 'A': /* append */
16750 append = TRUE;
16751 break;
16752 case 'v': case 'c': /* character-wise selection */
16753 yank_type = MCHAR;
16754 break;
16755 case 'V': case 'l': /* line-wise selection */
16756 yank_type = MLINE;
16757 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 case 'b': case Ctrl_V: /* block-wise selection */
16759 yank_type = MBLOCK;
16760 if (VIM_ISDIGIT(stropt[1]))
16761 {
16762 ++stropt;
16763 block_len = getdigits(&stropt) - 1;
16764 --stropt;
16765 }
16766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 }
16768 }
16769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016770 strval = get_tv_string_chk(&argvars[1]);
16771 if (strval != NULL)
16772 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016774 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775}
16776
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016777/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016778 * "settabvar()" function
16779 */
16780 static void
16781f_settabvar(argvars, rettv)
16782 typval_T *argvars;
16783 typval_T *rettv;
16784{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016785#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016786 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016787 tabpage_T *tp;
16788#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016789 char_u *varname, *tabvarname;
16790 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016791
16792 rettv->vval.v_number = 0;
16793
16794 if (check_restricted() || check_secure())
16795 return;
16796
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016797#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016798 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016799#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016800 varname = get_tv_string_chk(&argvars[1]);
16801 varp = &argvars[2];
16802
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016803 if (varname != NULL && varp != NULL
16804#ifdef FEAT_WINDOWS
16805 && tp != NULL
16806#endif
16807 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016808 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016809#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016810 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016811 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016812#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016813
16814 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16815 if (tabvarname != NULL)
16816 {
16817 STRCPY(tabvarname, "t:");
16818 STRCPY(tabvarname + 2, varname);
16819 set_var(tabvarname, varp, TRUE);
16820 vim_free(tabvarname);
16821 }
16822
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016823#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016824 /* Restore current tabpage */
16825 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016826 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016827#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016828 }
16829}
16830
16831/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016832 * "settabwinvar()" function
16833 */
16834 static void
16835f_settabwinvar(argvars, rettv)
16836 typval_T *argvars;
16837 typval_T *rettv;
16838{
16839 setwinvar(argvars, rettv, 1);
16840}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841
16842/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016843 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016846f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 typval_T *argvars;
16848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016850 setwinvar(argvars, rettv, 0);
16851}
16852
16853/*
16854 * "setwinvar()" and "settabwinvar()" functions
16855 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016856
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016857 static void
16858setwinvar(argvars, rettv, off)
16859 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016860 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016861 int off;
16862{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 win_T *win;
16864#ifdef FEAT_WINDOWS
16865 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016866 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867#endif
16868 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016871 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872
16873 if (check_restricted() || check_secure())
16874 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016875
16876#ifdef FEAT_WINDOWS
16877 if (off == 1)
16878 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16879 else
16880 tp = curtab;
16881#endif
16882 win = find_win_by_nr(&argvars[off], tp);
16883 varname = get_tv_string_chk(&argvars[off + 1]);
16884 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885
16886 if (win != NULL && varname != NULL && varp != NULL)
16887 {
16888#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016889 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016890 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891#endif
16892
16893 if (*varname == '&')
16894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 long numval;
16896 char_u *strval;
16897 int error = FALSE;
16898
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016900 numval = get_tv_number_chk(varp, &error);
16901 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016902 if (!error && strval != NULL)
16903 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 }
16905 else
16906 {
16907 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16908 if (winvarname != NULL)
16909 {
16910 STRCPY(winvarname, "w:");
16911 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016912 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 vim_free(winvarname);
16914 }
16915 }
16916
16917#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016918 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919#endif
16920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921}
16922
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016923#ifdef FEAT_CRYPT
16924/*
16925 * "sha256({string})" function
16926 */
16927 static void
16928f_sha256(argvars, rettv)
16929 typval_T *argvars;
16930 typval_T *rettv;
16931{
16932 char_u *p;
16933
16934 p = get_tv_string(&argvars[0]);
16935 rettv->vval.v_string = vim_strsave(
16936 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16937 rettv->v_type = VAR_STRING;
16938}
16939#endif /* FEAT_CRYPT */
16940
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016942 * "shellescape({string})" function
16943 */
16944 static void
16945f_shellescape(argvars, rettv)
16946 typval_T *argvars;
16947 typval_T *rettv;
16948{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016949 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010016950 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016951 rettv->v_type = VAR_STRING;
16952}
16953
16954/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016955 * shiftwidth() function
16956 */
16957 static void
16958f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016959 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016960 typval_T *rettv;
16961{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016962 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016963}
16964
16965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 */
16968 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016970 typval_T *argvars;
16971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 p = get_tv_string(&argvars[0]);
16976 rettv->vval.v_string = vim_strsave(p);
16977 simplify_filename(rettv->vval.v_string); /* simplify in place */
16978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979}
16980
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016981#ifdef FEAT_FLOAT
16982/*
16983 * "sin()" function
16984 */
16985 static void
16986f_sin(argvars, rettv)
16987 typval_T *argvars;
16988 typval_T *rettv;
16989{
16990 float_T f;
16991
16992 rettv->v_type = VAR_FLOAT;
16993 if (get_float_arg(argvars, &f) == OK)
16994 rettv->vval.v_float = sin(f);
16995 else
16996 rettv->vval.v_float = 0.0;
16997}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016998
16999/*
17000 * "sinh()" function
17001 */
17002 static void
17003f_sinh(argvars, rettv)
17004 typval_T *argvars;
17005 typval_T *rettv;
17006{
17007 float_T f;
17008
17009 rettv->v_type = VAR_FLOAT;
17010 if (get_float_arg(argvars, &f) == OK)
17011 rettv->vval.v_float = sinh(f);
17012 else
17013 rettv->vval.v_float = 0.0;
17014}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017015#endif
17016
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017static int
17018#ifdef __BORLANDC__
17019 _RTLENTRYF
17020#endif
17021 item_compare __ARGS((const void *s1, const void *s2));
17022static int
17023#ifdef __BORLANDC__
17024 _RTLENTRYF
17025#endif
17026 item_compare2 __ARGS((const void *s1, const void *s2));
17027
17028static int item_compare_ic;
17029static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017030static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017031static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017032static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017033#define ITEM_COMPARE_FAIL 999
17034
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017036 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 static int
17039#ifdef __BORLANDC__
17040_RTLENTRYF
17041#endif
17042item_compare(s1, s2)
17043 const void *s1;
17044 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 char_u *p1, *p2;
17047 char_u *tofree1, *tofree2;
17048 int res;
17049 char_u numbuf1[NUMBUFLEN];
17050 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017052 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17053 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017054 if (p1 == NULL)
17055 p1 = (char_u *)"";
17056 if (p2 == NULL)
17057 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 if (item_compare_ic)
17059 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 res = STRCMP(p1, p2);
17062 vim_free(tofree1);
17063 vim_free(tofree2);
17064 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065}
17066
17067 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068#ifdef __BORLANDC__
17069_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071item_compare2(s1, s2)
17072 const void *s1;
17073 const void *s2;
17074{
17075 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017076 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017077 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 /* shortcut after failure in previous call; compare all items equal */
17081 if (item_compare_func_err)
17082 return 0;
17083
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017084 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17085 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017086 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17087 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088
17089 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017090 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017091 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17092 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 clear_tv(&argv[0]);
17094 clear_tv(&argv[1]);
17095
17096 if (res == FAIL)
17097 res = ITEM_COMPARE_FAIL;
17098 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017099 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17100 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017101 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102 clear_tv(&rettv);
17103 return res;
17104}
17105
17106/*
17107 * "sort({list})" function
17108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017110do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 typval_T *argvars;
17112 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017113 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114{
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 list_T *l;
17116 listitem_T *li;
17117 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 long len;
17119 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017122 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 else
17124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017126 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017127 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 return;
17129 rettv->vval.v_list = l;
17130 rettv->v_type = VAR_LIST;
17131 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 len = list_len(l);
17134 if (len <= 1)
17135 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 item_compare_ic = FALSE;
17138 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017139 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140 if (argvars[1].v_type != VAR_UNKNOWN)
17141 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017142 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 else
17146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 int error = FALSE;
17148
17149 i = get_tv_number_chk(&argvars[1], &error);
17150 if (error)
17151 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 if (i == 1)
17153 item_compare_ic = TRUE;
17154 else
17155 item_compare_func = get_tv_string(&argvars[1]);
17156 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017157
17158 if (argvars[2].v_type != VAR_UNKNOWN)
17159 {
17160 /* optional third argument: {dict} */
17161 if (argvars[2].v_type != VAR_DICT)
17162 {
17163 EMSG(_(e_dictreq));
17164 return;
17165 }
17166 item_compare_selfdict = argvars[2].vval.v_dict;
17167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 if (ptrs == NULL)
17173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174
Bram Moolenaar327aa022014-03-25 18:24:23 +010017175 i = 0;
17176 if (sort)
17177 {
17178 /* sort(): ptrs will be the list to sort */
17179 for (li = l->lv_first; li != NULL; li = li->li_next)
17180 ptrs[i++] = li;
17181
17182 item_compare_func_err = FALSE;
17183 /* test the compare function */
17184 if (item_compare_func != NULL
17185 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017187 EMSG(_("E702: Sort compare function failed"));
17188 else
17189 {
17190 /* Sort the array with item pointers. */
17191 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17192 item_compare_func == NULL ? item_compare : item_compare2);
17193
17194 if (!item_compare_func_err)
17195 {
17196 /* Clear the List and append the items in sorted order. */
17197 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17198 l->lv_len = 0;
17199 for (i = 0; i < len; ++i)
17200 list_append(l, ptrs[i]);
17201 }
17202 }
17203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017206 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17207
17208 /* f_uniq(): ptrs will be a stack of items to remove */
17209 item_compare_func_err = FALSE;
17210 item_compare_func_ptr = item_compare_func
17211 ? item_compare2 : item_compare;
17212
17213 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17214 li = li->li_next)
17215 {
17216 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17217 == 0)
17218 ptrs[i++] = li;
17219 if (item_compare_func_err)
17220 {
17221 EMSG(_("E882: Uniq compare function failed"));
17222 break;
17223 }
17224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017226 if (!item_compare_func_err)
17227 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017228 while (--i >= 0)
17229 {
17230 li = ptrs[i]->li_next;
17231 ptrs[i]->li_next = li->li_next;
17232 if (li->li_next != NULL)
17233 li->li_next->li_prev = ptrs[i];
17234 else
17235 l->lv_last = ptrs[i];
17236 list_fix_watch(l, li);
17237 listitem_free(li);
17238 l->lv_len--;
17239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 }
17242
17243 vim_free(ptrs);
17244 }
17245}
17246
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017247/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017248 * "sort({list})" function
17249 */
17250 static void
17251f_sort(argvars, rettv)
17252 typval_T *argvars;
17253 typval_T *rettv;
17254{
17255 do_sort_uniq(argvars, rettv, TRUE);
17256}
17257
17258/*
17259 * "uniq({list})" function
17260 */
17261 static void
17262f_uniq(argvars, rettv)
17263 typval_T *argvars;
17264 typval_T *rettv;
17265{
17266 do_sort_uniq(argvars, rettv, FALSE);
17267}
17268
17269/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017270 * "soundfold({word})" function
17271 */
17272 static void
17273f_soundfold(argvars, rettv)
17274 typval_T *argvars;
17275 typval_T *rettv;
17276{
17277 char_u *s;
17278
17279 rettv->v_type = VAR_STRING;
17280 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017281#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017282 rettv->vval.v_string = eval_soundfold(s);
17283#else
17284 rettv->vval.v_string = vim_strsave(s);
17285#endif
17286}
17287
17288/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017289 * "spellbadword()" function
17290 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017291 static void
17292f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017293 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017294 typval_T *rettv;
17295{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017296 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017297 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017298 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017301 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017302
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017303#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017304 if (argvars[0].v_type == VAR_UNKNOWN)
17305 {
17306 /* Find the start and length of the badly spelled word. */
17307 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17308 if (len != 0)
17309 word = ml_get_cursor();
17310 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017311 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017312 {
17313 char_u *str = get_tv_string_chk(&argvars[0]);
17314 int capcol = -1;
17315
17316 if (str != NULL)
17317 {
17318 /* Check the argument for spelling. */
17319 while (*str != NUL)
17320 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017321 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017322 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017323 {
17324 word = str;
17325 break;
17326 }
17327 str += len;
17328 }
17329 }
17330 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017331#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017332
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017333 list_append_string(rettv->vval.v_list, word, len);
17334 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017335 attr == HLF_SPB ? "bad" :
17336 attr == HLF_SPR ? "rare" :
17337 attr == HLF_SPL ? "local" :
17338 attr == HLF_SPC ? "caps" :
17339 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017340}
17341
17342/*
17343 * "spellsuggest()" function
17344 */
17345 static void
17346f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017347 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017348 typval_T *rettv;
17349{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017350#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017351 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017352 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017353 int maxcount;
17354 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017355 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017356 listitem_T *li;
17357 int need_capital = FALSE;
17358#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017359
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017360 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017361 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017362
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017363#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017364 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017365 {
17366 str = get_tv_string(&argvars[0]);
17367 if (argvars[1].v_type != VAR_UNKNOWN)
17368 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017369 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017370 if (maxcount <= 0)
17371 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017372 if (argvars[2].v_type != VAR_UNKNOWN)
17373 {
17374 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17375 if (typeerr)
17376 return;
17377 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017378 }
17379 else
17380 maxcount = 25;
17381
Bram Moolenaar4770d092006-01-12 23:22:24 +000017382 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017383
17384 for (i = 0; i < ga.ga_len; ++i)
17385 {
17386 str = ((char_u **)ga.ga_data)[i];
17387
17388 li = listitem_alloc();
17389 if (li == NULL)
17390 vim_free(str);
17391 else
17392 {
17393 li->li_tv.v_type = VAR_STRING;
17394 li->li_tv.v_lock = 0;
17395 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017397 }
17398 }
17399 ga_clear(&ga);
17400 }
17401#endif
17402}
17403
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017405f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017406 typval_T *argvars;
17407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408{
17409 char_u *str;
17410 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017411 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017412 regmatch_T regmatch;
17413 char_u patbuf[NUMBUFLEN];
17414 char_u *save_cpo;
17415 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017416 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017417 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017418 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419
17420 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17421 save_cpo = p_cpo;
17422 p_cpo = (char_u *)"";
17423
17424 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017425 if (argvars[1].v_type != VAR_UNKNOWN)
17426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017427 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17428 if (pat == NULL)
17429 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017431 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017432 }
17433 if (pat == NULL || *pat == NUL)
17434 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 if (typeerr)
17439 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17442 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017444 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017445 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017447 if (*str == NUL)
17448 match = FALSE; /* empty item at the end */
17449 else
17450 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 if (match)
17452 end = regmatch.startp[0];
17453 else
17454 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017455 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17456 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017458 if (list_append_string(rettv->vval.v_list, str,
17459 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017460 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017461 }
17462 if (!match)
17463 break;
17464 /* Advance to just after the match. */
17465 if (regmatch.endp[0] > str)
17466 col = 0;
17467 else
17468 {
17469 /* Don't get stuck at the same match. */
17470#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017471 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472#else
17473 col = 1;
17474#endif
17475 }
17476 str = regmatch.endp[0];
17477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478
Bram Moolenaar473de612013-06-08 18:19:48 +020017479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481
Bram Moolenaar0d660222005-01-07 21:51:51 +000017482 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017485#ifdef FEAT_FLOAT
17486/*
17487 * "sqrt()" function
17488 */
17489 static void
17490f_sqrt(argvars, rettv)
17491 typval_T *argvars;
17492 typval_T *rettv;
17493{
17494 float_T f;
17495
17496 rettv->v_type = VAR_FLOAT;
17497 if (get_float_arg(argvars, &f) == OK)
17498 rettv->vval.v_float = sqrt(f);
17499 else
17500 rettv->vval.v_float = 0.0;
17501}
17502
17503/*
17504 * "str2float()" function
17505 */
17506 static void
17507f_str2float(argvars, rettv)
17508 typval_T *argvars;
17509 typval_T *rettv;
17510{
17511 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17512
17513 if (*p == '+')
17514 p = skipwhite(p + 1);
17515 (void)string2float(p, &rettv->vval.v_float);
17516 rettv->v_type = VAR_FLOAT;
17517}
17518#endif
17519
Bram Moolenaar2c932302006-03-18 21:42:09 +000017520/*
17521 * "str2nr()" function
17522 */
17523 static void
17524f_str2nr(argvars, rettv)
17525 typval_T *argvars;
17526 typval_T *rettv;
17527{
17528 int base = 10;
17529 char_u *p;
17530 long n;
17531
17532 if (argvars[1].v_type != VAR_UNKNOWN)
17533 {
17534 base = get_tv_number(&argvars[1]);
17535 if (base != 8 && base != 10 && base != 16)
17536 {
17537 EMSG(_(e_invarg));
17538 return;
17539 }
17540 }
17541
17542 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017543 if (*p == '+')
17544 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017545 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17546 rettv->vval.v_number = n;
17547}
17548
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549#ifdef HAVE_STRFTIME
17550/*
17551 * "strftime({format}[, {time}])" function
17552 */
17553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017554f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 typval_T *argvars;
17556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557{
17558 char_u result_buf[256];
17559 struct tm *curtime;
17560 time_t seconds;
17561 char_u *p;
17562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017566 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 seconds = time(NULL);
17568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017569 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 curtime = localtime(&seconds);
17571 /* MSVC returns NULL for an invalid value of seconds. */
17572 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017573 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 else
17575 {
17576# ifdef FEAT_MBYTE
17577 vimconv_T conv;
17578 char_u *enc;
17579
17580 conv.vc_type = CONV_NONE;
17581 enc = enc_locale();
17582 convert_setup(&conv, p_enc, enc);
17583 if (conv.vc_type != CONV_NONE)
17584 p = string_convert(&conv, p, NULL);
17585# endif
17586 if (p != NULL)
17587 (void)strftime((char *)result_buf, sizeof(result_buf),
17588 (char *)p, curtime);
17589 else
17590 result_buf[0] = NUL;
17591
17592# ifdef FEAT_MBYTE
17593 if (conv.vc_type != CONV_NONE)
17594 vim_free(p);
17595 convert_setup(&conv, enc, p_enc);
17596 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017597 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 else
17599# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017600 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
17602# ifdef FEAT_MBYTE
17603 /* Release conversion descriptors */
17604 convert_setup(&conv, NULL, NULL);
17605 vim_free(enc);
17606# endif
17607 }
17608}
17609#endif
17610
17611/*
17612 * "stridx()" function
17613 */
17614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 typval_T *argvars;
17617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618{
17619 char_u buf[NUMBUFLEN];
17620 char_u *needle;
17621 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626 needle = get_tv_string_chk(&argvars[1]);
17627 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017628 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 if (needle == NULL || haystack == NULL)
17630 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
Bram Moolenaar33570922005-01-25 22:26:29 +000017632 if (argvars[2].v_type != VAR_UNKNOWN)
17633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017634 int error = FALSE;
17635
17636 start_idx = get_tv_number_chk(&argvars[2], &error);
17637 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017638 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017639 if (start_idx >= 0)
17640 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 }
17642
17643 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17644 if (pos != NULL)
17645 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
17648/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017649 * "string()" function
17650 */
17651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 typval_T *argvars;
17654 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017655{
17656 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017657 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017660 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017661 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017662 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664}
17665
17666/*
17667 * "strlen()" function
17668 */
17669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017671 typval_T *argvars;
17672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 rettv->vval.v_number = (varnumber_T)(STRLEN(
17675 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676}
17677
17678/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017679 * "strchars()" function
17680 */
17681 static void
17682f_strchars(argvars, rettv)
17683 typval_T *argvars;
17684 typval_T *rettv;
17685{
17686 char_u *s = get_tv_string(&argvars[0]);
17687#ifdef FEAT_MBYTE
17688 varnumber_T len = 0;
17689
17690 while (*s != NUL)
17691 {
17692 mb_cptr2char_adv(&s);
17693 ++len;
17694 }
17695 rettv->vval.v_number = len;
17696#else
17697 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17698#endif
17699}
17700
17701/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017702 * "strdisplaywidth()" function
17703 */
17704 static void
17705f_strdisplaywidth(argvars, rettv)
17706 typval_T *argvars;
17707 typval_T *rettv;
17708{
17709 char_u *s = get_tv_string(&argvars[0]);
17710 int col = 0;
17711
17712 if (argvars[1].v_type != VAR_UNKNOWN)
17713 col = get_tv_number(&argvars[1]);
17714
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017715 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017716}
17717
17718/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017719 * "strwidth()" function
17720 */
17721 static void
17722f_strwidth(argvars, rettv)
17723 typval_T *argvars;
17724 typval_T *rettv;
17725{
17726 char_u *s = get_tv_string(&argvars[0]);
17727
17728 rettv->vval.v_number = (varnumber_T)(
17729#ifdef FEAT_MBYTE
17730 mb_string2cells(s, -1)
17731#else
17732 STRLEN(s)
17733#endif
17734 );
17735}
17736
17737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 * "strpart()" function
17739 */
17740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017742 typval_T *argvars;
17743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744{
17745 char_u *p;
17746 int n;
17747 int len;
17748 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017749 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 slen = (int)STRLEN(p);
17753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017754 n = get_tv_number_chk(&argvars[1], &error);
17755 if (error)
17756 len = 0;
17757 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 else
17760 len = slen - n; /* default len: all bytes that are available. */
17761
17762 /*
17763 * Only return the overlap between the specified part and the actual
17764 * string.
17765 */
17766 if (n < 0)
17767 {
17768 len += n;
17769 n = 0;
17770 }
17771 else if (n > slen)
17772 n = slen;
17773 if (len < 0)
17774 len = 0;
17775 else if (n + len > slen)
17776 len = slen - n;
17777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778 rettv->v_type = VAR_STRING;
17779 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780}
17781
17782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783 * "strridx()" function
17784 */
17785 static void
17786f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017787 typval_T *argvars;
17788 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017789{
17790 char_u buf[NUMBUFLEN];
17791 char_u *needle;
17792 char_u *haystack;
17793 char_u *rest;
17794 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017795 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 needle = get_tv_string_chk(&argvars[1]);
17798 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017799
17800 rettv->vval.v_number = -1;
17801 if (needle == NULL || haystack == NULL)
17802 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017803
17804 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017805 if (argvars[2].v_type != VAR_UNKNOWN)
17806 {
17807 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017808 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017809 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017810 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017811 }
17812 else
17813 end_idx = haystack_len;
17814
Bram Moolenaar0d660222005-01-07 21:51:51 +000017815 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017816 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017818 lastmatch = haystack + end_idx;
17819 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017820 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017821 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017822 for (rest = haystack; *rest != '\0'; ++rest)
17823 {
17824 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017825 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017826 break;
17827 lastmatch = rest;
17828 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017829 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017830
17831 if (lastmatch == NULL)
17832 rettv->vval.v_number = -1;
17833 else
17834 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17835}
17836
17837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 * "strtrans()" function
17839 */
17840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017841f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 typval_T *argvars;
17843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845 rettv->v_type = VAR_STRING;
17846 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847}
17848
17849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850 * "submatch()" function
17851 */
17852 static void
17853f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *argvars;
17855 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017856{
17857 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017858 rettv->vval.v_string =
17859 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860}
17861
17862/*
17863 * "substitute()" function
17864 */
17865 static void
17866f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017867 typval_T *argvars;
17868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017869{
17870 char_u patbuf[NUMBUFLEN];
17871 char_u subbuf[NUMBUFLEN];
17872 char_u flagsbuf[NUMBUFLEN];
17873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017874 char_u *str = get_tv_string_chk(&argvars[0]);
17875 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17876 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17877 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17878
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17881 rettv->vval.v_string = NULL;
17882 else
17883 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017884}
17885
17886/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017887 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893{
17894 int id = 0;
17895#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017896 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 long col;
17898 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017899 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017901 lnum = get_tv_lnum(argvars); /* -1 on type error */
17902 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17903 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017905 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017906 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017907 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908#endif
17909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911}
17912
17913/*
17914 * "synIDattr(id, what [, mode])" function
17915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017917f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017918 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920{
17921 char_u *p = NULL;
17922#ifdef FEAT_SYN_HL
17923 int id;
17924 char_u *what;
17925 char_u *mode;
17926 char_u modebuf[NUMBUFLEN];
17927 int modec;
17928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017929 id = get_tv_number(&argvars[0]);
17930 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017931 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017935 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 modec = 0; /* replace invalid with current */
17937 }
17938 else
17939 {
17940#ifdef FEAT_GUI
17941 if (gui.in_use)
17942 modec = 'g';
17943 else
17944#endif
17945 if (t_colors > 1)
17946 modec = 'c';
17947 else
17948 modec = 't';
17949 }
17950
17951
17952 switch (TOLOWER_ASC(what[0]))
17953 {
17954 case 'b':
17955 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17956 p = highlight_color(id, what, modec);
17957 else /* bold */
17958 p = highlight_has_attr(id, HL_BOLD, modec);
17959 break;
17960
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017961 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 p = highlight_color(id, what, modec);
17963 break;
17964
17965 case 'i':
17966 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17967 p = highlight_has_attr(id, HL_INVERSE, modec);
17968 else /* italic */
17969 p = highlight_has_attr(id, HL_ITALIC, modec);
17970 break;
17971
17972 case 'n': /* name */
17973 p = get_highlight_name(NULL, id - 1);
17974 break;
17975
17976 case 'r': /* reverse */
17977 p = highlight_has_attr(id, HL_INVERSE, modec);
17978 break;
17979
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017980 case 's':
17981 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17982 p = highlight_color(id, what, modec);
17983 else /* standout */
17984 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 break;
17986
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017987 case 'u':
17988 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17989 /* underline */
17990 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17991 else
17992 /* undercurl */
17993 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 break;
17995 }
17996
17997 if (p != NULL)
17998 p = vim_strsave(p);
17999#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018000 rettv->v_type = VAR_STRING;
18001 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002}
18003
18004/*
18005 * "synIDtrans(id)" function
18006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011{
18012 int id;
18013
18014#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018015 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016
18017 if (id > 0)
18018 id = syn_get_final_id(id);
18019 else
18020#endif
18021 id = 0;
18022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018023 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024}
18025
18026/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018027 * "synconcealed(lnum, col)" function
18028 */
18029 static void
18030f_synconcealed(argvars, rettv)
18031 typval_T *argvars UNUSED;
18032 typval_T *rettv;
18033{
18034#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18035 long lnum;
18036 long col;
18037 int syntax_flags = 0;
18038 int cchar;
18039 int matchid = 0;
18040 char_u str[NUMBUFLEN];
18041#endif
18042
18043 rettv->v_type = VAR_LIST;
18044 rettv->vval.v_list = NULL;
18045
18046#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18047 lnum = get_tv_lnum(argvars); /* -1 on type error */
18048 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18049
18050 vim_memset(str, NUL, sizeof(str));
18051
18052 if (rettv_list_alloc(rettv) != FAIL)
18053 {
18054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18055 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18056 && curwin->w_p_cole > 0)
18057 {
18058 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18059 syntax_flags = get_syntax_info(&matchid);
18060
18061 /* get the conceal character */
18062 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18063 {
18064 cchar = syn_get_sub_char();
18065 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18066 cchar = lcs_conceal;
18067 if (cchar != NUL)
18068 {
18069# ifdef FEAT_MBYTE
18070 if (has_mbyte)
18071 (*mb_char2bytes)(cchar, str);
18072 else
18073# endif
18074 str[0] = cchar;
18075 }
18076 }
18077 }
18078
18079 list_append_number(rettv->vval.v_list,
18080 (syntax_flags & HL_CONCEAL) != 0);
18081 /* -1 to auto-determine strlen */
18082 list_append_string(rettv->vval.v_list, str, -1);
18083 list_append_number(rettv->vval.v_list, matchid);
18084 }
18085#endif
18086}
18087
18088/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018089 * "synstack(lnum, col)" function
18090 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018091 static void
18092f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018093 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018094 typval_T *rettv;
18095{
18096#ifdef FEAT_SYN_HL
18097 long lnum;
18098 long col;
18099 int i;
18100 int id;
18101#endif
18102
18103 rettv->v_type = VAR_LIST;
18104 rettv->vval.v_list = NULL;
18105
18106#ifdef FEAT_SYN_HL
18107 lnum = get_tv_lnum(argvars); /* -1 on type error */
18108 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18109
18110 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018111 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018112 && rettv_list_alloc(rettv) != FAIL)
18113 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018114 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018115 for (i = 0; ; ++i)
18116 {
18117 id = syn_get_stack_item(i);
18118 if (id < 0)
18119 break;
18120 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18121 break;
18122 }
18123 }
18124#endif
18125}
18126
18127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 * "system()" function
18129 */
18130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018131f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 typval_T *argvars;
18133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018135 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018137 char_u *infile = NULL;
18138 char_u buf[NUMBUFLEN];
18139 int err = FALSE;
18140 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018142 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018143 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018146 {
18147 /*
18148 * Write the string to a temp file, to be used for input of the shell
18149 * command.
18150 */
18151 if ((infile = vim_tempname('i')) == NULL)
18152 {
18153 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018154 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018155 }
18156
18157 fd = mch_fopen((char *)infile, WRITEBIN);
18158 if (fd == NULL)
18159 {
18160 EMSG2(_(e_notopen), infile);
18161 goto done;
18162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018163 p = get_tv_string_buf_chk(&argvars[1], buf);
18164 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018165 {
18166 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018168 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018169 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18170 err = TRUE;
18171 if (fclose(fd) != 0)
18172 err = TRUE;
18173 if (err)
18174 {
18175 EMSG(_("E677: Error writing temp file"));
18176 goto done;
18177 }
18178 }
18179
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018180 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18181 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018182
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183#ifdef USE_CR
18184 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018185 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 {
18187 char_u *s;
18188
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018189 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 {
18191 if (*s == CAR)
18192 *s = NL;
18193 }
18194 }
18195#else
18196# ifdef USE_CRNL
18197 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018198 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
18200 char_u *s, *d;
18201
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018202 d = res;
18203 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 {
18205 if (s[0] == CAR && s[1] == NL)
18206 ++s;
18207 *d++ = *s;
18208 }
18209 *d = NUL;
18210 }
18211# endif
18212#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018213
18214done:
18215 if (infile != NULL)
18216 {
18217 mch_remove(infile);
18218 vim_free(infile);
18219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 rettv->v_type = VAR_STRING;
18221 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222}
18223
18224/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018225 * "tabpagebuflist()" function
18226 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018227 static void
18228f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018229 typval_T *argvars UNUSED;
18230 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018231{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018232#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018233 tabpage_T *tp;
18234 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018235
18236 if (argvars[0].v_type == VAR_UNKNOWN)
18237 wp = firstwin;
18238 else
18239 {
18240 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18241 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018242 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018243 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018244 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018245 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018246 for (; wp != NULL; wp = wp->w_next)
18247 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018248 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018249 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018250 }
18251#endif
18252}
18253
18254
18255/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018256 * "tabpagenr()" function
18257 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018258 static void
18259f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018260 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018261 typval_T *rettv;
18262{
18263 int nr = 1;
18264#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018265 char_u *arg;
18266
18267 if (argvars[0].v_type != VAR_UNKNOWN)
18268 {
18269 arg = get_tv_string_chk(&argvars[0]);
18270 nr = 0;
18271 if (arg != NULL)
18272 {
18273 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018274 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018275 else
18276 EMSG2(_(e_invexpr2), arg);
18277 }
18278 }
18279 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018280 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018281#endif
18282 rettv->vval.v_number = nr;
18283}
18284
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018285
18286#ifdef FEAT_WINDOWS
18287static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18288
18289/*
18290 * Common code for tabpagewinnr() and winnr().
18291 */
18292 static int
18293get_winnr(tp, argvar)
18294 tabpage_T *tp;
18295 typval_T *argvar;
18296{
18297 win_T *twin;
18298 int nr = 1;
18299 win_T *wp;
18300 char_u *arg;
18301
18302 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18303 if (argvar->v_type != VAR_UNKNOWN)
18304 {
18305 arg = get_tv_string_chk(argvar);
18306 if (arg == NULL)
18307 nr = 0; /* type error; errmsg already given */
18308 else if (STRCMP(arg, "$") == 0)
18309 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18310 else if (STRCMP(arg, "#") == 0)
18311 {
18312 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18313 if (twin == NULL)
18314 nr = 0;
18315 }
18316 else
18317 {
18318 EMSG2(_(e_invexpr2), arg);
18319 nr = 0;
18320 }
18321 }
18322
18323 if (nr > 0)
18324 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18325 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018326 {
18327 if (wp == NULL)
18328 {
18329 /* didn't find it in this tabpage */
18330 nr = 0;
18331 break;
18332 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018333 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018334 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018335 return nr;
18336}
18337#endif
18338
18339/*
18340 * "tabpagewinnr()" function
18341 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018342 static void
18343f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018344 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018345 typval_T *rettv;
18346{
18347 int nr = 1;
18348#ifdef FEAT_WINDOWS
18349 tabpage_T *tp;
18350
18351 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18352 if (tp == NULL)
18353 nr = 0;
18354 else
18355 nr = get_winnr(tp, &argvars[1]);
18356#endif
18357 rettv->vval.v_number = nr;
18358}
18359
18360
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018361/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018362 * "tagfiles()" function
18363 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018364 static void
18365f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018366 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018367 typval_T *rettv;
18368{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018369 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018370 tagname_T tn;
18371 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018372
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018373 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018374 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018375 fname = alloc(MAXPATHL);
18376 if (fname == NULL)
18377 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018378
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018379 for (first = TRUE; ; first = FALSE)
18380 if (get_tagfname(&tn, first, fname) == FAIL
18381 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018382 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018383 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018384 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018385}
18386
18387/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018388 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018389 */
18390 static void
18391f_taglist(argvars, rettv)
18392 typval_T *argvars;
18393 typval_T *rettv;
18394{
18395 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018396
18397 tag_pattern = get_tv_string(&argvars[0]);
18398
18399 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018400 if (*tag_pattern == NUL)
18401 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018402
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018403 if (rettv_list_alloc(rettv) == OK)
18404 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018405}
18406
18407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 * "tempname()" function
18409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414{
18415 static int x = 'A';
18416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018417 rettv->v_type = VAR_STRING;
18418 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419
18420 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18421 * names. Skip 'I' and 'O', they are used for shell redirection. */
18422 do
18423 {
18424 if (x == 'Z')
18425 x = '0';
18426 else if (x == '9')
18427 x = 'A';
18428 else
18429 {
18430#ifdef EBCDIC
18431 if (x == 'I')
18432 x = 'J';
18433 else if (x == 'R')
18434 x = 'S';
18435 else
18436#endif
18437 ++x;
18438 }
18439 } while (x == 'I' || x == 'O');
18440}
18441
18442/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018443 * "test(list)" function: Just checking the walls...
18444 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018445 static void
18446f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018447 typval_T *argvars UNUSED;
18448 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018449{
18450 /* Used for unit testing. Change the code below to your liking. */
18451#if 0
18452 listitem_T *li;
18453 list_T *l;
18454 char_u *bad, *good;
18455
18456 if (argvars[0].v_type != VAR_LIST)
18457 return;
18458 l = argvars[0].vval.v_list;
18459 if (l == NULL)
18460 return;
18461 li = l->lv_first;
18462 if (li == NULL)
18463 return;
18464 bad = get_tv_string(&li->li_tv);
18465 li = li->li_next;
18466 if (li == NULL)
18467 return;
18468 good = get_tv_string(&li->li_tv);
18469 rettv->vval.v_number = test_edit_score(bad, good);
18470#endif
18471}
18472
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018473#ifdef FEAT_FLOAT
18474/*
18475 * "tan()" function
18476 */
18477 static void
18478f_tan(argvars, rettv)
18479 typval_T *argvars;
18480 typval_T *rettv;
18481{
18482 float_T f;
18483
18484 rettv->v_type = VAR_FLOAT;
18485 if (get_float_arg(argvars, &f) == OK)
18486 rettv->vval.v_float = tan(f);
18487 else
18488 rettv->vval.v_float = 0.0;
18489}
18490
18491/*
18492 * "tanh()" function
18493 */
18494 static void
18495f_tanh(argvars, rettv)
18496 typval_T *argvars;
18497 typval_T *rettv;
18498{
18499 float_T f;
18500
18501 rettv->v_type = VAR_FLOAT;
18502 if (get_float_arg(argvars, &f) == OK)
18503 rettv->vval.v_float = tanh(f);
18504 else
18505 rettv->vval.v_float = 0.0;
18506}
18507#endif
18508
Bram Moolenaard52d9742005-08-21 22:20:28 +000018509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 * "tolower(string)" function
18511 */
18512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018513f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018514 typval_T *argvars;
18515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516{
18517 char_u *p;
18518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018519 p = vim_strsave(get_tv_string(&argvars[0]));
18520 rettv->v_type = VAR_STRING;
18521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522
18523 if (p != NULL)
18524 while (*p != NUL)
18525 {
18526#ifdef FEAT_MBYTE
18527 int l;
18528
18529 if (enc_utf8)
18530 {
18531 int c, lc;
18532
18533 c = utf_ptr2char(p);
18534 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018535 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 /* TODO: reallocate string when byte count changes. */
18537 if (utf_char2len(lc) == l)
18538 utf_char2bytes(lc, p);
18539 p += l;
18540 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018541 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 p += l; /* skip multi-byte character */
18543 else
18544#endif
18545 {
18546 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18547 ++p;
18548 }
18549 }
18550}
18551
18552/*
18553 * "toupper(string)" function
18554 */
18555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018556f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018557 typval_T *argvars;
18558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018561 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562}
18563
18564/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018565 * "tr(string, fromstr, tostr)" function
18566 */
18567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 typval_T *argvars;
18570 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018571{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018572 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018573 char_u *fromstr;
18574 char_u *tostr;
18575 char_u *p;
18576#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018577 int inlen;
18578 int fromlen;
18579 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018580 int idx;
18581 char_u *cpstr;
18582 int cplen;
18583 int first = TRUE;
18584#endif
18585 char_u buf[NUMBUFLEN];
18586 char_u buf2[NUMBUFLEN];
18587 garray_T ga;
18588
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018589 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18591 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018592
18593 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->v_type = VAR_STRING;
18595 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018596 if (fromstr == NULL || tostr == NULL)
18597 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018598 ga_init2(&ga, (int)sizeof(char), 80);
18599
18600#ifdef FEAT_MBYTE
18601 if (!has_mbyte)
18602#endif
18603 /* not multi-byte: fromstr and tostr must be the same length */
18604 if (STRLEN(fromstr) != STRLEN(tostr))
18605 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018606#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018607error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018608#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018609 EMSG2(_(e_invarg2), fromstr);
18610 ga_clear(&ga);
18611 return;
18612 }
18613
18614 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018615 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018616 {
18617#ifdef FEAT_MBYTE
18618 if (has_mbyte)
18619 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018620 inlen = (*mb_ptr2len)(in_str);
18621 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018622 cplen = inlen;
18623 idx = 0;
18624 for (p = fromstr; *p != NUL; p += fromlen)
18625 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018626 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018627 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018628 {
18629 for (p = tostr; *p != NUL; p += tolen)
18630 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018631 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018632 if (idx-- == 0)
18633 {
18634 cplen = tolen;
18635 cpstr = p;
18636 break;
18637 }
18638 }
18639 if (*p == NUL) /* tostr is shorter than fromstr */
18640 goto error;
18641 break;
18642 }
18643 ++idx;
18644 }
18645
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018646 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018647 {
18648 /* Check that fromstr and tostr have the same number of
18649 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018650 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018651 first = FALSE;
18652 for (p = tostr; *p != NUL; p += tolen)
18653 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018654 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018655 --idx;
18656 }
18657 if (idx != 0)
18658 goto error;
18659 }
18660
18661 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018662 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018663 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018664
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018665 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018666 }
18667 else
18668#endif
18669 {
18670 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018671 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018672 if (p != NULL)
18673 ga_append(&ga, tostr[p - fromstr]);
18674 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018675 ga_append(&ga, *in_str);
18676 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018677 }
18678 }
18679
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018680 /* add a terminating NUL */
18681 ga_grow(&ga, 1);
18682 ga_append(&ga, NUL);
18683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018685}
18686
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018687#ifdef FEAT_FLOAT
18688/*
18689 * "trunc({float})" function
18690 */
18691 static void
18692f_trunc(argvars, rettv)
18693 typval_T *argvars;
18694 typval_T *rettv;
18695{
18696 float_T f;
18697
18698 rettv->v_type = VAR_FLOAT;
18699 if (get_float_arg(argvars, &f) == OK)
18700 /* trunc() is not in C90, use floor() or ceil() instead. */
18701 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18702 else
18703 rettv->vval.v_float = 0.0;
18704}
18705#endif
18706
Bram Moolenaar8299df92004-07-10 09:47:34 +000018707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 * "type(expr)" function
18709 */
18710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018712 typval_T *argvars;
18713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018715 int n;
18716
18717 switch (argvars[0].v_type)
18718 {
18719 case VAR_NUMBER: n = 0; break;
18720 case VAR_STRING: n = 1; break;
18721 case VAR_FUNC: n = 2; break;
18722 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018723 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018724#ifdef FEAT_FLOAT
18725 case VAR_FLOAT: n = 5; break;
18726#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018727 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18728 }
18729 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730}
18731
18732/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018733 * "undofile(name)" function
18734 */
18735 static void
18736f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018737 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018738 typval_T *rettv;
18739{
18740 rettv->v_type = VAR_STRING;
18741#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018742 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018743 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018744
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018745 if (*fname == NUL)
18746 {
18747 /* If there is no file name there will be no undo file. */
18748 rettv->vval.v_string = NULL;
18749 }
18750 else
18751 {
18752 char_u *ffname = FullName_save(fname, FALSE);
18753
18754 if (ffname != NULL)
18755 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18756 vim_free(ffname);
18757 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018758 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018759#else
18760 rettv->vval.v_string = NULL;
18761#endif
18762}
18763
18764/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018765 * "undotree()" function
18766 */
18767 static void
18768f_undotree(argvars, rettv)
18769 typval_T *argvars UNUSED;
18770 typval_T *rettv;
18771{
18772 if (rettv_dict_alloc(rettv) == OK)
18773 {
18774 dict_T *dict = rettv->vval.v_dict;
18775 list_T *list;
18776
Bram Moolenaar730cde92010-06-27 05:18:54 +020018777 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018778 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018779 dict_add_nr_str(dict, "save_last",
18780 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018781 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18782 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018783 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018784
18785 list = list_alloc();
18786 if (list != NULL)
18787 {
18788 u_eval_tree(curbuf->b_u_oldhead, list);
18789 dict_add_list(dict, "entries", list);
18790 }
18791 }
18792}
18793
18794/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018795 * "values(dict)" function
18796 */
18797 static void
18798f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 typval_T *argvars;
18800 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018801{
18802 dict_list(argvars, rettv, 1);
18803}
18804
18805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 * "virtcol(string)" function
18807 */
18808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 typval_T *argvars;
18811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812{
18813 colnr_T vcol = 0;
18814 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018815 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018817 fp = var2fpos(&argvars[0], FALSE, &fnum);
18818 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18819 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 {
18821 getvvcol(curwin, fp, NULL, NULL, &vcol);
18822 ++vcol;
18823 }
18824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018825 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826}
18827
18828/*
18829 * "visualmode()" function
18830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018832f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018833 typval_T *argvars UNUSED;
18834 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 char_u str[2];
18837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 str[0] = curbuf->b_visual_mode_eval;
18840 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842
18843 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018844 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
18848/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018849 * "wildmenumode()" function
18850 */
18851 static void
18852f_wildmenumode(argvars, rettv)
18853 typval_T *argvars UNUSED;
18854 typval_T *rettv UNUSED;
18855{
18856#ifdef FEAT_WILDMENU
18857 if (wild_menu_showing)
18858 rettv->vval.v_number = 1;
18859#endif
18860}
18861
18862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 * "winbufnr(nr)" function
18864 */
18865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018866f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 typval_T *argvars;
18868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869{
18870 win_T *wp;
18871
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018872 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018876 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877}
18878
18879/*
18880 * "wincol()" function
18881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018884 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886{
18887 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018888 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889}
18890
18891/*
18892 * "winheight(nr)" function
18893 */
18894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018895f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 typval_T *argvars;
18897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898{
18899 win_T *wp;
18900
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018901 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906}
18907
18908/*
18909 * "winline()" function
18910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018912f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018913 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
18916 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018917 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918}
18919
18920/*
18921 * "winnr()" function
18922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018925 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927{
18928 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018929
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018931 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934}
18935
18936/*
18937 * "winrestcmd()" function
18938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018941 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943{
18944#ifdef FEAT_WINDOWS
18945 win_T *wp;
18946 int winnr = 1;
18947 garray_T ga;
18948 char_u buf[50];
18949
18950 ga_init2(&ga, (int)sizeof(char), 70);
18951 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18952 {
18953 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18954 ga_concat(&ga, buf);
18955# ifdef FEAT_VERTSPLIT
18956 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18957 ga_concat(&ga, buf);
18958# endif
18959 ++winnr;
18960 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018961 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968}
18969
18970/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018971 * "winrestview()" function
18972 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018973 static void
18974f_winrestview(argvars, rettv)
18975 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018976 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018977{
18978 dict_T *dict;
18979
18980 if (argvars[0].v_type != VAR_DICT
18981 || (dict = argvars[0].vval.v_dict) == NULL)
18982 EMSG(_(e_invarg));
18983 else
18984 {
18985 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18986 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18987#ifdef FEAT_VIRTUALEDIT
18988 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18989#endif
18990 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018991 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018992
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018993 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018994#ifdef FEAT_DIFF
18995 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18996#endif
18997 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18998 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18999
19000 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019001 win_new_height(curwin, curwin->w_height);
19002# ifdef FEAT_VERTSPLIT
19003 win_new_width(curwin, W_WIDTH(curwin));
19004# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019005 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019006
19007 if (curwin->w_topline == 0)
19008 curwin->w_topline = 1;
19009 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19010 curwin->w_topline = curbuf->b_ml.ml_line_count;
19011#ifdef FEAT_DIFF
19012 check_topfill(curwin, TRUE);
19013#endif
19014 }
19015}
19016
19017/*
19018 * "winsaveview()" function
19019 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019020 static void
19021f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019022 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019023 typval_T *rettv;
19024{
19025 dict_T *dict;
19026
Bram Moolenaara800b422010-06-27 01:15:55 +020019027 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019028 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019029 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019030
19031 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19032 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19033#ifdef FEAT_VIRTUALEDIT
19034 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19035#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019036 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019037 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19038
19039 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19040#ifdef FEAT_DIFF
19041 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19042#endif
19043 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19044 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19045}
19046
19047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 * "winwidth(nr)" function
19049 */
19050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019052 typval_T *argvars;
19053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054{
19055 win_T *wp;
19056
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019057 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 else
19061#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019064 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065#endif
19066}
19067
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019069 * "writefile()" function
19070 */
19071 static void
19072f_writefile(argvars, rettv)
19073 typval_T *argvars;
19074 typval_T *rettv;
19075{
19076 int binary = FALSE;
19077 char_u *fname;
19078 FILE *fd;
19079 listitem_T *li;
19080 char_u *s;
19081 int ret = 0;
19082 int c;
19083
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019084 if (check_restricted() || check_secure())
19085 return;
19086
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019087 if (argvars[0].v_type != VAR_LIST)
19088 {
19089 EMSG2(_(e_listarg), "writefile()");
19090 return;
19091 }
19092 if (argvars[0].vval.v_list == NULL)
19093 return;
19094
19095 if (argvars[2].v_type != VAR_UNKNOWN
19096 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19097 binary = TRUE;
19098
19099 /* Always open the file in binary mode, library functions have a mind of
19100 * their own about CR-LF conversion. */
19101 fname = get_tv_string(&argvars[1]);
19102 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19103 {
19104 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19105 ret = -1;
19106 }
19107 else
19108 {
19109 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19110 li = li->li_next)
19111 {
19112 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19113 {
19114 if (*s == '\n')
19115 c = putc(NUL, fd);
19116 else
19117 c = putc(*s, fd);
19118 if (c == EOF)
19119 {
19120 ret = -1;
19121 break;
19122 }
19123 }
19124 if (!binary || li->li_next != NULL)
19125 if (putc('\n', fd) == EOF)
19126 {
19127 ret = -1;
19128 break;
19129 }
19130 if (ret < 0)
19131 {
19132 EMSG(_(e_write));
19133 break;
19134 }
19135 }
19136 fclose(fd);
19137 }
19138
19139 rettv->vval.v_number = ret;
19140}
19141
19142/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019143 * "xor(expr, expr)" function
19144 */
19145 static void
19146f_xor(argvars, rettv)
19147 typval_T *argvars;
19148 typval_T *rettv;
19149{
19150 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19151 ^ get_tv_number_chk(&argvars[1], NULL);
19152}
19153
19154
19155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019157 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 */
19159 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019160var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019162 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019163 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019165 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019167 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168
Bram Moolenaara5525202006-03-02 22:52:09 +000019169 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019170 if (varp->v_type == VAR_LIST)
19171 {
19172 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019173 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019174 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019175 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019176
19177 l = varp->vval.v_list;
19178 if (l == NULL)
19179 return NULL;
19180
19181 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019182 pos.lnum = list_find_nr(l, 0L, &error);
19183 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019184 return NULL; /* invalid line number */
19185
19186 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019187 pos.col = list_find_nr(l, 1L, &error);
19188 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019189 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019190 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019191
19192 /* We accept "$" for the column number: last column. */
19193 li = list_find(l, 1L);
19194 if (li != NULL && li->li_tv.v_type == VAR_STRING
19195 && li->li_tv.vval.v_string != NULL
19196 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19197 pos.col = len + 1;
19198
Bram Moolenaara5525202006-03-02 22:52:09 +000019199 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019200 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019201 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019202 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019203
Bram Moolenaara5525202006-03-02 22:52:09 +000019204#ifdef FEAT_VIRTUALEDIT
19205 /* Get the virtual offset. Defaults to zero. */
19206 pos.coladd = list_find_nr(l, 2L, &error);
19207 if (error)
19208 pos.coladd = 0;
19209#endif
19210
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019211 return &pos;
19212 }
19213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019214 name = get_tv_string_chk(varp);
19215 if (name == NULL)
19216 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019217 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019219 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19220 {
19221 if (VIsual_active)
19222 return &VIsual;
19223 return &curwin->w_cursor;
19224 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019225 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019227 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19229 return NULL;
19230 return pp;
19231 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019232
19233#ifdef FEAT_VIRTUALEDIT
19234 pos.coladd = 0;
19235#endif
19236
Bram Moolenaar477933c2007-07-17 14:32:23 +000019237 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019238 {
19239 pos.col = 0;
19240 if (name[1] == '0') /* "w0": first visible line */
19241 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019242 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019243 pos.lnum = curwin->w_topline;
19244 return &pos;
19245 }
19246 else if (name[1] == '$') /* "w$": last visible line */
19247 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019248 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019249 pos.lnum = curwin->w_botline - 1;
19250 return &pos;
19251 }
19252 }
19253 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019255 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 {
19257 pos.lnum = curbuf->b_ml.ml_line_count;
19258 pos.col = 0;
19259 }
19260 else
19261 {
19262 pos.lnum = curwin->w_cursor.lnum;
19263 pos.col = (colnr_T)STRLEN(ml_get_curline());
19264 }
19265 return &pos;
19266 }
19267 return NULL;
19268}
19269
19270/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019271 * Convert list in "arg" into a position and optional file number.
19272 * When "fnump" is NULL there is no file number, only 3 items.
19273 * Note that the column is passed on as-is, the caller may want to decrement
19274 * it to use 1 for the first column.
19275 * Return FAIL when conversion is not possible, doesn't check the position for
19276 * validity.
19277 */
19278 static int
19279list2fpos(arg, posp, fnump)
19280 typval_T *arg;
19281 pos_T *posp;
19282 int *fnump;
19283{
19284 list_T *l = arg->vval.v_list;
19285 long i = 0;
19286 long n;
19287
Bram Moolenaarbde35262006-07-23 20:12:24 +000019288 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19289 * when "fnump" isn't NULL and "coladd" is optional. */
19290 if (arg->v_type != VAR_LIST
19291 || l == NULL
19292 || l->lv_len < (fnump == NULL ? 2 : 3)
19293 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019294 return FAIL;
19295
19296 if (fnump != NULL)
19297 {
19298 n = list_find_nr(l, i++, NULL); /* fnum */
19299 if (n < 0)
19300 return FAIL;
19301 if (n == 0)
19302 n = curbuf->b_fnum; /* current buffer */
19303 *fnump = n;
19304 }
19305
19306 n = list_find_nr(l, i++, NULL); /* lnum */
19307 if (n < 0)
19308 return FAIL;
19309 posp->lnum = n;
19310
19311 n = list_find_nr(l, i++, NULL); /* col */
19312 if (n < 0)
19313 return FAIL;
19314 posp->col = n;
19315
19316#ifdef FEAT_VIRTUALEDIT
19317 n = list_find_nr(l, i, NULL);
19318 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019319 posp->coladd = 0;
19320 else
19321 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019322#endif
19323
19324 return OK;
19325}
19326
19327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 * Get the length of an environment variable name.
19329 * Advance "arg" to the first character after the name.
19330 * Return 0 for error.
19331 */
19332 static int
19333get_env_len(arg)
19334 char_u **arg;
19335{
19336 char_u *p;
19337 int len;
19338
19339 for (p = *arg; vim_isIDc(*p); ++p)
19340 ;
19341 if (p == *arg) /* no name found */
19342 return 0;
19343
19344 len = (int)(p - *arg);
19345 *arg = p;
19346 return len;
19347}
19348
19349/*
19350 * Get the length of the name of a function or internal variable.
19351 * "arg" is advanced to the first non-white character after the name.
19352 * Return 0 if something is wrong.
19353 */
19354 static int
19355get_id_len(arg)
19356 char_u **arg;
19357{
19358 char_u *p;
19359 int len;
19360
19361 /* Find the end of the name. */
19362 for (p = *arg; eval_isnamec(*p); ++p)
19363 ;
19364 if (p == *arg) /* no name found */
19365 return 0;
19366
19367 len = (int)(p - *arg);
19368 *arg = skipwhite(p);
19369
19370 return len;
19371}
19372
19373/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019374 * Get the length of the name of a variable or function.
19375 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019377 * Return -1 if curly braces expansion failed.
19378 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 * If the name contains 'magic' {}'s, expand them and return the
19380 * expanded name in an allocated string via 'alias' - caller must free.
19381 */
19382 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019383get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 char_u **arg;
19385 char_u **alias;
19386 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019387 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388{
19389 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 char_u *p;
19391 char_u *expr_start;
19392 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393
19394 *alias = NULL; /* default to no alias */
19395
19396 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19397 && (*arg)[2] == (int)KE_SNR)
19398 {
19399 /* hard coded <SNR>, already translated */
19400 *arg += 3;
19401 return get_id_len(arg) + 3;
19402 }
19403 len = eval_fname_script(*arg);
19404 if (len > 0)
19405 {
19406 /* literal "<SID>", "s:" or "<SNR>" */
19407 *arg += len;
19408 }
19409
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019413 p = find_name_end(*arg, &expr_start, &expr_end,
19414 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 if (expr_start != NULL)
19416 {
19417 char_u *temp_string;
19418
19419 if (!evaluate)
19420 {
19421 len += (int)(p - *arg);
19422 *arg = skipwhite(p);
19423 return len;
19424 }
19425
19426 /*
19427 * Include any <SID> etc in the expanded string:
19428 * Thus the -len here.
19429 */
19430 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19431 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019432 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 *alias = temp_string;
19434 *arg = skipwhite(p);
19435 return (int)STRLEN(temp_string);
19436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437
19438 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019439 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 EMSG2(_(e_invexpr2), *arg);
19441
19442 return len;
19443}
19444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445/*
19446 * Find the end of a variable or function name, taking care of magic braces.
19447 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19448 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019449 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019450 * Return a pointer to just after the name. Equal to "arg" if there is no
19451 * valid name.
19452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019454find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 char_u *arg;
19456 char_u **expr_start;
19457 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019458 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 int mb_nest = 0;
19461 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 char_u *p;
19463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019464 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 *expr_start = NULL;
19467 *expr_end = NULL;
19468 }
19469
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019470 /* Quick check for valid starting character. */
19471 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19472 return arg;
19473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019474 for (p = arg; *p != NUL
19475 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019476 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019477 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019478 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019479 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019481 if (*p == '\'')
19482 {
19483 /* skip over 'string' to avoid counting [ and ] inside it. */
19484 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19485 ;
19486 if (*p == NUL)
19487 break;
19488 }
19489 else if (*p == '"')
19490 {
19491 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19492 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19493 if (*p == '\\' && p[1] != NUL)
19494 ++p;
19495 if (*p == NUL)
19496 break;
19497 }
19498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019501 if (*p == '[')
19502 ++br_nest;
19503 else if (*p == ']')
19504 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 if (*p == '{')
19510 {
19511 mb_nest++;
19512 if (expr_start != NULL && *expr_start == NULL)
19513 *expr_start = p;
19514 }
19515 else if (*p == '}')
19516 {
19517 mb_nest--;
19518 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19519 *expr_end = p;
19520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
19523
19524 return p;
19525}
19526
19527/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019528 * Expands out the 'magic' {}'s in a variable/function name.
19529 * Note that this can call itself recursively, to deal with
19530 * constructs like foo{bar}{baz}{bam}
19531 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19532 * "in_start" ^
19533 * "expr_start" ^
19534 * "expr_end" ^
19535 * "in_end" ^
19536 *
19537 * Returns a new allocated string, which the caller must free.
19538 * Returns NULL for failure.
19539 */
19540 static char_u *
19541make_expanded_name(in_start, expr_start, expr_end, in_end)
19542 char_u *in_start;
19543 char_u *expr_start;
19544 char_u *expr_end;
19545 char_u *in_end;
19546{
19547 char_u c1;
19548 char_u *retval = NULL;
19549 char_u *temp_result;
19550 char_u *nextcmd = NULL;
19551
19552 if (expr_end == NULL || in_end == NULL)
19553 return NULL;
19554 *expr_start = NUL;
19555 *expr_end = NUL;
19556 c1 = *in_end;
19557 *in_end = NUL;
19558
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019559 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019560 if (temp_result != NULL && nextcmd == NULL)
19561 {
19562 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19563 + (in_end - expr_end) + 1));
19564 if (retval != NULL)
19565 {
19566 STRCPY(retval, in_start);
19567 STRCAT(retval, temp_result);
19568 STRCAT(retval, expr_end + 1);
19569 }
19570 }
19571 vim_free(temp_result);
19572
19573 *in_end = c1; /* put char back for error messages */
19574 *expr_start = '{';
19575 *expr_end = '}';
19576
19577 if (retval != NULL)
19578 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019579 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019580 if (expr_start != NULL)
19581 {
19582 /* Further expansion! */
19583 temp_result = make_expanded_name(retval, expr_start,
19584 expr_end, temp_result);
19585 vim_free(retval);
19586 retval = temp_result;
19587 }
19588 }
19589
19590 return retval;
19591}
19592
19593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019595 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 */
19597 static int
19598eval_isnamec(c)
19599 int c;
19600{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019601 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19602}
19603
19604/*
19605 * Return TRUE if character "c" can be used as the first character in a
19606 * variable or function name (excluding '{' and '}').
19607 */
19608 static int
19609eval_isnamec1(c)
19610 int c;
19611{
19612 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613}
19614
19615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 * Set number v: variable to "val".
19617 */
19618 void
19619set_vim_var_nr(idx, val)
19620 int idx;
19621 long val;
19622{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019623 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624}
19625
19626/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019627 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 */
19629 long
19630get_vim_var_nr(idx)
19631 int idx;
19632{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019633 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634}
19635
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019636/*
19637 * Get string v: variable value. Uses a static buffer, can only be used once.
19638 */
19639 char_u *
19640get_vim_var_str(idx)
19641 int idx;
19642{
19643 return get_tv_string(&vimvars[idx].vv_tv);
19644}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019645
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019647 * Get List v: variable value. Caller must take care of reference count when
19648 * needed.
19649 */
19650 list_T *
19651get_vim_var_list(idx)
19652 int idx;
19653{
19654 return vimvars[idx].vv_list;
19655}
19656
19657/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019658 * Set v:char to character "c".
19659 */
19660 void
19661set_vim_var_char(c)
19662 int c;
19663{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019664 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019665
19666#ifdef FEAT_MBYTE
19667 if (has_mbyte)
19668 buf[(*mb_char2bytes)(c, buf)] = NUL;
19669 else
19670#endif
19671 {
19672 buf[0] = c;
19673 buf[1] = NUL;
19674 }
19675 set_vim_var_string(VV_CHAR, buf, -1);
19676}
19677
19678/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019679 * Set v:count to "count" and v:count1 to "count1".
19680 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 */
19682 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019683set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 long count;
19685 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019686 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019688 if (set_prevcount)
19689 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019690 vimvars[VV_COUNT].vv_nr = count;
19691 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692}
19693
19694/*
19695 * Set string v: variable to a copy of "val".
19696 */
19697 void
19698set_vim_var_string(idx, val, len)
19699 int idx;
19700 char_u *val;
19701 int len; /* length of "val" to use or -1 (whole string) */
19702{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019703 /* Need to do this (at least) once, since we can't initialize a union.
19704 * Will always be invoked when "v:progname" is set. */
19705 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19706
Bram Moolenaare9a41262005-01-15 22:18:47 +000019707 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019709 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019711 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019713 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714}
19715
19716/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019717 * Set List v: variable to "val".
19718 */
19719 void
19720set_vim_var_list(idx, val)
19721 int idx;
19722 list_T *val;
19723{
19724 list_unref(vimvars[idx].vv_list);
19725 vimvars[idx].vv_list = val;
19726 if (val != NULL)
19727 ++val->lv_refcount;
19728}
19729
19730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 * Set v:register if needed.
19732 */
19733 void
19734set_reg_var(c)
19735 int c;
19736{
19737 char_u regname;
19738
19739 if (c == 0 || c == ' ')
19740 regname = '"';
19741 else
19742 regname = c;
19743 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 set_vim_var_string(VV_REG, &regname, 1);
19746}
19747
19748/*
19749 * Get or set v:exception. If "oldval" == NULL, return the current value.
19750 * Otherwise, restore the value to "oldval" and return NULL.
19751 * Must always be called in pairs to save and restore v:exception! Does not
19752 * take care of memory allocations.
19753 */
19754 char_u *
19755v_exception(oldval)
19756 char_u *oldval;
19757{
19758 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019759 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760
Bram Moolenaare9a41262005-01-15 22:18:47 +000019761 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 return NULL;
19763}
19764
19765/*
19766 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19767 * Otherwise, restore the value to "oldval" and return NULL.
19768 * Must always be called in pairs to save and restore v:throwpoint! Does not
19769 * take care of memory allocations.
19770 */
19771 char_u *
19772v_throwpoint(oldval)
19773 char_u *oldval;
19774{
19775 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019776 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
Bram Moolenaare9a41262005-01-15 22:18:47 +000019778 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 return NULL;
19780}
19781
19782#if defined(FEAT_AUTOCMD) || defined(PROTO)
19783/*
19784 * Set v:cmdarg.
19785 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19786 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19787 * Must always be called in pairs!
19788 */
19789 char_u *
19790set_cmdarg(eap, oldarg)
19791 exarg_T *eap;
19792 char_u *oldarg;
19793{
19794 char_u *oldval;
19795 char_u *newval;
19796 unsigned len;
19797
Bram Moolenaare9a41262005-01-15 22:18:47 +000019798 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019799 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019801 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019802 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019803 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 }
19805
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019806 if (eap->force_bin == FORCE_BIN)
19807 len = 6;
19808 else if (eap->force_bin == FORCE_NOBIN)
19809 len = 8;
19810 else
19811 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019812
19813 if (eap->read_edit)
19814 len += 7;
19815
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019816 if (eap->force_ff != 0)
19817 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19818# ifdef FEAT_MBYTE
19819 if (eap->force_enc != 0)
19820 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019821 if (eap->bad_char != 0)
19822 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019823# endif
19824
19825 newval = alloc(len + 1);
19826 if (newval == NULL)
19827 return NULL;
19828
19829 if (eap->force_bin == FORCE_BIN)
19830 sprintf((char *)newval, " ++bin");
19831 else if (eap->force_bin == FORCE_NOBIN)
19832 sprintf((char *)newval, " ++nobin");
19833 else
19834 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019835
19836 if (eap->read_edit)
19837 STRCAT(newval, " ++edit");
19838
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019839 if (eap->force_ff != 0)
19840 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19841 eap->cmd + eap->force_ff);
19842# ifdef FEAT_MBYTE
19843 if (eap->force_enc != 0)
19844 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19845 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019846 if (eap->bad_char == BAD_KEEP)
19847 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19848 else if (eap->bad_char == BAD_DROP)
19849 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19850 else if (eap->bad_char != 0)
19851 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019852# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019853 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019854 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855}
19856#endif
19857
19858/*
19859 * Get the value of internal variable "name".
19860 * Return OK or FAIL.
19861 */
19862 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019863get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 char_u *name;
19865 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019866 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019867 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019868 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869{
19870 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019871 typval_T *tv = NULL;
19872 typval_T atv;
19873 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875
19876 /* truncate the name, so that we can use strcmp() */
19877 cc = name[len];
19878 name[len] = NUL;
19879
19880 /*
19881 * Check for "b:changedtick".
19882 */
19883 if (STRCMP(name, "b:changedtick") == 0)
19884 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019885 atv.v_type = VAR_NUMBER;
19886 atv.vval.v_number = curbuf->b_changedtick;
19887 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 }
19889
19890 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 * Check for user-defined variables.
19892 */
19893 else
19894 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019895 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 }
19899
Bram Moolenaare9a41262005-01-15 22:18:47 +000019900 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019902 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 ret = FAIL;
19905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019907 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908
19909 name[len] = cc;
19910
19911 return ret;
19912}
19913
19914/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019915 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19916 * Also handle function call with Funcref variable: func(expr)
19917 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19918 */
19919 static int
19920handle_subscript(arg, rettv, evaluate, verbose)
19921 char_u **arg;
19922 typval_T *rettv;
19923 int evaluate; /* do more than finding the end */
19924 int verbose; /* give error messages */
19925{
19926 int ret = OK;
19927 dict_T *selfdict = NULL;
19928 char_u *s;
19929 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019930 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019931
19932 while (ret == OK
19933 && (**arg == '['
19934 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019935 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019936 && !vim_iswhite(*(*arg - 1)))
19937 {
19938 if (**arg == '(')
19939 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019940 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019941 if (evaluate)
19942 {
19943 functv = *rettv;
19944 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019945
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019946 /* Invoke the function. Recursive! */
19947 s = functv.vval.v_string;
19948 }
19949 else
19950 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019951 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019952 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19953 &len, evaluate, selfdict);
19954
19955 /* Clear the funcref afterwards, so that deleting it while
19956 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019957 if (evaluate)
19958 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019959
19960 /* Stop the expression evaluation when immediately aborting on
19961 * error, or when an interrupt occurred or an exception was thrown
19962 * but not caught. */
19963 if (aborting())
19964 {
19965 if (ret == OK)
19966 clear_tv(rettv);
19967 ret = FAIL;
19968 }
19969 dict_unref(selfdict);
19970 selfdict = NULL;
19971 }
19972 else /* **arg == '[' || **arg == '.' */
19973 {
19974 dict_unref(selfdict);
19975 if (rettv->v_type == VAR_DICT)
19976 {
19977 selfdict = rettv->vval.v_dict;
19978 if (selfdict != NULL)
19979 ++selfdict->dv_refcount;
19980 }
19981 else
19982 selfdict = NULL;
19983 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19984 {
19985 clear_tv(rettv);
19986 ret = FAIL;
19987 }
19988 }
19989 }
19990 dict_unref(selfdict);
19991 return ret;
19992}
19993
19994/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019995 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019996 * value).
19997 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019998 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000{
Bram Moolenaar33570922005-01-25 22:26:29 +000020001 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020002}
20003
20004/*
20005 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 * The string "s" must have been allocated, it is consumed.
20007 * Return NULL for out of memory, the variable otherwise.
20008 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020009 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 char_u *s;
20012{
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 rettv = alloc_tv();
20016 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 rettv->v_type = VAR_STRING;
20019 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
20021 else
20022 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024}
20025
20026/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020029 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020030free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032{
20033 if (varp != NULL)
20034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020035 switch (varp->v_type)
20036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020037 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 func_unref(varp->vval.v_string);
20039 /*FALLTHROUGH*/
20040 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020041 vim_free(varp->vval.v_string);
20042 break;
20043 case VAR_LIST:
20044 list_unref(varp->vval.v_list);
20045 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020046 case VAR_DICT:
20047 dict_unref(varp->vval.v_dict);
20048 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020049 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020050#ifdef FEAT_FLOAT
20051 case VAR_FLOAT:
20052#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020053 case VAR_UNKNOWN:
20054 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020055 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020056 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020057 break;
20058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 vim_free(varp);
20060 }
20061}
20062
20063/*
20064 * Free the memory for a variable value and set the value to NULL or 0.
20065 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020066 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
20070 if (varp != NULL)
20071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020072 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020074 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 func_unref(varp->vval.v_string);
20076 /*FALLTHROUGH*/
20077 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020078 vim_free(varp->vval.v_string);
20079 varp->vval.v_string = NULL;
20080 break;
20081 case VAR_LIST:
20082 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020083 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020084 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020085 case VAR_DICT:
20086 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020087 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020088 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 varp->vval.v_number = 0;
20091 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020092#ifdef FEAT_FLOAT
20093 case VAR_FLOAT:
20094 varp->vval.v_float = 0.0;
20095 break;
20096#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 case VAR_UNKNOWN:
20098 break;
20099 default:
20100 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020102 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 }
20104}
20105
20106/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 * Set the value of a variable to NULL without freeing items.
20108 */
20109 static void
20110init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112{
20113 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115}
20116
20117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 * Get the number value of a variable.
20119 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020120 * For incompatible types, return 0.
20121 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20122 * caller of incompatible types: it sets *denote to TRUE if "denote"
20123 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 */
20125 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020129 int error = FALSE;
20130
20131 return get_tv_number_chk(varp, &error); /* return 0L on error */
20132}
20133
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020134 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020135get_tv_number_chk(varp, denote)
20136 typval_T *varp;
20137 int *denote;
20138{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020139 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020141 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020143 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020144 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020145#ifdef FEAT_FLOAT
20146 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020147 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020148 break;
20149#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020150 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020151 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020152 break;
20153 case VAR_STRING:
20154 if (varp->vval.v_string != NULL)
20155 vim_str2nr(varp->vval.v_string, NULL, NULL,
20156 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020157 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020158 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020159 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020160 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020161 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020162 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020163 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020164 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020165 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020168 if (denote == NULL) /* useful for values that must be unsigned */
20169 n = -1;
20170 else
20171 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020172 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173}
20174
20175/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020176 * Get the lnum from the first argument.
20177 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020178 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 */
20180 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020181get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183{
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 linenr_T lnum;
20186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020187 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 if (lnum == 0) /* no valid number, try using line() */
20189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv.v_type = VAR_NUMBER;
20191 f_line(argvars, &rettv);
20192 lnum = rettv.vval.v_number;
20193 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 }
20195 return lnum;
20196}
20197
20198/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020199 * Get the lnum from the first argument.
20200 * Also accepts "$", then "buf" is used.
20201 * Returns 0 on error.
20202 */
20203 static linenr_T
20204get_tv_lnum_buf(argvars, buf)
20205 typval_T *argvars;
20206 buf_T *buf;
20207{
20208 if (argvars[0].v_type == VAR_STRING
20209 && argvars[0].vval.v_string != NULL
20210 && argvars[0].vval.v_string[0] == '$'
20211 && buf != NULL)
20212 return buf->b_ml.ml_line_count;
20213 return get_tv_number_chk(&argvars[0], NULL);
20214}
20215
20216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 * Get the string value of a variable.
20218 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020219 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20220 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 * If the String variable has never been set, return an empty string.
20222 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020223 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20224 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 */
20226 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229{
20230 static char_u mybuf[NUMBUFLEN];
20231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233}
20234
20235 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020236get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020238 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020240 char_u *res = get_tv_string_buf_chk(varp, buf);
20241
20242 return res != NULL ? res : (char_u *)"";
20243}
20244
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020245 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020246get_tv_string_chk(varp)
20247 typval_T *varp;
20248{
20249 static char_u mybuf[NUMBUFLEN];
20250
20251 return get_tv_string_buf_chk(varp, mybuf);
20252}
20253
20254 static char_u *
20255get_tv_string_buf_chk(varp, buf)
20256 typval_T *varp;
20257 char_u *buf;
20258{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020259 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020261 case VAR_NUMBER:
20262 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20263 return buf;
20264 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020265 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020266 break;
20267 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020268 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020269 break;
20270 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020271 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020272 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020273#ifdef FEAT_FLOAT
20274 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020275 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020276 break;
20277#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020278 case VAR_STRING:
20279 if (varp->vval.v_string != NULL)
20280 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020281 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020282 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020283 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020284 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020286 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287}
20288
20289/*
20290 * Find variable "name" in the list of variables.
20291 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020292 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020293 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020294 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020297find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020300 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304
Bram Moolenaara7043832005-01-21 11:56:39 +000020305 ht = find_var_ht(name, &varname);
20306 if (htp != NULL)
20307 *htp = ht;
20308 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020310 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311}
20312
20313/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020314 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020315 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020317 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020318find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020320 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020321 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020322 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020323{
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 hashitem_T *hi;
20325
20326 if (*varname == NUL)
20327 {
20328 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020329 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020330 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020331 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 case 'g': return &globvars_var;
20333 case 'v': return &vimvars_var;
20334 case 'b': return &curbuf->b_bufvar;
20335 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020336#ifdef FEAT_WINDOWS
20337 case 't': return &curtab->tp_winvar;
20338#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020339 case 'l': return current_funccal == NULL
20340 ? NULL : &current_funccal->l_vars_var;
20341 case 'a': return current_funccal == NULL
20342 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020343 }
20344 return NULL;
20345 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020346
20347 hi = hash_find(ht, varname);
20348 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020349 {
20350 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020351 * worked find the variable again. Don't auto-load a script if it was
20352 * loaded already, otherwise it would be loaded every time when
20353 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020354 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020355 {
20356 /* Note: script_autoload() may make "hi" invalid. It must either
20357 * be obtained again or not used. */
20358 if (!script_autoload(varname, FALSE) || aborting())
20359 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020360 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020361 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020362 if (HASHITEM_EMPTY(hi))
20363 return NULL;
20364 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020365 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020366}
20367
20368/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020370 * Set "varname" to the start of name without ':'.
20371 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020372 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020373find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 char_u *name;
20375 char_u **varname;
20376{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020377 hashitem_T *hi;
20378
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 if (name[1] != ':')
20380 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020381 /* The name must not start with a colon or #. */
20382 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 return NULL;
20384 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020385
20386 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020387 hi = hash_find(&compat_hashtab, name);
20388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020389 return &compat_hashtab;
20390
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 return &globvarht; /* global variable */
20393 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 }
20395 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020396 if (*name == 'g') /* global variable */
20397 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020398 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20399 */
20400 if (vim_strchr(name + 2, ':') != NULL
20401 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020404 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020406 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020407#ifdef FEAT_WINDOWS
20408 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020409 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020410#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 if (*name == 'v') /* v: variable */
20412 return &vimvarht;
20413 if (*name == 'a' && current_funccal != NULL) /* function argument */
20414 return &current_funccal->l_avars.dv_hashtab;
20415 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20416 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 if (*name == 's' /* script variable */
20418 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20419 return &SCRIPT_VARS(current_SID);
20420 return NULL;
20421}
20422
20423/*
20424 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020425 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 * Returns NULL when it doesn't exist.
20427 */
20428 char_u *
20429get_var_value(name)
20430 char_u *name;
20431{
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020434 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 if (v == NULL)
20436 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020437 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438}
20439
20440/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020441 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 * sourcing this script and when executing functions defined in the script.
20443 */
20444 void
20445new_script_vars(id)
20446 scid_T id;
20447{
Bram Moolenaara7043832005-01-21 11:56:39 +000020448 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 hashtab_T *ht;
20450 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020451
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20453 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020454 /* Re-allocating ga_data means that an ht_array pointing to
20455 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020456 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020457 for (i = 1; i <= ga_scripts.ga_len; ++i)
20458 {
20459 ht = &SCRIPT_VARS(i);
20460 if (ht->ht_mask == HT_INIT_SIZE - 1)
20461 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020462 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020463 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 }
20465
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 while (ga_scripts.ga_len < id)
20467 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020468 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020469 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020470 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 }
20473 }
20474}
20475
20476/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20478 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 */
20480 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020481init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 dict_T *dict;
20483 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020484 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485{
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020487 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020488 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020489 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020490 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020491 dict_var->di_tv.vval.v_dict = dict;
20492 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020493 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20495 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496}
20497
20498/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020499 * Unreference a dictionary initialized by init_var_dict().
20500 */
20501 void
20502unref_var_dict(dict)
20503 dict_T *dict;
20504{
20505 /* Now the dict needs to be freed if no one else is using it, go back to
20506 * normal reference counting. */
20507 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20508 dict_unref(dict);
20509}
20510
20511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020513 * Frees all allocated variables and the value they contain.
20514 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 */
20516 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020517vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020518 hashtab_T *ht;
20519{
20520 vars_clear_ext(ht, TRUE);
20521}
20522
20523/*
20524 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20525 */
20526 static void
20527vars_clear_ext(ht, free_val)
20528 hashtab_T *ht;
20529 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530{
Bram Moolenaara7043832005-01-21 11:56:39 +000020531 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 hashitem_T *hi;
20533 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020536 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020537 for (hi = ht->ht_array; todo > 0; ++hi)
20538 {
20539 if (!HASHITEM_EMPTY(hi))
20540 {
20541 --todo;
20542
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020544 * ht_array might change then. hash_clear() takes care of it
20545 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020546 v = HI2DI(hi);
20547 if (free_val)
20548 clear_tv(&v->di_tv);
20549 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20550 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020551 }
20552 }
20553 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020554 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555}
20556
Bram Moolenaara7043832005-01-21 11:56:39 +000020557/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020558 * Delete a variable from hashtab "ht" at item "hi".
20559 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020562delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 hashtab_T *ht;
20564 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565{
Bram Moolenaar33570922005-01-25 22:26:29 +000020566 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020567
20568 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020569 clear_tv(&di->di_tv);
20570 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
20574 * List the value of one internal variable.
20575 */
20576 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020577list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020580 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020582 char_u *tofree;
20583 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020584 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020585
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020586 current_copyID += COPYID_INC;
20587 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020589 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591}
20592
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020594list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 char_u *prefix;
20596 char_u *name;
20597 int type;
20598 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020599 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600{
Bram Moolenaar31859182007-08-14 20:41:13 +000020601 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20602 msg_start();
20603 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 if (name != NULL) /* "a:" vars don't have a name stored */
20605 msg_puts(name);
20606 msg_putchar(' ');
20607 msg_advance(22);
20608 if (type == VAR_NUMBER)
20609 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020610 else if (type == VAR_FUNC)
20611 msg_putchar('*');
20612 else if (type == VAR_LIST)
20613 {
20614 msg_putchar('[');
20615 if (*string == '[')
20616 ++string;
20617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020618 else if (type == VAR_DICT)
20619 {
20620 msg_putchar('{');
20621 if (*string == '{')
20622 ++string;
20623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 else
20625 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020626
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020628
20629 if (type == VAR_FUNC)
20630 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020631 if (*first)
20632 {
20633 msg_clr_eos();
20634 *first = FALSE;
20635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636}
20637
20638/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 * If the variable already exists, the value is updated.
20641 * Otherwise the variable is created.
20642 */
20643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020644set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020646 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020647 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648{
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020651 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020653 ht = find_var_ht(name, &varname);
20654 if (ht == NULL || *varname == NUL)
20655 {
20656 EMSG2(_(e_illvar), name);
20657 return;
20658 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020659 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020660
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020661 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20662 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020663
Bram Moolenaar33570922005-01-25 22:26:29 +000020664 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020666 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020667 if (var_check_ro(v->di_flags, name)
20668 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 return;
20670 if (v->di_tv.v_type != tv->v_type
20671 && !((v->di_tv.v_type == VAR_STRING
20672 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020673 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020674 || tv->v_type == VAR_NUMBER))
20675#ifdef FEAT_FLOAT
20676 && !((v->di_tv.v_type == VAR_NUMBER
20677 || v->di_tv.v_type == VAR_FLOAT)
20678 && (tv->v_type == VAR_NUMBER
20679 || tv->v_type == VAR_FLOAT))
20680#endif
20681 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020682 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020683 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020684 return;
20685 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020686
20687 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020688 * Handle setting internal v: variables separately: we don't change
20689 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 */
20691 if (ht == &vimvarht)
20692 {
20693 if (v->di_tv.v_type == VAR_STRING)
20694 {
20695 vim_free(v->di_tv.vval.v_string);
20696 if (copy || tv->v_type != VAR_STRING)
20697 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20698 else
20699 {
20700 /* Take over the string to avoid an extra alloc/free. */
20701 v->di_tv.vval.v_string = tv->vval.v_string;
20702 tv->vval.v_string = NULL;
20703 }
20704 }
20705 else if (v->di_tv.v_type != VAR_NUMBER)
20706 EMSG2(_(e_intern2), "set_var()");
20707 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020708 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020709 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020710 if (STRCMP(varname, "searchforward") == 0)
20711 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020712#ifdef FEAT_SEARCH_EXTRA
20713 else if (STRCMP(varname, "hlsearch") == 0)
20714 {
20715 no_hlsearch = !v->di_tv.vval.v_number;
20716 redraw_all_later(SOME_VALID);
20717 }
20718#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020719 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020720 return;
20721 }
20722
20723 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 }
20725 else /* add a new variable */
20726 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020727 /* Can't add "v:" variable. */
20728 if (ht == &vimvarht)
20729 {
20730 EMSG2(_(e_illvar), name);
20731 return;
20732 }
20733
Bram Moolenaar92124a32005-06-17 22:03:40 +000020734 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020735 if (!valid_varname(varname))
20736 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020737
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020738 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20739 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020740 if (v == NULL)
20741 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020742 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020743 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020745 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 return;
20747 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020748 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020750
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020751 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020752 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020753 else
20754 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020755 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020756 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020757 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759}
20760
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020761/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020762 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020763 * Also give an error message.
20764 */
20765 static int
20766var_check_ro(flags, name)
20767 int flags;
20768 char_u *name;
20769{
20770 if (flags & DI_FLAGS_RO)
20771 {
20772 EMSG2(_(e_readonlyvar), name);
20773 return TRUE;
20774 }
20775 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20776 {
20777 EMSG2(_(e_readonlysbx), name);
20778 return TRUE;
20779 }
20780 return FALSE;
20781}
20782
20783/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020784 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20785 * Also give an error message.
20786 */
20787 static int
20788var_check_fixed(flags, name)
20789 int flags;
20790 char_u *name;
20791{
20792 if (flags & DI_FLAGS_FIX)
20793 {
20794 EMSG2(_("E795: Cannot delete variable %s"), name);
20795 return TRUE;
20796 }
20797 return FALSE;
20798}
20799
20800/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020801 * Check if a funcref is assigned to a valid variable name.
20802 * Return TRUE and give an error if not.
20803 */
20804 static int
20805var_check_func_name(name, new_var)
20806 char_u *name; /* points to start of variable name */
20807 int new_var; /* TRUE when creating the variable */
20808{
20809 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20810 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20811 ? name[2] : name[0]))
20812 {
20813 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20814 name);
20815 return TRUE;
20816 }
20817 /* Don't allow hiding a function. When "v" is not NULL we might be
20818 * assigning another function to the same var, the type is checked
20819 * below. */
20820 if (new_var && function_exists(name))
20821 {
20822 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20823 name);
20824 return TRUE;
20825 }
20826 return FALSE;
20827}
20828
20829/*
20830 * Check if a variable name is valid.
20831 * Return FALSE and give an error if not.
20832 */
20833 static int
20834valid_varname(varname)
20835 char_u *varname;
20836{
20837 char_u *p;
20838
20839 for (p = varname; *p != NUL; ++p)
20840 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20841 && *p != AUTOLOAD_CHAR)
20842 {
20843 EMSG2(_(e_illvar), varname);
20844 return FALSE;
20845 }
20846 return TRUE;
20847}
20848
20849/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020850 * Return TRUE if typeval "tv" is set to be locked (immutable).
20851 * Also give an error message, using "name".
20852 */
20853 static int
20854tv_check_lock(lock, name)
20855 int lock;
20856 char_u *name;
20857{
20858 if (lock & VAR_LOCKED)
20859 {
20860 EMSG2(_("E741: Value is locked: %s"),
20861 name == NULL ? (char_u *)_("Unknown") : name);
20862 return TRUE;
20863 }
20864 if (lock & VAR_FIXED)
20865 {
20866 EMSG2(_("E742: Cannot change value of %s"),
20867 name == NULL ? (char_u *)_("Unknown") : name);
20868 return TRUE;
20869 }
20870 return FALSE;
20871}
20872
20873/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020874 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020875 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020876 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020877 * It is OK for "from" and "to" to point to the same item. This is used to
20878 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020879 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020880 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020882 typval_T *from;
20883 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020885 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020886 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020887 switch (from->v_type)
20888 {
20889 case VAR_NUMBER:
20890 to->vval.v_number = from->vval.v_number;
20891 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020892#ifdef FEAT_FLOAT
20893 case VAR_FLOAT:
20894 to->vval.v_float = from->vval.v_float;
20895 break;
20896#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020897 case VAR_STRING:
20898 case VAR_FUNC:
20899 if (from->vval.v_string == NULL)
20900 to->vval.v_string = NULL;
20901 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020903 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020904 if (from->v_type == VAR_FUNC)
20905 func_ref(to->vval.v_string);
20906 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020907 break;
20908 case VAR_LIST:
20909 if (from->vval.v_list == NULL)
20910 to->vval.v_list = NULL;
20911 else
20912 {
20913 to->vval.v_list = from->vval.v_list;
20914 ++to->vval.v_list->lv_refcount;
20915 }
20916 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020917 case VAR_DICT:
20918 if (from->vval.v_dict == NULL)
20919 to->vval.v_dict = NULL;
20920 else
20921 {
20922 to->vval.v_dict = from->vval.v_dict;
20923 ++to->vval.v_dict->dv_refcount;
20924 }
20925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020927 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928 break;
20929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930}
20931
20932/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020933 * Make a copy of an item.
20934 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020935 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20936 * reference to an already copied list/dict can be used.
20937 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020938 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020939 static int
20940item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020941 typval_T *from;
20942 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020943 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020944 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020945{
20946 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020947 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020948
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020950 {
20951 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020952 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020953 }
20954 ++recurse;
20955
20956 switch (from->v_type)
20957 {
20958 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020959#ifdef FEAT_FLOAT
20960 case VAR_FLOAT:
20961#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020962 case VAR_STRING:
20963 case VAR_FUNC:
20964 copy_tv(from, to);
20965 break;
20966 case VAR_LIST:
20967 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020968 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020969 if (from->vval.v_list == NULL)
20970 to->vval.v_list = NULL;
20971 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20972 {
20973 /* use the copy made earlier */
20974 to->vval.v_list = from->vval.v_list->lv_copylist;
20975 ++to->vval.v_list->lv_refcount;
20976 }
20977 else
20978 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20979 if (to->vval.v_list == NULL)
20980 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020981 break;
20982 case VAR_DICT:
20983 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020984 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020985 if (from->vval.v_dict == NULL)
20986 to->vval.v_dict = NULL;
20987 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20988 {
20989 /* use the copy made earlier */
20990 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20991 ++to->vval.v_dict->dv_refcount;
20992 }
20993 else
20994 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20995 if (to->vval.v_dict == NULL)
20996 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020997 break;
20998 default:
20999 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021000 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021001 }
21002 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021003 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021004}
21005
21006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 * ":echo expr1 ..." print each argument separated with a space, add a
21008 * newline at the end.
21009 * ":echon expr1 ..." print each argument plain.
21010 */
21011 void
21012ex_echo(eap)
21013 exarg_T *eap;
21014{
21015 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021017 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 char_u *p;
21019 int needclr = TRUE;
21020 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021021 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022
21023 if (eap->skip)
21024 ++emsg_skip;
21025 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21026 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021027 /* If eval1() causes an error message the text from the command may
21028 * still need to be cleared. E.g., "echo 22,44". */
21029 need_clr_eos = needclr;
21030
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 {
21034 /*
21035 * Report the invalid expression unless the expression evaluation
21036 * has been cancelled due to an aborting error, an interrupt, or an
21037 * exception.
21038 */
21039 if (!aborting())
21040 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021041 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 break;
21043 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021044 need_clr_eos = FALSE;
21045
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 if (!eap->skip)
21047 {
21048 if (atstart)
21049 {
21050 atstart = FALSE;
21051 /* Call msg_start() after eval1(), evaluating the expression
21052 * may cause a message to appear. */
21053 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021054 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021055 /* Mark the saved text as finishing the line, so that what
21056 * follows is displayed on a new line when scrolling back
21057 * at the more prompt. */
21058 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 }
21062 else if (eap->cmdidx == CMD_echo)
21063 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021064 current_copyID += COPYID_INC;
21065 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021066 if (p != NULL)
21067 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021069 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021071 if (*p != TAB && needclr)
21072 {
21073 /* remove any text still there from the command */
21074 msg_clr_eos();
21075 needclr = FALSE;
21076 }
21077 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 }
21079 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021080 {
21081#ifdef FEAT_MBYTE
21082 if (has_mbyte)
21083 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021084 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021085
21086 (void)msg_outtrans_len_attr(p, i, echo_attr);
21087 p += i - 1;
21088 }
21089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021091 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021094 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021096 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 arg = skipwhite(arg);
21098 }
21099 eap->nextcmd = check_nextcmd(arg);
21100
21101 if (eap->skip)
21102 --emsg_skip;
21103 else
21104 {
21105 /* remove text that may still be there from the command */
21106 if (needclr)
21107 msg_clr_eos();
21108 if (eap->cmdidx == CMD_echo)
21109 msg_end();
21110 }
21111}
21112
21113/*
21114 * ":echohl {name}".
21115 */
21116 void
21117ex_echohl(eap)
21118 exarg_T *eap;
21119{
21120 int id;
21121
21122 id = syn_name2id(eap->arg);
21123 if (id == 0)
21124 echo_attr = 0;
21125 else
21126 echo_attr = syn_id2attr(id);
21127}
21128
21129/*
21130 * ":execute expr1 ..." execute the result of an expression.
21131 * ":echomsg expr1 ..." Print a message
21132 * ":echoerr expr1 ..." Print an error
21133 * Each gets spaces around each argument and a newline at the end for
21134 * echo commands
21135 */
21136 void
21137ex_execute(eap)
21138 exarg_T *eap;
21139{
21140 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 int ret = OK;
21143 char_u *p;
21144 garray_T ga;
21145 int len;
21146 int save_did_emsg;
21147
21148 ga_init2(&ga, 1, 80);
21149
21150 if (eap->skip)
21151 ++emsg_skip;
21152 while (*arg != NUL && *arg != '|' && *arg != '\n')
21153 {
21154 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 {
21157 /*
21158 * Report the invalid expression unless the expression evaluation
21159 * has been cancelled due to an aborting error, an interrupt, or an
21160 * exception.
21161 */
21162 if (!aborting())
21163 EMSG2(_(e_invexpr2), p);
21164 ret = FAIL;
21165 break;
21166 }
21167
21168 if (!eap->skip)
21169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 len = (int)STRLEN(p);
21172 if (ga_grow(&ga, len + 2) == FAIL)
21173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021174 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 ret = FAIL;
21176 break;
21177 }
21178 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 ga.ga_len += len;
21182 }
21183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021184 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 arg = skipwhite(arg);
21186 }
21187
21188 if (ret != FAIL && ga.ga_data != NULL)
21189 {
21190 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021193 out_flush();
21194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 else if (eap->cmdidx == CMD_echoerr)
21196 {
21197 /* We don't want to abort following commands, restore did_emsg. */
21198 save_did_emsg = did_emsg;
21199 EMSG((char_u *)ga.ga_data);
21200 if (!force_abort)
21201 did_emsg = save_did_emsg;
21202 }
21203 else if (eap->cmdidx == CMD_execute)
21204 do_cmdline((char_u *)ga.ga_data,
21205 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21206 }
21207
21208 ga_clear(&ga);
21209
21210 if (eap->skip)
21211 --emsg_skip;
21212
21213 eap->nextcmd = check_nextcmd(arg);
21214}
21215
21216/*
21217 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21218 * "arg" points to the "&" or '+' when called, to "option" when returning.
21219 * Returns NULL when no option name found. Otherwise pointer to the char
21220 * after the option name.
21221 */
21222 static char_u *
21223find_option_end(arg, opt_flags)
21224 char_u **arg;
21225 int *opt_flags;
21226{
21227 char_u *p = *arg;
21228
21229 ++p;
21230 if (*p == 'g' && p[1] == ':')
21231 {
21232 *opt_flags = OPT_GLOBAL;
21233 p += 2;
21234 }
21235 else if (*p == 'l' && p[1] == ':')
21236 {
21237 *opt_flags = OPT_LOCAL;
21238 p += 2;
21239 }
21240 else
21241 *opt_flags = 0;
21242
21243 if (!ASCII_ISALPHA(*p))
21244 return NULL;
21245 *arg = p;
21246
21247 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21248 p += 4; /* termcap option */
21249 else
21250 while (ASCII_ISALPHA(*p))
21251 ++p;
21252 return p;
21253}
21254
21255/*
21256 * ":function"
21257 */
21258 void
21259ex_function(eap)
21260 exarg_T *eap;
21261{
21262 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021263 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 int j;
21265 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021267 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 char_u *name = NULL;
21269 char_u *p;
21270 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021271 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 garray_T newargs;
21273 garray_T newlines;
21274 int varargs = FALSE;
21275 int mustend = FALSE;
21276 int flags = 0;
21277 ufunc_T *fp;
21278 int indent;
21279 int nesting;
21280 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 dictitem_T *v;
21282 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021283 static int func_nr = 0; /* number for nameless function */
21284 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021285 hashtab_T *ht;
21286 int todo;
21287 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021288 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289
21290 /*
21291 * ":function" without argument: list functions.
21292 */
21293 if (ends_excmd(*eap->arg))
21294 {
21295 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021297 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021298 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021299 {
21300 if (!HASHITEM_EMPTY(hi))
21301 {
21302 --todo;
21303 fp = HI2UF(hi);
21304 if (!isdigit(*fp->uf_name))
21305 list_func_head(fp, FALSE);
21306 }
21307 }
21308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 eap->nextcmd = check_nextcmd(eap->arg);
21310 return;
21311 }
21312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021313 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021314 * ":function /pat": list functions matching pattern.
21315 */
21316 if (*eap->arg == '/')
21317 {
21318 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21319 if (!eap->skip)
21320 {
21321 regmatch_T regmatch;
21322
21323 c = *p;
21324 *p = NUL;
21325 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21326 *p = c;
21327 if (regmatch.regprog != NULL)
21328 {
21329 regmatch.rm_ic = p_ic;
21330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021331 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021332 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21333 {
21334 if (!HASHITEM_EMPTY(hi))
21335 {
21336 --todo;
21337 fp = HI2UF(hi);
21338 if (!isdigit(*fp->uf_name)
21339 && vim_regexec(&regmatch, fp->uf_name, 0))
21340 list_func_head(fp, FALSE);
21341 }
21342 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021343 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021344 }
21345 }
21346 if (*p == '/')
21347 ++p;
21348 eap->nextcmd = check_nextcmd(p);
21349 return;
21350 }
21351
21352 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021353 * Get the function name. There are these situations:
21354 * func normal function name
21355 * "name" == func, "fudi.fd_dict" == NULL
21356 * dict.func new dictionary entry
21357 * "name" == NULL, "fudi.fd_dict" set,
21358 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21359 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021360 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021361 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21362 * dict.func existing dict entry that's not a Funcref
21363 * "name" == NULL, "fudi.fd_dict" set,
21364 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021367 name = trans_function_name(&p, eap->skip, 0, &fudi);
21368 paren = (vim_strchr(p, '(') != NULL);
21369 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 {
21371 /*
21372 * Return on an invalid expression in braces, unless the expression
21373 * evaluation has been cancelled due to an aborting error, an
21374 * interrupt, or an exception.
21375 */
21376 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021378 if (!eap->skip && fudi.fd_newkey != NULL)
21379 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021380 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 else
21384 eap->skip = TRUE;
21385 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021386
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 /* An error in a function call during evaluation of an expression in magic
21388 * braces should not cause the function not to be defined. */
21389 saved_did_emsg = did_emsg;
21390 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391
21392 /*
21393 * ":function func" with only function name: list function.
21394 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021395 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 {
21397 if (!ends_excmd(*skipwhite(p)))
21398 {
21399 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021400 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 }
21402 eap->nextcmd = check_nextcmd(p);
21403 if (eap->nextcmd != NULL)
21404 *p = NUL;
21405 if (!eap->skip && !got_int)
21406 {
21407 fp = find_func(name);
21408 if (fp != NULL)
21409 {
21410 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021411 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021413 if (FUNCLINE(fp, j) == NULL)
21414 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 msg_putchar('\n');
21416 msg_outnum((long)(j + 1));
21417 if (j < 9)
21418 msg_putchar(' ');
21419 if (j < 99)
21420 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021421 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 out_flush(); /* show a line at a time */
21423 ui_breakcheck();
21424 }
21425 if (!got_int)
21426 {
21427 msg_putchar('\n');
21428 msg_puts((char_u *)" endfunction");
21429 }
21430 }
21431 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021432 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021434 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 }
21436
21437 /*
21438 * ":function name(arg1, arg2)" Define function.
21439 */
21440 p = skipwhite(p);
21441 if (*p != '(')
21442 {
21443 if (!eap->skip)
21444 {
21445 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021446 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 }
21448 /* attempt to continue by skipping some text */
21449 if (vim_strchr(p, '(') != NULL)
21450 p = vim_strchr(p, '(');
21451 }
21452 p = skipwhite(p + 1);
21453
21454 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21455 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21456
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021457 if (!eap->skip)
21458 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021459 /* Check the name of the function. Unless it's a dictionary function
21460 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021461 if (name != NULL)
21462 arg = name;
21463 else
21464 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021465 if (arg != NULL && (fudi.fd_di == NULL
21466 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021467 {
21468 if (*arg == K_SPECIAL)
21469 j = 3;
21470 else
21471 j = 0;
21472 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21473 : eval_isnamec(arg[j])))
21474 ++j;
21475 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021476 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021477 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021478 /* Disallow using the g: dict. */
21479 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21480 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021481 }
21482
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 /*
21484 * Isolate the arguments: "arg1, arg2, ...)"
21485 */
21486 while (*p != ')')
21487 {
21488 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21489 {
21490 varargs = TRUE;
21491 p += 3;
21492 mustend = TRUE;
21493 }
21494 else
21495 {
21496 arg = p;
21497 while (ASCII_ISALNUM(*p) || *p == '_')
21498 ++p;
21499 if (arg == p || isdigit(*arg)
21500 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21501 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21502 {
21503 if (!eap->skip)
21504 EMSG2(_("E125: Illegal argument: %s"), arg);
21505 break;
21506 }
21507 if (ga_grow(&newargs, 1) == FAIL)
21508 goto erret;
21509 c = *p;
21510 *p = NUL;
21511 arg = vim_strsave(arg);
21512 if (arg == NULL)
21513 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021514
21515 /* Check for duplicate argument name. */
21516 for (i = 0; i < newargs.ga_len; ++i)
21517 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21518 {
21519 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021520 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021521 goto erret;
21522 }
21523
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21525 *p = c;
21526 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 if (*p == ',')
21528 ++p;
21529 else
21530 mustend = TRUE;
21531 }
21532 p = skipwhite(p);
21533 if (mustend && *p != ')')
21534 {
21535 if (!eap->skip)
21536 EMSG2(_(e_invarg2), eap->arg);
21537 break;
21538 }
21539 }
21540 ++p; /* skip the ')' */
21541
Bram Moolenaare9a41262005-01-15 22:18:47 +000021542 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 for (;;)
21544 {
21545 p = skipwhite(p);
21546 if (STRNCMP(p, "range", 5) == 0)
21547 {
21548 flags |= FC_RANGE;
21549 p += 5;
21550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021551 else if (STRNCMP(p, "dict", 4) == 0)
21552 {
21553 flags |= FC_DICT;
21554 p += 4;
21555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 else if (STRNCMP(p, "abort", 5) == 0)
21557 {
21558 flags |= FC_ABORT;
21559 p += 5;
21560 }
21561 else
21562 break;
21563 }
21564
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021565 /* When there is a line break use what follows for the function body.
21566 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21567 if (*p == '\n')
21568 line_arg = p + 1;
21569 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 EMSG(_(e_trailing));
21571
21572 /*
21573 * Read the body of the function, until ":endfunction" is found.
21574 */
21575 if (KeyTyped)
21576 {
21577 /* Check if the function already exists, don't let the user type the
21578 * whole function before telling him it doesn't work! For a script we
21579 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 if (!eap->skip && !eap->forceit)
21581 {
21582 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21583 EMSG(_(e_funcdict));
21584 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021585 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021588 if (!eap->skip && did_emsg)
21589 goto erret;
21590
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 msg_putchar('\n'); /* don't overwrite the function name */
21592 cmdline_row = msg_row;
21593 }
21594
21595 indent = 2;
21596 nesting = 0;
21597 for (;;)
21598 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021599 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021600 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021601 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021602 saved_wait_return = FALSE;
21603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021605 sourcing_lnum_off = sourcing_lnum;
21606
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021607 if (line_arg != NULL)
21608 {
21609 /* Use eap->arg, split up in parts by line breaks. */
21610 theline = line_arg;
21611 p = vim_strchr(theline, '\n');
21612 if (p == NULL)
21613 line_arg += STRLEN(line_arg);
21614 else
21615 {
21616 *p = NUL;
21617 line_arg = p + 1;
21618 }
21619 }
21620 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 theline = getcmdline(':', 0L, indent);
21622 else
21623 theline = eap->getline(':', eap->cookie, indent);
21624 if (KeyTyped)
21625 lines_left = Rows - 1;
21626 if (theline == NULL)
21627 {
21628 EMSG(_("E126: Missing :endfunction"));
21629 goto erret;
21630 }
21631
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021632 /* Detect line continuation: sourcing_lnum increased more than one. */
21633 if (sourcing_lnum > sourcing_lnum_off + 1)
21634 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21635 else
21636 sourcing_lnum_off = 0;
21637
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 if (skip_until != NULL)
21639 {
21640 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21641 * don't check for ":endfunc". */
21642 if (STRCMP(theline, skip_until) == 0)
21643 {
21644 vim_free(skip_until);
21645 skip_until = NULL;
21646 }
21647 }
21648 else
21649 {
21650 /* skip ':' and blanks*/
21651 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21652 ;
21653
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021654 /* Check for "endfunction". */
21655 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021657 if (line_arg == NULL)
21658 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 break;
21660 }
21661
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021662 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 * at "end". */
21664 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21665 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021666 else if (STRNCMP(p, "if", 2) == 0
21667 || STRNCMP(p, "wh", 2) == 0
21668 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 || STRNCMP(p, "try", 3) == 0)
21670 indent += 2;
21671
21672 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021673 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021675 if (*p == '!')
21676 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 p += eval_fname_script(p);
21678 if (ASCII_ISALPHA(*p))
21679 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021680 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 if (*skipwhite(p) == '(')
21682 {
21683 ++nesting;
21684 indent += 2;
21685 }
21686 }
21687 }
21688
21689 /* Check for ":append" or ":insert". */
21690 p = skip_range(p, NULL);
21691 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21692 || (p[0] == 'i'
21693 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21694 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21695 skip_until = vim_strsave((char_u *)".");
21696
21697 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21698 arg = skipwhite(skiptowhite(p));
21699 if (arg[0] == '<' && arg[1] =='<'
21700 && ((p[0] == 'p' && p[1] == 'y'
21701 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21702 || (p[0] == 'p' && p[1] == 'e'
21703 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21704 || (p[0] == 't' && p[1] == 'c'
21705 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021706 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21707 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21709 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021710 || (p[0] == 'm' && p[1] == 'z'
21711 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 ))
21713 {
21714 /* ":python <<" continues until a dot, like ":append" */
21715 p = skipwhite(arg + 2);
21716 if (*p == NUL)
21717 skip_until = vim_strsave((char_u *)".");
21718 else
21719 skip_until = vim_strsave(p);
21720 }
21721 }
21722
21723 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021724 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021725 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021726 if (line_arg == NULL)
21727 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021729 }
21730
21731 /* Copy the line to newly allocated memory. get_one_sourceline()
21732 * allocates 250 bytes per line, this saves 80% on average. The cost
21733 * is an extra alloc/free. */
21734 p = vim_strsave(theline);
21735 if (p != NULL)
21736 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021737 if (line_arg == NULL)
21738 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021739 theline = p;
21740 }
21741
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021742 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21743
21744 /* Add NULL lines for continuation lines, so that the line count is
21745 * equal to the index in the growarray. */
21746 while (sourcing_lnum_off-- > 0)
21747 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021748
21749 /* Check for end of eap->arg. */
21750 if (line_arg != NULL && *line_arg == NUL)
21751 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 }
21753
21754 /* Don't define the function when skipping commands or when an error was
21755 * detected. */
21756 if (eap->skip || did_emsg)
21757 goto erret;
21758
21759 /*
21760 * If there are no errors, add the function
21761 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021762 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021763 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021764 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021766 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021767 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021768 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021769 goto erret;
21770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021771
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021772 fp = find_func(name);
21773 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021775 if (!eap->forceit)
21776 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021777 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021778 goto erret;
21779 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021780 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021781 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021782 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021783 name);
21784 goto erret;
21785 }
21786 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021787 ga_clear_strings(&(fp->uf_args));
21788 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021789 vim_free(name);
21790 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 }
21793 else
21794 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 char numbuf[20];
21796
21797 fp = NULL;
21798 if (fudi.fd_newkey == NULL && !eap->forceit)
21799 {
21800 EMSG(_(e_funcdict));
21801 goto erret;
21802 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021803 if (fudi.fd_di == NULL)
21804 {
21805 /* Can't add a function to a locked dictionary */
21806 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21807 goto erret;
21808 }
21809 /* Can't change an existing function if it is locked */
21810 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21811 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021812
21813 /* Give the function a sequential number. Can only be used with a
21814 * Funcref! */
21815 vim_free(name);
21816 sprintf(numbuf, "%d", ++func_nr);
21817 name = vim_strsave((char_u *)numbuf);
21818 if (name == NULL)
21819 goto erret;
21820 }
21821
21822 if (fp == NULL)
21823 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021824 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021825 {
21826 int slen, plen;
21827 char_u *scriptname;
21828
21829 /* Check that the autoload name matches the script name. */
21830 j = FAIL;
21831 if (sourcing_name != NULL)
21832 {
21833 scriptname = autoload_name(name);
21834 if (scriptname != NULL)
21835 {
21836 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021837 plen = (int)STRLEN(p);
21838 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021839 if (slen > plen && fnamecmp(p,
21840 sourcing_name + slen - plen) == 0)
21841 j = OK;
21842 vim_free(scriptname);
21843 }
21844 }
21845 if (j == FAIL)
21846 {
21847 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21848 goto erret;
21849 }
21850 }
21851
21852 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 if (fp == NULL)
21854 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021855
21856 if (fudi.fd_dict != NULL)
21857 {
21858 if (fudi.fd_di == NULL)
21859 {
21860 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021861 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021862 if (fudi.fd_di == NULL)
21863 {
21864 vim_free(fp);
21865 goto erret;
21866 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021867 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21868 {
21869 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021870 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021871 goto erret;
21872 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021873 }
21874 else
21875 /* overwrite existing dict entry */
21876 clear_tv(&fudi.fd_di->di_tv);
21877 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021878 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021879 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021880 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021881
21882 /* behave like "dict" was used */
21883 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021884 }
21885
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021887 STRCPY(fp->uf_name, name);
21888 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 fp->uf_args = newargs;
21891 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021892#ifdef FEAT_PROFILE
21893 fp->uf_tml_count = NULL;
21894 fp->uf_tml_total = NULL;
21895 fp->uf_tml_self = NULL;
21896 fp->uf_profiling = FALSE;
21897 if (prof_def_func())
21898 func_do_profile(fp);
21899#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021900 fp->uf_varargs = varargs;
21901 fp->uf_flags = flags;
21902 fp->uf_calls = 0;
21903 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021904 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905
21906erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 ga_clear_strings(&newargs);
21908 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021909ret_free:
21910 vim_free(skip_until);
21911 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021914 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915}
21916
21917/*
21918 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021919 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021922 * TFN_INT: internal function name OK
21923 * TFN_QUIET: be quiet
21924 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 * Advances "pp" to just after the function name (if no error).
21926 */
21927 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021928trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 char_u **pp;
21930 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021931 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021934 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 char_u *start;
21936 char_u *end;
21937 int lead;
21938 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021941
21942 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021943 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021945
21946 /* Check for hard coded <SNR>: already translated function ID (from a user
21947 * command). */
21948 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21949 && (*pp)[2] == (int)KE_SNR)
21950 {
21951 *pp += 3;
21952 len = get_id_len(pp) + 3;
21953 return vim_strnsave(start, len);
21954 }
21955
21956 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21957 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021959 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021961
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021962 /* Note that TFN_ flags use the same values as GLV_ flags. */
21963 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021964 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021965 if (end == start)
21966 {
21967 if (!skip)
21968 EMSG(_("E129: Function name required"));
21969 goto theend;
21970 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021971 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021972 {
21973 /*
21974 * Report an invalid expression in braces, unless the expression
21975 * evaluation has been cancelled due to an aborting error, an
21976 * interrupt, or an exception.
21977 */
21978 if (!aborting())
21979 {
21980 if (end != NULL)
21981 EMSG2(_(e_invarg2), start);
21982 }
21983 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021984 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021985 goto theend;
21986 }
21987
21988 if (lv.ll_tv != NULL)
21989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021990 if (fdp != NULL)
21991 {
21992 fdp->fd_dict = lv.ll_dict;
21993 fdp->fd_newkey = lv.ll_newkey;
21994 lv.ll_newkey = NULL;
21995 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021997 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21998 {
21999 name = vim_strsave(lv.ll_tv->vval.v_string);
22000 *pp = end;
22001 }
22002 else
22003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022004 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22005 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022006 EMSG(_(e_funcref));
22007 else
22008 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022009 name = NULL;
22010 }
22011 goto theend;
22012 }
22013
22014 if (lv.ll_name == NULL)
22015 {
22016 /* Error found, but continue after the function name. */
22017 *pp = end;
22018 goto theend;
22019 }
22020
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022021 /* Check if the name is a Funcref. If so, use the value. */
22022 if (lv.ll_exp_name != NULL)
22023 {
22024 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022025 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022026 if (name == lv.ll_exp_name)
22027 name = NULL;
22028 }
22029 else
22030 {
22031 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022032 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022033 if (name == *pp)
22034 name = NULL;
22035 }
22036 if (name != NULL)
22037 {
22038 name = vim_strsave(name);
22039 *pp = end;
22040 goto theend;
22041 }
22042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022043 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022045 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022046 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22047 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22048 {
22049 /* When there was "s:" already or the name expanded to get a
22050 * leading "s:" then remove it. */
22051 lv.ll_name += 2;
22052 len -= 2;
22053 lead = 2;
22054 }
22055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022056 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022057 {
22058 if (lead == 2) /* skip over "s:" */
22059 lv.ll_name += 2;
22060 len = (int)(end - lv.ll_name);
22061 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022062
22063 /*
22064 * Copy the function name to allocated memory.
22065 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22066 * Accept <SNR>123_name() outside a script.
22067 */
22068 if (skip)
22069 lead = 0; /* do nothing */
22070 else if (lead > 0)
22071 {
22072 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022073 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22074 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022075 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022076 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022077 if (current_SID <= 0)
22078 {
22079 EMSG(_(e_usingsid));
22080 goto theend;
22081 }
22082 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22083 lead += (int)STRLEN(sid_buf);
22084 }
22085 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022086 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022087 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022088 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022089 goto theend;
22090 }
22091 name = alloc((unsigned)(len + lead + 1));
22092 if (name != NULL)
22093 {
22094 if (lead > 0)
22095 {
22096 name[0] = K_SPECIAL;
22097 name[1] = KS_EXTRA;
22098 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022099 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022100 STRCPY(name + 3, sid_buf);
22101 }
22102 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22103 name[len + lead] = NUL;
22104 }
22105 *pp = end;
22106
22107theend:
22108 clear_lval(&lv);
22109 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110}
22111
22112/*
22113 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22114 * Return 2 if "p" starts with "s:".
22115 * Return 0 otherwise.
22116 */
22117 static int
22118eval_fname_script(p)
22119 char_u *p;
22120{
22121 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22122 || STRNICMP(p + 1, "SNR>", 4) == 0))
22123 return 5;
22124 if (p[0] == 's' && p[1] == ':')
22125 return 2;
22126 return 0;
22127}
22128
22129/*
22130 * Return TRUE if "p" starts with "<SID>" or "s:".
22131 * Only works if eval_fname_script() returned non-zero for "p"!
22132 */
22133 static int
22134eval_fname_sid(p)
22135 char_u *p;
22136{
22137 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22138}
22139
22140/*
22141 * List the head of the function: "name(arg1, arg2)".
22142 */
22143 static void
22144list_func_head(fp, indent)
22145 ufunc_T *fp;
22146 int indent;
22147{
22148 int j;
22149
22150 msg_start();
22151 if (indent)
22152 MSG_PUTS(" ");
22153 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022154 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 {
22156 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022157 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
22159 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022160 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022162 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 {
22164 if (j)
22165 MSG_PUTS(", ");
22166 msg_puts(FUNCARG(fp, j));
22167 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022168 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 {
22170 if (j)
22171 MSG_PUTS(", ");
22172 MSG_PUTS("...");
22173 }
22174 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022175 if (fp->uf_flags & FC_ABORT)
22176 MSG_PUTS(" abort");
22177 if (fp->uf_flags & FC_RANGE)
22178 MSG_PUTS(" range");
22179 if (fp->uf_flags & FC_DICT)
22180 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022181 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022182 if (p_verbose > 0)
22183 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184}
22185
22186/*
22187 * Find a function by name, return pointer to it in ufuncs.
22188 * Return NULL for unknown function.
22189 */
22190 static ufunc_T *
22191find_func(name)
22192 char_u *name;
22193{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022194 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022196 hi = hash_find(&func_hashtab, name);
22197 if (!HASHITEM_EMPTY(hi))
22198 return HI2UF(hi);
22199 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200}
22201
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022202#if defined(EXITFREE) || defined(PROTO)
22203 void
22204free_all_functions()
22205{
22206 hashitem_T *hi;
22207
22208 /* Need to start all over every time, because func_free() may change the
22209 * hash table. */
22210 while (func_hashtab.ht_used > 0)
22211 for (hi = func_hashtab.ht_array; ; ++hi)
22212 if (!HASHITEM_EMPTY(hi))
22213 {
22214 func_free(HI2UF(hi));
22215 break;
22216 }
22217}
22218#endif
22219
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022220 int
22221translated_function_exists(name)
22222 char_u *name;
22223{
22224 if (builtin_function(name))
22225 return find_internal_func(name) >= 0;
22226 return find_func(name) != NULL;
22227}
22228
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022229/*
22230 * Return TRUE if a function "name" exists.
22231 */
22232 static int
22233function_exists(name)
22234 char_u *name;
22235{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022236 char_u *nm = name;
22237 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 int n = FALSE;
22239
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022240 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22241 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022242 nm = skipwhite(nm);
22243
22244 /* Only accept "funcname", "funcname ", "funcname (..." and
22245 * "funcname(...", not "funcname!...". */
22246 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022247 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022248 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022249 return n;
22250}
22251
Bram Moolenaara1544c02013-05-30 12:35:52 +020022252 char_u *
22253get_expanded_name(name, check)
22254 char_u *name;
22255 int check;
22256{
22257 char_u *nm = name;
22258 char_u *p;
22259
22260 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22261
22262 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022263 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022264 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022265
Bram Moolenaara1544c02013-05-30 12:35:52 +020022266 vim_free(p);
22267 return NULL;
22268}
22269
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022270/*
22271 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022272 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022273 */
22274 static int
22275builtin_function(name)
22276 char_u *name;
22277{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022278 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22279 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022280}
22281
Bram Moolenaar05159a02005-02-26 23:04:13 +000022282#if defined(FEAT_PROFILE) || defined(PROTO)
22283/*
22284 * Start profiling function "fp".
22285 */
22286 static void
22287func_do_profile(fp)
22288 ufunc_T *fp;
22289{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022290 int len = fp->uf_lines.ga_len;
22291
22292 if (len == 0)
22293 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022294 fp->uf_tm_count = 0;
22295 profile_zero(&fp->uf_tm_self);
22296 profile_zero(&fp->uf_tm_total);
22297 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022298 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022299 if (fp->uf_tml_total == NULL)
22300 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022301 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022302 if (fp->uf_tml_self == NULL)
22303 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022304 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022305 fp->uf_tml_idx = -1;
22306 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22307 || fp->uf_tml_self == NULL)
22308 return; /* out of memory */
22309
22310 fp->uf_profiling = TRUE;
22311}
22312
22313/*
22314 * Dump the profiling results for all functions in file "fd".
22315 */
22316 void
22317func_dump_profile(fd)
22318 FILE *fd;
22319{
22320 hashitem_T *hi;
22321 int todo;
22322 ufunc_T *fp;
22323 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022324 ufunc_T **sorttab;
22325 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022326
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022327 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022328 if (todo == 0)
22329 return; /* nothing to dump */
22330
Bram Moolenaar73830342005-02-28 22:48:19 +000022331 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22332
Bram Moolenaar05159a02005-02-26 23:04:13 +000022333 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22334 {
22335 if (!HASHITEM_EMPTY(hi))
22336 {
22337 --todo;
22338 fp = HI2UF(hi);
22339 if (fp->uf_profiling)
22340 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022341 if (sorttab != NULL)
22342 sorttab[st_len++] = fp;
22343
Bram Moolenaar05159a02005-02-26 23:04:13 +000022344 if (fp->uf_name[0] == K_SPECIAL)
22345 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22346 else
22347 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22348 if (fp->uf_tm_count == 1)
22349 fprintf(fd, "Called 1 time\n");
22350 else
22351 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22352 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22353 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22354 fprintf(fd, "\n");
22355 fprintf(fd, "count total (s) self (s)\n");
22356
22357 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22358 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022359 if (FUNCLINE(fp, i) == NULL)
22360 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022361 prof_func_line(fd, fp->uf_tml_count[i],
22362 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022363 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22364 }
22365 fprintf(fd, "\n");
22366 }
22367 }
22368 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022369
22370 if (sorttab != NULL && st_len > 0)
22371 {
22372 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22373 prof_total_cmp);
22374 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22375 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22376 prof_self_cmp);
22377 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22378 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022379
22380 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022381}
Bram Moolenaar73830342005-02-28 22:48:19 +000022382
22383 static void
22384prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22385 FILE *fd;
22386 ufunc_T **sorttab;
22387 int st_len;
22388 char *title;
22389 int prefer_self; /* when equal print only self time */
22390{
22391 int i;
22392 ufunc_T *fp;
22393
22394 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22395 fprintf(fd, "count total (s) self (s) function\n");
22396 for (i = 0; i < 20 && i < st_len; ++i)
22397 {
22398 fp = sorttab[i];
22399 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22400 prefer_self);
22401 if (fp->uf_name[0] == K_SPECIAL)
22402 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22403 else
22404 fprintf(fd, " %s()\n", fp->uf_name);
22405 }
22406 fprintf(fd, "\n");
22407}
22408
22409/*
22410 * Print the count and times for one function or function line.
22411 */
22412 static void
22413prof_func_line(fd, count, total, self, prefer_self)
22414 FILE *fd;
22415 int count;
22416 proftime_T *total;
22417 proftime_T *self;
22418 int prefer_self; /* when equal print only self time */
22419{
22420 if (count > 0)
22421 {
22422 fprintf(fd, "%5d ", count);
22423 if (prefer_self && profile_equal(total, self))
22424 fprintf(fd, " ");
22425 else
22426 fprintf(fd, "%s ", profile_msg(total));
22427 if (!prefer_self && profile_equal(total, self))
22428 fprintf(fd, " ");
22429 else
22430 fprintf(fd, "%s ", profile_msg(self));
22431 }
22432 else
22433 fprintf(fd, " ");
22434}
22435
22436/*
22437 * Compare function for total time sorting.
22438 */
22439 static int
22440#ifdef __BORLANDC__
22441_RTLENTRYF
22442#endif
22443prof_total_cmp(s1, s2)
22444 const void *s1;
22445 const void *s2;
22446{
22447 ufunc_T *p1, *p2;
22448
22449 p1 = *(ufunc_T **)s1;
22450 p2 = *(ufunc_T **)s2;
22451 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22452}
22453
22454/*
22455 * Compare function for self time sorting.
22456 */
22457 static int
22458#ifdef __BORLANDC__
22459_RTLENTRYF
22460#endif
22461prof_self_cmp(s1, s2)
22462 const void *s1;
22463 const void *s2;
22464{
22465 ufunc_T *p1, *p2;
22466
22467 p1 = *(ufunc_T **)s1;
22468 p2 = *(ufunc_T **)s2;
22469 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22470}
22471
Bram Moolenaar05159a02005-02-26 23:04:13 +000022472#endif
22473
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022474/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022475 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022476 * Return TRUE if a package was loaded.
22477 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022478 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022479script_autoload(name, reload)
22480 char_u *name;
22481 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022482{
22483 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022484 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022485 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022486 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022488 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022489 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022490 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022491 return FALSE;
22492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022493 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022495 /* Find the name in the list of previously loaded package names. Skip
22496 * "autoload/", it's always the same. */
22497 for (i = 0; i < ga_loaded.ga_len; ++i)
22498 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22499 break;
22500 if (!reload && i < ga_loaded.ga_len)
22501 ret = FALSE; /* was loaded already */
22502 else
22503 {
22504 /* Remember the name if it wasn't loaded already. */
22505 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22506 {
22507 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22508 tofree = NULL;
22509 }
22510
22511 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022512 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022513 ret = TRUE;
22514 }
22515
22516 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022517 return ret;
22518}
22519
22520/*
22521 * Return the autoload script name for a function or variable name.
22522 * Returns NULL when out of memory.
22523 */
22524 static char_u *
22525autoload_name(name)
22526 char_u *name;
22527{
22528 char_u *p;
22529 char_u *scriptname;
22530
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022531 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022532 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22533 if (scriptname == NULL)
22534 return FALSE;
22535 STRCPY(scriptname, "autoload/");
22536 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022537 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022538 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022539 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022540 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022541 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022542}
22543
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22545
22546/*
22547 * Function given to ExpandGeneric() to obtain the list of user defined
22548 * function names.
22549 */
22550 char_u *
22551get_user_func_name(xp, idx)
22552 expand_T *xp;
22553 int idx;
22554{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022555 static long_u done;
22556 static hashitem_T *hi;
22557 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558
22559 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022561 done = 0;
22562 hi = func_hashtab.ht_array;
22563 }
22564 if (done < func_hashtab.ht_used)
22565 {
22566 if (done++ > 0)
22567 ++hi;
22568 while (HASHITEM_EMPTY(hi))
22569 ++hi;
22570 fp = HI2UF(hi);
22571
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022572 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022573 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022574
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022575 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22576 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
22578 cat_func_name(IObuff, fp);
22579 if (xp->xp_context != EXPAND_USER_FUNC)
22580 {
22581 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022582 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 STRCAT(IObuff, ")");
22584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 return IObuff;
22586 }
22587 return NULL;
22588}
22589
22590#endif /* FEAT_CMDL_COMPL */
22591
22592/*
22593 * Copy the function name of "fp" to buffer "buf".
22594 * "buf" must be able to hold the function name plus three bytes.
22595 * Takes care of script-local function names.
22596 */
22597 static void
22598cat_func_name(buf, fp)
22599 char_u *buf;
22600 ufunc_T *fp;
22601{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022602 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 {
22604 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022605 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 }
22607 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022608 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609}
22610
22611/*
22612 * ":delfunction {name}"
22613 */
22614 void
22615ex_delfunction(eap)
22616 exarg_T *eap;
22617{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022618 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 char_u *p;
22620 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622
22623 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022624 name = trans_function_name(&p, eap->skip, 0, &fudi);
22625 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 {
22628 if (fudi.fd_dict != NULL && !eap->skip)
22629 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 if (!ends_excmd(*skipwhite(p)))
22633 {
22634 vim_free(name);
22635 EMSG(_(e_trailing));
22636 return;
22637 }
22638 eap->nextcmd = check_nextcmd(p);
22639 if (eap->nextcmd != NULL)
22640 *p = NUL;
22641
22642 if (!eap->skip)
22643 fp = find_func(name);
22644 vim_free(name);
22645
22646 if (!eap->skip)
22647 {
22648 if (fp == NULL)
22649 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022650 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 return;
22652 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022653 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
22655 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22656 return;
22657 }
22658
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022659 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661 /* Delete the dict item that refers to the function, it will
22662 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022663 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 else
22666 func_free(fp);
22667 }
22668}
22669
22670/*
22671 * Free a function and remove it from the list of functions.
22672 */
22673 static void
22674func_free(fp)
22675 ufunc_T *fp;
22676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022677 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022678
22679 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022680 ga_clear_strings(&(fp->uf_args));
22681 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022682#ifdef FEAT_PROFILE
22683 vim_free(fp->uf_tml_count);
22684 vim_free(fp->uf_tml_total);
22685 vim_free(fp->uf_tml_self);
22686#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022687
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022688 /* remove the function from the function hashtable */
22689 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22690 if (HASHITEM_EMPTY(hi))
22691 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022692 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022693 hash_remove(&func_hashtab, hi);
22694
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022695 vim_free(fp);
22696}
22697
22698/*
22699 * Unreference a Function: decrement the reference count and free it when it
22700 * becomes zero. Only for numbered functions.
22701 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022702 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022703func_unref(name)
22704 char_u *name;
22705{
22706 ufunc_T *fp;
22707
22708 if (name != NULL && isdigit(*name))
22709 {
22710 fp = find_func(name);
22711 if (fp == NULL)
22712 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022713 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022714 {
22715 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022716 * when "uf_calls" becomes zero. */
22717 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022718 func_free(fp);
22719 }
22720 }
22721}
22722
22723/*
22724 * Count a reference to a Function.
22725 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022726 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022727func_ref(name)
22728 char_u *name;
22729{
22730 ufunc_T *fp;
22731
22732 if (name != NULL && isdigit(*name))
22733 {
22734 fp = find_func(name);
22735 if (fp == NULL)
22736 EMSG2(_(e_intern2), "func_ref()");
22737 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022738 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 }
22740}
22741
22742/*
22743 * Call a user function.
22744 */
22745 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022746call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 ufunc_T *fp; /* pointer to function */
22748 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 typval_T *argvars; /* arguments */
22750 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 linenr_T firstline; /* first line of range */
22752 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
Bram Moolenaar33570922005-01-25 22:26:29 +000022755 char_u *save_sourcing_name;
22756 linenr_T save_sourcing_lnum;
22757 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 int save_did_emsg;
22760 static int depth = 0;
22761 dictitem_T *v;
22762 int fixvar_idx = 0; /* index in fixvar[] */
22763 int i;
22764 int ai;
22765 char_u numbuf[NUMBUFLEN];
22766 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022767#ifdef FEAT_PROFILE
22768 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022769 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771
22772 /* If depth of calling is getting too high, don't execute the function */
22773 if (depth >= p_mfd)
22774 {
22775 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022776 rettv->v_type = VAR_NUMBER;
22777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 return;
22779 }
22780 ++depth;
22781
22782 line_breakcheck(); /* check for CTRL-C hit */
22783
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022784 fc = (funccall_T *)alloc(sizeof(funccall_T));
22785 fc->caller = current_funccal;
22786 current_funccal = fc;
22787 fc->func = fp;
22788 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022789 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022790 fc->linenr = 0;
22791 fc->returned = FALSE;
22792 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022794 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22795 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022798 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22800 * each argument variable and saves a lot of time.
22801 */
22802 /*
22803 * Init l: variables.
22804 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022805 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022806 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022807 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022808 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22809 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022810 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022811 name = v->di_key;
22812 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022813 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022814 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022815 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022816 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022817 v->di_tv.vval.v_dict = selfdict;
22818 ++selfdict->dv_refcount;
22819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022820
Bram Moolenaar33570922005-01-25 22:26:29 +000022821 /*
22822 * Init a: variables.
22823 * Set a:0 to "argcount".
22824 * Set a:000 to a list with room for the "..." arguments.
22825 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022826 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022827 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022828 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022829 /* Use "name" to avoid a warning from some compiler that checks the
22830 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022831 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022832 name = v->di_key;
22833 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022835 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022837 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022838 v->di_tv.vval.v_list = &fc->l_varlist;
22839 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22840 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22841 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022842
22843 /*
22844 * Set a:firstline to "firstline" and a:lastline to "lastline".
22845 * Set a:name to named arguments.
22846 * Set a:N to the "..." arguments.
22847 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022848 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022850 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 (varnumber_T)lastline);
22852 for (i = 0; i < argcount; ++i)
22853 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022854 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 if (ai < 0)
22856 /* named argument a:name */
22857 name = FUNCARG(fp, i);
22858 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022859 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022860 /* "..." argument a:1, a:2, etc. */
22861 sprintf((char *)numbuf, "%d", ai + 1);
22862 name = numbuf;
22863 }
22864 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22865 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022866 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22868 }
22869 else
22870 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022871 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22872 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 if (v == NULL)
22874 break;
22875 v->di_flags = DI_FLAGS_RO;
22876 }
22877 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022878 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022880 /* Note: the values are copied directly to avoid alloc/free.
22881 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022883 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022884
22885 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22886 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022887 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22888 fc->l_listitems[ai].li_tv = argvars[i];
22889 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022890 }
22891 }
22892
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 /* Don't redraw while executing the function. */
22894 ++RedrawingDisabled;
22895 save_sourcing_name = sourcing_name;
22896 save_sourcing_lnum = sourcing_lnum;
22897 sourcing_lnum = 1;
22898 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022899 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 if (sourcing_name != NULL)
22901 {
22902 if (save_sourcing_name != NULL
22903 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22904 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22905 else
22906 STRCPY(sourcing_name, "function ");
22907 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22908
22909 if (p_verbose >= 12)
22910 {
22911 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022912 verbose_enter_scroll();
22913
Bram Moolenaar555b2802005-05-19 21:08:39 +000022914 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 if (p_verbose >= 14)
22916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022918 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022919 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022920 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921
22922 msg_puts((char_u *)"(");
22923 for (i = 0; i < argcount; ++i)
22924 {
22925 if (i > 0)
22926 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022927 if (argvars[i].v_type == VAR_NUMBER)
22928 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 else
22930 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022931 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22932 if (s != NULL)
22933 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022934 if (vim_strsize(s) > MSG_BUF_CLEN)
22935 {
22936 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22937 s = buf;
22938 }
22939 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022940 vim_free(tofree);
22941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 }
22943 }
22944 msg_puts((char_u *)")");
22945 }
22946 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022947
22948 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 --no_wait_return;
22950 }
22951 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022952#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022953 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022954 {
22955 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22956 func_do_profile(fp);
22957 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022958 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022959 {
22960 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022961 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022962 profile_zero(&fp->uf_tm_children);
22963 }
22964 script_prof_save(&wait_start);
22965 }
22966#endif
22967
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022969 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 save_did_emsg = did_emsg;
22971 did_emsg = FALSE;
22972
22973 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022974 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22976
22977 --RedrawingDisabled;
22978
22979 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022980 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022982 clear_tv(rettv);
22983 rettv->v_type = VAR_NUMBER;
22984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 }
22986
Bram Moolenaar05159a02005-02-26 23:04:13 +000022987#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022988 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022989 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022990 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022991 profile_end(&call_start);
22992 profile_sub_wait(&wait_start, &call_start);
22993 profile_add(&fp->uf_tm_total, &call_start);
22994 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022995 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022996 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022997 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22998 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022999 }
23000 }
23001#endif
23002
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 /* when being verbose, mention the return value */
23004 if (p_verbose >= 12)
23005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023007 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023010 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023011 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023012 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023013 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023016 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023017 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023018 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023019 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023020
Bram Moolenaar555b2802005-05-19 21:08:39 +000023021 /* The value may be very long. Skip the middle part, so that we
23022 * have some idea how it starts and ends. smsg() would always
23023 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023024 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023025 if (s != NULL)
23026 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023027 if (vim_strsize(s) > MSG_BUF_CLEN)
23028 {
23029 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23030 s = buf;
23031 }
23032 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023033 vim_free(tofree);
23034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 }
23036 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023037
23038 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 --no_wait_return;
23040 }
23041
23042 vim_free(sourcing_name);
23043 sourcing_name = save_sourcing_name;
23044 sourcing_lnum = save_sourcing_lnum;
23045 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023046#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023047 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023048 script_prof_restore(&wait_start);
23049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050
23051 if (p_verbose >= 12 && sourcing_name != NULL)
23052 {
23053 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023054 verbose_enter_scroll();
23055
Bram Moolenaar555b2802005-05-19 21:08:39 +000023056 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023058
23059 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 --no_wait_return;
23061 }
23062
23063 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023064 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023066
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023067 /* If the a:000 list and the l: and a: dicts are not referenced we can
23068 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023069 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23070 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23071 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23072 {
23073 free_funccal(fc, FALSE);
23074 }
23075 else
23076 {
23077 hashitem_T *hi;
23078 listitem_T *li;
23079 int todo;
23080
23081 /* "fc" is still in use. This can happen when returning "a:000" or
23082 * assigning "l:" to a global variable.
23083 * Link "fc" in the list for garbage collection later. */
23084 fc->caller = previous_funccal;
23085 previous_funccal = fc;
23086
23087 /* Make a copy of the a: variables, since we didn't do that above. */
23088 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23089 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23090 {
23091 if (!HASHITEM_EMPTY(hi))
23092 {
23093 --todo;
23094 v = HI2DI(hi);
23095 copy_tv(&v->di_tv, &v->di_tv);
23096 }
23097 }
23098
23099 /* Make a copy of the a:000 items, since we didn't do that above. */
23100 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23101 copy_tv(&li->li_tv, &li->li_tv);
23102 }
23103}
23104
23105/*
23106 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023107 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023108 */
23109 static int
23110can_free_funccal(fc, copyID)
23111 funccall_T *fc;
23112 int copyID;
23113{
23114 return (fc->l_varlist.lv_copyID != copyID
23115 && fc->l_vars.dv_copyID != copyID
23116 && fc->l_avars.dv_copyID != copyID);
23117}
23118
23119/*
23120 * Free "fc" and what it contains.
23121 */
23122 static void
23123free_funccal(fc, free_val)
23124 funccall_T *fc;
23125 int free_val; /* a: vars were allocated */
23126{
23127 listitem_T *li;
23128
23129 /* The a: variables typevals may not have been allocated, only free the
23130 * allocated variables. */
23131 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23132
23133 /* free all l: variables */
23134 vars_clear(&fc->l_vars.dv_hashtab);
23135
23136 /* Free the a:000 variables if they were allocated. */
23137 if (free_val)
23138 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23139 clear_tv(&li->li_tv);
23140
23141 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142}
23143
23144/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 * Add a number variable "name" to dict "dp" with value "nr".
23146 */
23147 static void
23148add_nr_var(dp, v, name, nr)
23149 dict_T *dp;
23150 dictitem_T *v;
23151 char *name;
23152 varnumber_T nr;
23153{
23154 STRCPY(v->di_key, name);
23155 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23156 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23157 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023158 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023159 v->di_tv.vval.v_number = nr;
23160}
23161
23162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 * ":return [expr]"
23164 */
23165 void
23166ex_return(eap)
23167 exarg_T *eap;
23168{
23169 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 int returning = FALSE;
23172
23173 if (current_funccal == NULL)
23174 {
23175 EMSG(_("E133: :return not inside a function"));
23176 return;
23177 }
23178
23179 if (eap->skip)
23180 ++emsg_skip;
23181
23182 eap->nextcmd = NULL;
23183 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 {
23186 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023189 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 }
23191 /* It's safer to return also on error. */
23192 else if (!eap->skip)
23193 {
23194 /*
23195 * Return unless the expression evaluation has been cancelled due to an
23196 * aborting error, an interrupt, or an exception.
23197 */
23198 if (!aborting())
23199 returning = do_return(eap, FALSE, TRUE, NULL);
23200 }
23201
23202 /* When skipping or the return gets pending, advance to the next command
23203 * in this line (!returning). Otherwise, ignore the rest of the line.
23204 * Following lines will be ignored by get_func_line(). */
23205 if (returning)
23206 eap->nextcmd = NULL;
23207 else if (eap->nextcmd == NULL) /* no argument */
23208 eap->nextcmd = check_nextcmd(arg);
23209
23210 if (eap->skip)
23211 --emsg_skip;
23212}
23213
23214/*
23215 * Return from a function. Possibly makes the return pending. Also called
23216 * for a pending return at the ":endtry" or after returning from an extra
23217 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023218 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023219 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 * FALSE when the return gets pending.
23221 */
23222 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023223do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 exarg_T *eap;
23225 int reanimate;
23226 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023227 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228{
23229 int idx;
23230 struct condstack *cstack = eap->cstack;
23231
23232 if (reanimate)
23233 /* Undo the return. */
23234 current_funccal->returned = FALSE;
23235
23236 /*
23237 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23238 * not in its finally clause (which then is to be executed next) is found.
23239 * In this case, make the ":return" pending for execution at the ":endtry".
23240 * Otherwise, return normally.
23241 */
23242 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23243 if (idx >= 0)
23244 {
23245 cstack->cs_pending[idx] = CSTP_RETURN;
23246
23247 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023248 /* A pending return again gets pending. "rettv" points to an
23249 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023251 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 else
23253 {
23254 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023255 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023257 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023259 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 {
23261 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023262 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 else
23265 EMSG(_(e_outofmem));
23266 }
23267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023268 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269
23270 if (reanimate)
23271 {
23272 /* The pending return value could be overwritten by a ":return"
23273 * without argument in a finally clause; reset the default
23274 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023275 current_funccal->rettv->v_type = VAR_NUMBER;
23276 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 }
23278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023279 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 }
23281 else
23282 {
23283 current_funccal->returned = TRUE;
23284
23285 /* If the return is carried out now, store the return value. For
23286 * a return immediately after reanimation, the value is already
23287 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023288 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023290 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023291 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023293 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 }
23295 }
23296
23297 return idx < 0;
23298}
23299
23300/*
23301 * Free the variable with a pending return value.
23302 */
23303 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023304discard_pending_return(rettv)
23305 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306{
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308}
23309
23310/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023311 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 * is an allocated string. Used by report_pending() for verbose messages.
23313 */
23314 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023315get_return_cmd(rettv)
23316 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023318 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023319 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023320 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023322 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023323 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023324 if (s == NULL)
23325 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023326
23327 STRCPY(IObuff, ":return ");
23328 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23329 if (STRLEN(s) + 8 >= IOSIZE)
23330 STRCPY(IObuff + IOSIZE - 4, "...");
23331 vim_free(tofree);
23332 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333}
23334
23335/*
23336 * Get next function line.
23337 * Called by do_cmdline() to get the next line.
23338 * Returns allocated string, or NULL for end of function.
23339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 char_u *
23341get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023342 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023344 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345{
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 ufunc_T *fp = fcp->func;
23348 char_u *retval;
23349 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350
23351 /* If breakpoints have been added/deleted need to check for it. */
23352 if (fcp->dbg_tick != debug_tick)
23353 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023354 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 sourcing_lnum);
23356 fcp->dbg_tick = debug_tick;
23357 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023360 func_line_end(cookie);
23361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362
Bram Moolenaar05159a02005-02-26 23:04:13 +000023363 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023364 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23365 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 retval = NULL;
23367 else
23368 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023369 /* Skip NULL lines (continuation lines). */
23370 while (fcp->linenr < gap->ga_len
23371 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23372 ++fcp->linenr;
23373 if (fcp->linenr >= gap->ga_len)
23374 retval = NULL;
23375 else
23376 {
23377 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23378 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023379#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023380 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023381 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023382#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 }
23385
23386 /* Did we encounter a breakpoint? */
23387 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023389 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023391 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 sourcing_lnum);
23393 fcp->dbg_tick = debug_tick;
23394 }
23395
23396 return retval;
23397}
23398
Bram Moolenaar05159a02005-02-26 23:04:13 +000023399#if defined(FEAT_PROFILE) || defined(PROTO)
23400/*
23401 * Called when starting to read a function line.
23402 * "sourcing_lnum" must be correct!
23403 * When skipping lines it may not actually be executed, but we won't find out
23404 * until later and we need to store the time now.
23405 */
23406 void
23407func_line_start(cookie)
23408 void *cookie;
23409{
23410 funccall_T *fcp = (funccall_T *)cookie;
23411 ufunc_T *fp = fcp->func;
23412
23413 if (fp->uf_profiling && sourcing_lnum >= 1
23414 && sourcing_lnum <= fp->uf_lines.ga_len)
23415 {
23416 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023417 /* Skip continuation lines. */
23418 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23419 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023420 fp->uf_tml_execed = FALSE;
23421 profile_start(&fp->uf_tml_start);
23422 profile_zero(&fp->uf_tml_children);
23423 profile_get_wait(&fp->uf_tml_wait);
23424 }
23425}
23426
23427/*
23428 * Called when actually executing a function line.
23429 */
23430 void
23431func_line_exec(cookie)
23432 void *cookie;
23433{
23434 funccall_T *fcp = (funccall_T *)cookie;
23435 ufunc_T *fp = fcp->func;
23436
23437 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23438 fp->uf_tml_execed = TRUE;
23439}
23440
23441/*
23442 * Called when done with a function line.
23443 */
23444 void
23445func_line_end(cookie)
23446 void *cookie;
23447{
23448 funccall_T *fcp = (funccall_T *)cookie;
23449 ufunc_T *fp = fcp->func;
23450
23451 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23452 {
23453 if (fp->uf_tml_execed)
23454 {
23455 ++fp->uf_tml_count[fp->uf_tml_idx];
23456 profile_end(&fp->uf_tml_start);
23457 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023458 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023459 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23460 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023461 }
23462 fp->uf_tml_idx = -1;
23463 }
23464}
23465#endif
23466
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467/*
23468 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023469 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 */
23471 int
23472func_has_ended(cookie)
23473 void *cookie;
23474{
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476
23477 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23478 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023479 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 || fcp->returned);
23481}
23482
23483/*
23484 * return TRUE if cookie indicates a function which "abort"s on errors.
23485 */
23486 int
23487func_has_abort(cookie)
23488 void *cookie;
23489{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023490 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491}
23492
23493#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23494typedef enum
23495{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023496 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23497 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23498 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499} var_flavour_T;
23500
23501static var_flavour_T var_flavour __ARGS((char_u *varname));
23502
23503 static var_flavour_T
23504var_flavour(varname)
23505 char_u *varname;
23506{
23507 char_u *p = varname;
23508
23509 if (ASCII_ISUPPER(*p))
23510 {
23511 while (*(++p))
23512 if (ASCII_ISLOWER(*p))
23513 return VAR_FLAVOUR_SESSION;
23514 return VAR_FLAVOUR_VIMINFO;
23515 }
23516 else
23517 return VAR_FLAVOUR_DEFAULT;
23518}
23519#endif
23520
23521#if defined(FEAT_VIMINFO) || defined(PROTO)
23522/*
23523 * Restore global vars that start with a capital from the viminfo file
23524 */
23525 int
23526read_viminfo_varlist(virp, writing)
23527 vir_T *virp;
23528 int writing;
23529{
23530 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023531 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023532 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533
23534 if (!writing && (find_viminfo_parameter('!') != NULL))
23535 {
23536 tab = vim_strchr(virp->vir_line + 1, '\t');
23537 if (tab != NULL)
23538 {
23539 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023540 switch (*tab)
23541 {
23542 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023543#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023544 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023545#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023546 case 'D': type = VAR_DICT; break;
23547 case 'L': type = VAR_LIST; break;
23548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549
23550 tab = vim_strchr(tab, '\t');
23551 if (tab != NULL)
23552 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023553 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023554 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023555 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023557#ifdef FEAT_FLOAT
23558 else if (type == VAR_FLOAT)
23559 (void)string2float(tab + 1, &tv.vval.v_float);
23560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023562 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023563 if (type == VAR_DICT || type == VAR_LIST)
23564 {
23565 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23566
23567 if (etv == NULL)
23568 /* Failed to parse back the dict or list, use it as a
23569 * string. */
23570 tv.v_type = VAR_STRING;
23571 else
23572 {
23573 vim_free(tv.vval.v_string);
23574 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023575 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023576 }
23577 }
23578
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023579 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023580
23581 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023582 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023583 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23584 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 }
23586 }
23587 }
23588
23589 return viminfo_readline(virp);
23590}
23591
23592/*
23593 * Write global vars that start with a capital to the viminfo file
23594 */
23595 void
23596write_viminfo_varlist(fp)
23597 FILE *fp;
23598{
Bram Moolenaar33570922005-01-25 22:26:29 +000023599 hashitem_T *hi;
23600 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023601 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023602 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023603 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023604 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023605 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606
23607 if (find_viminfo_parameter('!') == NULL)
23608 return;
23609
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023610 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023611
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023612 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023613 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023615 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023617 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023618 this_var = HI2DI(hi);
23619 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023621 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023622 {
23623 case VAR_STRING: s = "STR"; break;
23624 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023625#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023626 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023627#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023628 case VAR_DICT: s = "DIC"; break;
23629 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023630 default: continue;
23631 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023632 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023633 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023634 if (p != NULL)
23635 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023636 vim_free(tofree);
23637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 }
23639 }
23640}
23641#endif
23642
23643#if defined(FEAT_SESSION) || defined(PROTO)
23644 int
23645store_session_globals(fd)
23646 FILE *fd;
23647{
Bram Moolenaar33570922005-01-25 22:26:29 +000023648 hashitem_T *hi;
23649 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023650 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 char_u *p, *t;
23652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023653 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023654 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023656 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023658 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023659 this_var = HI2DI(hi);
23660 if ((this_var->di_tv.v_type == VAR_NUMBER
23661 || this_var->di_tv.v_type == VAR_STRING)
23662 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023663 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023664 /* Escape special characters with a backslash. Turn a LF and
23665 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023667 (char_u *)"\\\"\n\r");
23668 if (p == NULL) /* out of memory */
23669 break;
23670 for (t = p; *t != NUL; ++t)
23671 if (*t == '\n')
23672 *t = 'n';
23673 else if (*t == '\r')
23674 *t = 'r';
23675 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023676 this_var->di_key,
23677 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23678 : ' ',
23679 p,
23680 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23681 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023682 || put_eol(fd) == FAIL)
23683 {
23684 vim_free(p);
23685 return FAIL;
23686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 vim_free(p);
23688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023689#ifdef FEAT_FLOAT
23690 else if (this_var->di_tv.v_type == VAR_FLOAT
23691 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23692 {
23693 float_T f = this_var->di_tv.vval.v_float;
23694 int sign = ' ';
23695
23696 if (f < 0)
23697 {
23698 f = -f;
23699 sign = '-';
23700 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023701 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023702 this_var->di_key, sign, f) < 0)
23703 || put_eol(fd) == FAIL)
23704 return FAIL;
23705 }
23706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 }
23708 }
23709 return OK;
23710}
23711#endif
23712
Bram Moolenaar661b1822005-07-28 22:36:45 +000023713/*
23714 * Display script name where an item was last set.
23715 * Should only be invoked when 'verbose' is non-zero.
23716 */
23717 void
23718last_set_msg(scriptID)
23719 scid_T scriptID;
23720{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023721 char_u *p;
23722
Bram Moolenaar661b1822005-07-28 22:36:45 +000023723 if (scriptID != 0)
23724 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023725 p = home_replace_save(NULL, get_scriptname(scriptID));
23726 if (p != NULL)
23727 {
23728 verbose_enter();
23729 MSG_PUTS(_("\n\tLast set from "));
23730 MSG_PUTS(p);
23731 vim_free(p);
23732 verbose_leave();
23733 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023734 }
23735}
23736
Bram Moolenaard812df62008-11-09 12:46:09 +000023737/*
23738 * List v:oldfiles in a nice way.
23739 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023740 void
23741ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023742 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023743{
23744 list_T *l = vimvars[VV_OLDFILES].vv_list;
23745 listitem_T *li;
23746 int nr = 0;
23747
23748 if (l == NULL)
23749 msg((char_u *)_("No old files"));
23750 else
23751 {
23752 msg_start();
23753 msg_scroll = TRUE;
23754 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23755 {
23756 msg_outnum((long)++nr);
23757 MSG_PUTS(": ");
23758 msg_outtrans(get_tv_string(&li->li_tv));
23759 msg_putchar('\n');
23760 out_flush(); /* output one line at a time */
23761 ui_breakcheck();
23762 }
23763 /* Assume "got_int" was set to truncate the listing. */
23764 got_int = FALSE;
23765
23766#ifdef FEAT_BROWSE_CMD
23767 if (cmdmod.browse)
23768 {
23769 quit_more = FALSE;
23770 nr = prompt_for_number(FALSE);
23771 msg_starthere();
23772 if (nr > 0)
23773 {
23774 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23775 (long)nr);
23776
23777 if (p != NULL)
23778 {
23779 p = expand_env_save(p);
23780 eap->arg = p;
23781 eap->cmdidx = CMD_edit;
23782 cmdmod.browse = FALSE;
23783 do_exedit(eap, NULL);
23784 vim_free(p);
23785 }
23786 }
23787 }
23788#endif
23789 }
23790}
23791
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792#endif /* FEAT_EVAL */
23793
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023795#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796
23797#ifdef WIN3264
23798/*
23799 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23800 */
23801static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23802static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23803static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23804
23805/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023806 * Get the short path (8.3) for the filename in "fnamep".
23807 * Only works for a valid file name.
23808 * When the path gets longer "fnamep" is changed and the allocated buffer
23809 * is put in "bufp".
23810 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23811 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 */
23813 static int
23814get_short_pathname(fnamep, bufp, fnamelen)
23815 char_u **fnamep;
23816 char_u **bufp;
23817 int *fnamelen;
23818{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023819 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 char_u *newbuf;
23821
23822 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 l = GetShortPathName(*fnamep, *fnamep, len);
23824 if (l > len - 1)
23825 {
23826 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023827 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 newbuf = vim_strnsave(*fnamep, l);
23829 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831
23832 vim_free(*bufp);
23833 *fnamep = *bufp = newbuf;
23834
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023835 /* Really should always succeed, as the buffer is big enough. */
23836 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 }
23838
23839 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841}
23842
23843/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023844 * Get the short path (8.3) for the filename in "fname". The converted
23845 * path is returned in "bufp".
23846 *
23847 * Some of the directories specified in "fname" may not exist. This function
23848 * will shorten the existing directories at the beginning of the path and then
23849 * append the remaining non-existing path.
23850 *
23851 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023852 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023853 * bufp - Pointer to an allocated buffer for the filename.
23854 * fnamelen - Length of the filename pointed to by fname
23855 *
23856 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 */
23858 static int
23859shortpath_for_invalid_fname(fname, bufp, fnamelen)
23860 char_u **fname;
23861 char_u **bufp;
23862 int *fnamelen;
23863{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023864 char_u *short_fname, *save_fname, *pbuf_unused;
23865 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023867 int old_len, len;
23868 int new_len, sfx_len;
23869 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870
23871 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023872 old_len = *fnamelen;
23873 save_fname = vim_strnsave(*fname, old_len);
23874 pbuf_unused = NULL;
23875 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023877 endp = save_fname + old_len - 1; /* Find the end of the copy */
23878 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023880 /*
23881 * Try shortening the supplied path till it succeeds by removing one
23882 * directory at a time from the tail of the path.
23883 */
23884 len = 0;
23885 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023887 /* go back one path-separator */
23888 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23889 --endp;
23890 if (endp <= save_fname)
23891 break; /* processed the complete path */
23892
23893 /*
23894 * Replace the path separator with a NUL and try to shorten the
23895 * resulting path.
23896 */
23897 ch = *endp;
23898 *endp = 0;
23899 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023900 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023901 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23902 {
23903 retval = FAIL;
23904 goto theend;
23905 }
23906 *endp = ch; /* preserve the string */
23907
23908 if (len > 0)
23909 break; /* successfully shortened the path */
23910
23911 /* failed to shorten the path. Skip the path separator */
23912 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 }
23914
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023915 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023917 /*
23918 * Succeeded in shortening the path. Now concatenate the shortened
23919 * path with the remaining path at the tail.
23920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023922 /* Compute the length of the new path. */
23923 sfx_len = (int)(save_endp - endp) + 1;
23924 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023926 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023928 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023930 /* There is not enough space in the currently allocated string,
23931 * copy it to a buffer big enough. */
23932 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023934 {
23935 retval = FAIL;
23936 goto theend;
23937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 }
23939 else
23940 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023941 /* Transfer short_fname to the main buffer (it's big enough),
23942 * unless get_short_pathname() did its work in-place. */
23943 *fname = *bufp = save_fname;
23944 if (short_fname != save_fname)
23945 vim_strncpy(save_fname, short_fname, len);
23946 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023948
23949 /* concat the not-shortened part of the path */
23950 vim_strncpy(*fname + len, endp, sfx_len);
23951 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023953
23954theend:
23955 vim_free(pbuf_unused);
23956 vim_free(save_fname);
23957
23958 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959}
23960
23961/*
23962 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023963 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 */
23965 static int
23966shortpath_for_partial(fnamep, bufp, fnamelen)
23967 char_u **fnamep;
23968 char_u **bufp;
23969 int *fnamelen;
23970{
23971 int sepcount, len, tflen;
23972 char_u *p;
23973 char_u *pbuf, *tfname;
23974 int hasTilde;
23975
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023976 /* Count up the path separators from the RHS.. so we know which part
23977 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023979 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 if (vim_ispathsep(*p))
23981 ++sepcount;
23982
23983 /* Need full path first (use expand_env() to remove a "~/") */
23984 hasTilde = (**fnamep == '~');
23985 if (hasTilde)
23986 pbuf = tfname = expand_env_save(*fnamep);
23987 else
23988 pbuf = tfname = FullName_save(*fnamep, FALSE);
23989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023990 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023992 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994
23995 if (len == 0)
23996 {
23997 /* Don't have a valid filename, so shorten the rest of the
23998 * path if we can. This CAN give us invalid 8.3 filenames, but
23999 * there's not a lot of point in guessing what it might be.
24000 */
24001 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024002 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 }
24005
24006 /* Count the paths backward to find the beginning of the desired string. */
24007 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024008 {
24009#ifdef FEAT_MBYTE
24010 if (has_mbyte)
24011 p -= mb_head_off(tfname, p);
24012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 if (vim_ispathsep(*p))
24014 {
24015 if (sepcount == 0 || (hasTilde && sepcount == 1))
24016 break;
24017 else
24018 sepcount --;
24019 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 if (hasTilde)
24022 {
24023 --p;
24024 if (p >= tfname)
24025 *p = '~';
24026 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 }
24029 else
24030 ++p;
24031
24032 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24033 vim_free(*bufp);
24034 *fnamelen = (int)STRLEN(p);
24035 *bufp = pbuf;
24036 *fnamep = p;
24037
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024038 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039}
24040#endif /* WIN3264 */
24041
24042/*
24043 * Adjust a filename, according to a string of modifiers.
24044 * *fnamep must be NUL terminated when called. When returning, the length is
24045 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024046 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 * When there is an error, *fnamep is set to NULL.
24048 */
24049 int
24050modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24051 char_u *src; /* string with modifiers */
24052 int *usedlen; /* characters after src that are used */
24053 char_u **fnamep; /* file name so far */
24054 char_u **bufp; /* buffer for allocated file name or NULL */
24055 int *fnamelen; /* length of fnamep */
24056{
24057 int valid = 0;
24058 char_u *tail;
24059 char_u *s, *p, *pbuf;
24060 char_u dirname[MAXPATHL];
24061 int c;
24062 int has_fullname = 0;
24063#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024064 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 int has_shortname = 0;
24066#endif
24067
24068repeat:
24069 /* ":p" - full path/file_name */
24070 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24071 {
24072 has_fullname = 1;
24073
24074 valid |= VALID_PATH;
24075 *usedlen += 2;
24076
24077 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24078 if ((*fnamep)[0] == '~'
24079#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24080 && ((*fnamep)[1] == '/'
24081# ifdef BACKSLASH_IN_FILENAME
24082 || (*fnamep)[1] == '\\'
24083# endif
24084 || (*fnamep)[1] == NUL)
24085
24086#endif
24087 )
24088 {
24089 *fnamep = expand_env_save(*fnamep);
24090 vim_free(*bufp); /* free any allocated file name */
24091 *bufp = *fnamep;
24092 if (*fnamep == NULL)
24093 return -1;
24094 }
24095
24096 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024097 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 {
24099 if (vim_ispathsep(*p)
24100 && p[1] == '.'
24101 && (p[2] == NUL
24102 || vim_ispathsep(p[2])
24103 || (p[2] == '.'
24104 && (p[3] == NUL || vim_ispathsep(p[3])))))
24105 break;
24106 }
24107
24108 /* FullName_save() is slow, don't use it when not needed. */
24109 if (*p != NUL || !vim_isAbsName(*fnamep))
24110 {
24111 *fnamep = FullName_save(*fnamep, *p != NUL);
24112 vim_free(*bufp); /* free any allocated file name */
24113 *bufp = *fnamep;
24114 if (*fnamep == NULL)
24115 return -1;
24116 }
24117
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024118#ifdef WIN3264
24119# if _WIN32_WINNT >= 0x0500
24120 if (vim_strchr(*fnamep, '~') != NULL)
24121 {
24122 /* Expand 8.3 filename to full path. Needed to make sure the same
24123 * file does not have two different names.
24124 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24125 p = alloc(_MAX_PATH + 1);
24126 if (p != NULL)
24127 {
24128 if (GetLongPathName(*fnamep, p, MAXPATHL))
24129 {
24130 vim_free(*bufp);
24131 *bufp = *fnamep = p;
24132 }
24133 else
24134 vim_free(p);
24135 }
24136 }
24137# endif
24138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 /* Append a path separator to a directory. */
24140 if (mch_isdir(*fnamep))
24141 {
24142 /* Make room for one or two extra characters. */
24143 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24144 vim_free(*bufp); /* free any allocated file name */
24145 *bufp = *fnamep;
24146 if (*fnamep == NULL)
24147 return -1;
24148 add_pathsep(*fnamep);
24149 }
24150 }
24151
24152 /* ":." - path relative to the current directory */
24153 /* ":~" - path relative to the home directory */
24154 /* ":8" - shortname path - postponed till after */
24155 while (src[*usedlen] == ':'
24156 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24157 {
24158 *usedlen += 2;
24159 if (c == '8')
24160 {
24161#ifdef WIN3264
24162 has_shortname = 1; /* Postpone this. */
24163#endif
24164 continue;
24165 }
24166 pbuf = NULL;
24167 /* Need full path first (use expand_env() to remove a "~/") */
24168 if (!has_fullname)
24169 {
24170 if (c == '.' && **fnamep == '~')
24171 p = pbuf = expand_env_save(*fnamep);
24172 else
24173 p = pbuf = FullName_save(*fnamep, FALSE);
24174 }
24175 else
24176 p = *fnamep;
24177
24178 has_fullname = 0;
24179
24180 if (p != NULL)
24181 {
24182 if (c == '.')
24183 {
24184 mch_dirname(dirname, MAXPATHL);
24185 s = shorten_fname(p, dirname);
24186 if (s != NULL)
24187 {
24188 *fnamep = s;
24189 if (pbuf != NULL)
24190 {
24191 vim_free(*bufp); /* free any allocated file name */
24192 *bufp = pbuf;
24193 pbuf = NULL;
24194 }
24195 }
24196 }
24197 else
24198 {
24199 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24200 /* Only replace it when it starts with '~' */
24201 if (*dirname == '~')
24202 {
24203 s = vim_strsave(dirname);
24204 if (s != NULL)
24205 {
24206 *fnamep = s;
24207 vim_free(*bufp);
24208 *bufp = s;
24209 }
24210 }
24211 }
24212 vim_free(pbuf);
24213 }
24214 }
24215
24216 tail = gettail(*fnamep);
24217 *fnamelen = (int)STRLEN(*fnamep);
24218
24219 /* ":h" - head, remove "/file_name", can be repeated */
24220 /* Don't remove the first "/" or "c:\" */
24221 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24222 {
24223 valid |= VALID_HEAD;
24224 *usedlen += 2;
24225 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024226 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024227 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 *fnamelen = (int)(tail - *fnamep);
24229#ifdef VMS
24230 if (*fnamelen > 0)
24231 *fnamelen += 1; /* the path separator is part of the path */
24232#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024233 if (*fnamelen == 0)
24234 {
24235 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24236 p = vim_strsave((char_u *)".");
24237 if (p == NULL)
24238 return -1;
24239 vim_free(*bufp);
24240 *bufp = *fnamep = tail = p;
24241 *fnamelen = 1;
24242 }
24243 else
24244 {
24245 while (tail > s && !after_pathsep(s, tail))
24246 mb_ptr_back(*fnamep, tail);
24247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 }
24249
24250 /* ":8" - shortname */
24251 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24252 {
24253 *usedlen += 2;
24254#ifdef WIN3264
24255 has_shortname = 1;
24256#endif
24257 }
24258
24259#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024260 /*
24261 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 */
24263 if (has_shortname)
24264 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024265 /* Copy the string if it is shortened by :h and when it wasn't copied
24266 * yet, because we are going to change it in place. Avoids changing
24267 * the buffer name for "%:8". */
24268 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 {
24270 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024271 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 return -1;
24273 vim_free(*bufp);
24274 *bufp = *fnamep = p;
24275 }
24276
24277 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024278 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 if (!has_fullname && !vim_isAbsName(*fnamep))
24280 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024281 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 return -1;
24283 }
24284 else
24285 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024286 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287
Bram Moolenaardc935552011-08-17 15:23:23 +020024288 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024290 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 return -1;
24292
24293 if (l == 0)
24294 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024295 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024297 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 return -1;
24299 }
24300 *fnamelen = l;
24301 }
24302 }
24303#endif /* WIN3264 */
24304
24305 /* ":t" - tail, just the basename */
24306 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24307 {
24308 *usedlen += 2;
24309 *fnamelen -= (int)(tail - *fnamep);
24310 *fnamep = tail;
24311 }
24312
24313 /* ":e" - extension, can be repeated */
24314 /* ":r" - root, without extension, can be repeated */
24315 while (src[*usedlen] == ':'
24316 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24317 {
24318 /* find a '.' in the tail:
24319 * - for second :e: before the current fname
24320 * - otherwise: The last '.'
24321 */
24322 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24323 s = *fnamep - 2;
24324 else
24325 s = *fnamep + *fnamelen - 1;
24326 for ( ; s > tail; --s)
24327 if (s[0] == '.')
24328 break;
24329 if (src[*usedlen + 1] == 'e') /* :e */
24330 {
24331 if (s > tail)
24332 {
24333 *fnamelen += (int)(*fnamep - (s + 1));
24334 *fnamep = s + 1;
24335#ifdef VMS
24336 /* cut version from the extension */
24337 s = *fnamep + *fnamelen - 1;
24338 for ( ; s > *fnamep; --s)
24339 if (s[0] == ';')
24340 break;
24341 if (s > *fnamep)
24342 *fnamelen = s - *fnamep;
24343#endif
24344 }
24345 else if (*fnamep <= tail)
24346 *fnamelen = 0;
24347 }
24348 else /* :r */
24349 {
24350 if (s > tail) /* remove one extension */
24351 *fnamelen = (int)(s - *fnamep);
24352 }
24353 *usedlen += 2;
24354 }
24355
24356 /* ":s?pat?foo?" - substitute */
24357 /* ":gs?pat?foo?" - global substitute */
24358 if (src[*usedlen] == ':'
24359 && (src[*usedlen + 1] == 's'
24360 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24361 {
24362 char_u *str;
24363 char_u *pat;
24364 char_u *sub;
24365 int sep;
24366 char_u *flags;
24367 int didit = FALSE;
24368
24369 flags = (char_u *)"";
24370 s = src + *usedlen + 2;
24371 if (src[*usedlen + 1] == 'g')
24372 {
24373 flags = (char_u *)"g";
24374 ++s;
24375 }
24376
24377 sep = *s++;
24378 if (sep)
24379 {
24380 /* find end of pattern */
24381 p = vim_strchr(s, sep);
24382 if (p != NULL)
24383 {
24384 pat = vim_strnsave(s, (int)(p - s));
24385 if (pat != NULL)
24386 {
24387 s = p + 1;
24388 /* find end of substitution */
24389 p = vim_strchr(s, sep);
24390 if (p != NULL)
24391 {
24392 sub = vim_strnsave(s, (int)(p - s));
24393 str = vim_strnsave(*fnamep, *fnamelen);
24394 if (sub != NULL && str != NULL)
24395 {
24396 *usedlen = (int)(p + 1 - src);
24397 s = do_string_sub(str, pat, sub, flags);
24398 if (s != NULL)
24399 {
24400 *fnamep = s;
24401 *fnamelen = (int)STRLEN(s);
24402 vim_free(*bufp);
24403 *bufp = s;
24404 didit = TRUE;
24405 }
24406 }
24407 vim_free(sub);
24408 vim_free(str);
24409 }
24410 vim_free(pat);
24411 }
24412 }
24413 /* after using ":s", repeat all the modifiers */
24414 if (didit)
24415 goto repeat;
24416 }
24417 }
24418
Bram Moolenaar26df0922014-02-23 23:39:13 +010024419 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24420 {
24421 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24422 if (p == NULL)
24423 return -1;
24424 vim_free(*bufp);
24425 *bufp = *fnamep = p;
24426 *fnamelen = (int)STRLEN(p);
24427 *usedlen += 2;
24428 }
24429
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430 return valid;
24431}
24432
24433/*
24434 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24435 * "flags" can be "g" to do a global substitute.
24436 * Returns an allocated string, NULL for error.
24437 */
24438 char_u *
24439do_string_sub(str, pat, sub, flags)
24440 char_u *str;
24441 char_u *pat;
24442 char_u *sub;
24443 char_u *flags;
24444{
24445 int sublen;
24446 regmatch_T regmatch;
24447 int i;
24448 int do_all;
24449 char_u *tail;
24450 garray_T ga;
24451 char_u *ret;
24452 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024453 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454
24455 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24456 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024457 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458
24459 ga_init2(&ga, 1, 200);
24460
24461 do_all = (flags[0] == 'g');
24462
24463 regmatch.rm_ic = p_ic;
24464 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24465 if (regmatch.regprog != NULL)
24466 {
24467 tail = str;
24468 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24469 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024470 /* Skip empty match except for first match. */
24471 if (regmatch.startp[0] == regmatch.endp[0])
24472 {
24473 if (zero_width == regmatch.startp[0])
24474 {
24475 /* avoid getting stuck on a match with an empty string */
24476 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24477 ++ga.ga_len;
24478 continue;
24479 }
24480 zero_width = regmatch.startp[0];
24481 }
24482
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 /*
24484 * Get some space for a temporary buffer to do the substitution
24485 * into. It will contain:
24486 * - The text up to where the match is.
24487 * - The substituted text.
24488 * - The text after the match.
24489 */
24490 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24491 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24492 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24493 {
24494 ga_clear(&ga);
24495 break;
24496 }
24497
24498 /* copy the text up to where the match is */
24499 i = (int)(regmatch.startp[0] - tail);
24500 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24501 /* add the substituted text */
24502 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24503 + ga.ga_len + i, TRUE, TRUE, FALSE);
24504 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024505 tail = regmatch.endp[0];
24506 if (*tail == NUL)
24507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 if (!do_all)
24509 break;
24510 }
24511
24512 if (ga.ga_data != NULL)
24513 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24514
Bram Moolenaar473de612013-06-08 18:19:48 +020024515 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 }
24517
24518 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24519 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024520 if (p_cpo == empty_option)
24521 p_cpo = save_cpo;
24522 else
24523 /* Darn, evaluating {sub} expression changed the value. */
24524 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525
24526 return ret;
24527}
24528
24529#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */