blob: 3d20d177b1605e26d64659918f21ac77337922de [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200729static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000731static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000732static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000733static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000734static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000736static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200737#ifdef FEAT_FLOAT
738static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
745static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200748static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200749static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100750static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar493c1782014-05-28 14:34:46 +0200767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200811static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200840static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200841static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200843
844#ifdef EBCDIC
845static int compare_func_name __ARGS((const void *s1, const void *s2));
846static void sortFunctions __ARGS(());
847#endif
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
859 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100883 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200921 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100922# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923
924 /* global variables */
925 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000927 /* autoloaded script names */
928 ga_clear_strings(&ga_loaded);
929
Bram Moolenaarcca74132013-09-25 21:00:28 +0200930 /* Script-local variables. First clear all the variables and in a second
931 * loop free the scriptvar_T, because a variable in one script might hold
932 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200936 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001864 expr = skipwhite(argend);
1865 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1866 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001868 /*
1869 * ":let" without "=": list variables
1870 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 if (*arg == '[')
1872 EMSG(_(e_invarg));
1873 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_glob_vars(&first);
1880 list_buf_vars(&first);
1881 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001884#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_script_vars(&first);
1886 list_func_vars(&first);
1887 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 eap->nextcmd = check_nextcmd(arg);
1890 }
1891 else
1892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op[0] = '=';
1894 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1898 op[0] = *expr; /* +=, -= or .= */
1899 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 else
1902 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (eap->skip)
1908 {
1909 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 --emsg_skip;
1912 }
1913 else if (i != FAIL)
1914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 }
1920}
1921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922/*
1923 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1924 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * When "nextchars" is not NULL it points to a string with characters that
1926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1927 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 * Returns OK or FAIL;
1929 */
1930 static int
1931ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1932 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int copy; /* copy values from "tv", don't move */
1935 int semicolon; /* from skip_var_list() */
1936 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938{
1939 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 listitem_T *item;
1943 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944
1945 if (*arg != '[')
1946 {
1947 /*
1948 * ":let var = expr" or ":for var in list"
1949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 return OK;
1953 }
1954
1955 /*
1956 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1957 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 {
1960 EMSG(_(e_listreq));
1961 return FAIL;
1962 }
1963
1964 i = list_len(l);
1965 if (semicolon == 0 && var_count < i)
1966 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001967 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return FAIL;
1969 }
1970 if (var_count - semicolon > i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975
1976 item = l->lv_first;
1977 while (*arg != ']')
1978 {
1979 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 item = item->li_next;
1982 if (arg == NULL)
1983 return FAIL;
1984
1985 arg = skipwhite(arg);
1986 if (*arg == ';')
1987 {
1988 /* Put the rest of the list (may be empty) in the var after ';'.
1989 * Create a new list for this. */
1990 l = list_alloc();
1991 if (l == NULL)
1992 return FAIL;
1993 while (item != NULL)
1994 {
1995 list_append_tv(l, &item->li_tv);
1996 item = item->li_next;
1997 }
1998
1999 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 ltv.vval.v_list = l;
2002 l->lv_refcount = 1;
2003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2005 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 clear_tv(&ltv);
2007 if (arg == NULL)
2008 return FAIL;
2009 break;
2010 }
2011 else if (*arg != ',' && *arg != ']')
2012 {
2013 EMSG2(_(e_intern2), "ex_let_vars()");
2014 return FAIL;
2015 }
2016 }
2017
2018 return OK;
2019}
2020
2021/*
2022 * Skip over assignable variable "var" or list of variables "[var, var]".
2023 * Used for ":let varvar = expr" and ":for varvar in expr".
2024 * For "[var, var]" increment "*var_count" for each variable.
2025 * for "[var, var; var]" set "semicolon".
2026 * Return NULL for an error.
2027 */
2028 static char_u *
2029skip_var_list(arg, var_count, semicolon)
2030 char_u *arg;
2031 int *var_count;
2032 int *semicolon;
2033{
2034 char_u *p, *s;
2035
2036 if (*arg == '[')
2037 {
2038 /* "[var, var]": find the matching ']'. */
2039 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002040 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2043 s = skip_var_one(p);
2044 if (s == p)
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 ++*var_count;
2050
2051 p = skipwhite(s);
2052 if (*p == ']')
2053 break;
2054 else if (*p == ';')
2055 {
2056 if (*semicolon == 1)
2057 {
2058 EMSG(_("Double ; in list of variables"));
2059 return NULL;
2060 }
2061 *semicolon = 1;
2062 }
2063 else if (*p != ',')
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 }
2069 return p + 1;
2070 }
2071 else
2072 return skip_var_one(arg);
2073}
2074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002077 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 static char_u *
2080skip_var_one(arg)
2081 char_u *arg;
2082{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 if (*arg == '@' && arg[1] != NUL)
2084 return arg + 2;
2085 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087}
2088
Bram Moolenaara7043832005-01-21 11:56:39 +00002089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 * List variables for hashtab "ht" with prefix "prefix".
2091 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashitem_T *hi;
2101 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 int todo;
2103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2106 {
2107 if (!HASHITEM_EMPTY(hi))
2108 {
2109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 di = HI2DI(hi);
2111 if (empty || di->di_tv.v_type != VAR_STRING
2112 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 }
2115 }
2116}
2117
2118/*
2119 * List global variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_glob_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List buffer variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_buf_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 char_u numbuf[NUMBUFLEN];
2136
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139
2140 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2142 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
2145/*
2146 * List window variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_win_vars(first)
2150 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154}
2155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156#ifdef FEAT_WINDOWS
2157/*
2158 * List tab page variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_tab_vars(first)
2162 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002164 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166}
2167#endif
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
2170 * List Vim variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_vim_vars(first)
2174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177}
2178
2179/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180 * List script-local variables, if there is a script.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_script_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2188 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
2192 * List function variables, if there is a function.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_func_vars(first)
2196 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197{
2198 if (current_funccal != NULL)
2199 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201}
2202
2203/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * List variables in "arg".
2205 */
2206 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 exarg_T *eap;
2209 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u *name_start;
2216 char_u *arg_subsc;
2217 char_u *tofree;
2218 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 while (!ends_excmd(*arg) && !got_int)
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2226 {
2227 emsg_severe = TRUE;
2228 EMSG(_(e_trailing));
2229 break;
2230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* get_name_len() takes care of expanding curly braces */
2235 name_start = name = arg;
2236 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2237 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* This is mainly to keep test 49 working: when expanding
2240 * curly braces fails overrule the exception error message. */
2241 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 emsg_severe = TRUE;
2244 EMSG2(_(e_invarg2), arg);
2245 break;
2246 }
2247 error = TRUE;
2248 }
2249 else
2250 {
2251 if (tofree != NULL)
2252 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002253 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 else
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* handle d.key, l[idx], f(expr) */
2258 arg_subsc = arg;
2259 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 'g': list_glob_vars(first); break;
2268 case 'b': list_buf_vars(first); break;
2269 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'v': list_vim_vars(first); break;
2274 case 's': list_script_vars(first); break;
2275 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 default:
2277 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
2280 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 {
2282 char_u numbuf[NUMBUFLEN];
2283 char_u *tf;
2284 int c;
2285 char_u *s;
2286
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002287 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 c = *arg;
2289 *arg = NUL;
2290 list_one_var_a((char_u *)"",
2291 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 tv.v_type,
2293 s == NULL ? (char_u *)"" : s,
2294 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 *arg = c;
2296 vim_free(tf);
2297 }
2298 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305
2306 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 return arg;
2310}
2311
2312/*
2313 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2314 * Returns a pointer to the char just after the var name.
2315 * Returns NULL if there is an error.
2316 */
2317 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324{
2325 int c1;
2326 char_u *name;
2327 char_u *p;
2328 char_u *arg_end = NULL;
2329 int len;
2330 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332
2333 /*
2334 * ":let $VAR = expr": Set environment variable.
2335 */
2336 if (*arg == '$')
2337 {
2338 /* Find the end of the name. */
2339 ++arg;
2340 name = arg;
2341 len = get_env_len(&arg);
2342 if (len == 0)
2343 EMSG2(_(e_invarg2), name - 1);
2344 else
2345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 if (op != NULL && (*op == '+' || *op == '-'))
2347 EMSG2(_(e_letwrong), op);
2348 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002351 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 {
2353 c1 = name[len];
2354 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 p = get_tv_string_chk(tv);
2356 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 {
2358 int mustfree = FALSE;
2359 char_u *s = vim_getenv(name, &mustfree);
2360
2361 if (s != NULL)
2362 {
2363 p = tofree = concat_str(s, p);
2364 if (mustfree)
2365 vim_free(s);
2366 }
2367 }
2368 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (STRICMP(name, "HOME") == 0)
2372 init_homedir();
2373 else if (didset_vim && STRICMP(name, "VIM") == 0)
2374 didset_vim = FALSE;
2375 else if (didset_vimruntime
2376 && STRICMP(name, "VIMRUNTIME") == 0)
2377 didset_vimruntime = FALSE;
2378 arg_end = arg;
2379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
2384 }
2385
2386 /*
2387 * ":let &option = expr": Set option value.
2388 * ":let &l:option = expr": Set local option value.
2389 * ":let &g:option = expr": Set global option value.
2390 */
2391 else if (*arg == '&')
2392 {
2393 /* Find the end of the name. */
2394 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002395 if (p == NULL || (endchars != NULL
2396 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 long n;
2401 int opt_type;
2402 long numval;
2403 char_u *stringval = NULL;
2404 char_u *s;
2405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 c1 = *p;
2407 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408
2409 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 s = get_tv_string_chk(tv); /* != NULL if number or string */
2411 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
2413 opt_type = get_option_value(arg, &numval,
2414 &stringval, opt_flags);
2415 if ((opt_type == 1 && *op == '.')
2416 || (opt_type == 0 && *op != '.'))
2417 EMSG2(_(e_letwrong), op);
2418 else
2419 {
2420 if (opt_type == 1) /* number */
2421 {
2422 if (*op == '+')
2423 n = numval + n;
2424 else
2425 n = numval - n;
2426 }
2427 else if (opt_type == 0 && stringval != NULL) /* string */
2428 {
2429 s = concat_str(stringval, s);
2430 vim_free(stringval);
2431 stringval = s;
2432 }
2433 }
2434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (s != NULL)
2436 {
2437 set_option_value(arg, n, s, opt_flags);
2438 arg_end = p;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let @r = expr": Set register contents.
2447 */
2448 else if (*arg == '@')
2449 {
2450 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 if (op != NULL && (*op == '+' || *op == '-'))
2452 EMSG2(_(e_letwrong), op);
2453 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *s;
2460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 p = get_tv_string_chk(tv);
2462 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (s != NULL)
2466 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(s);
2469 }
2470 }
2471 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 arg_end = arg + 1;
2475 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002476 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002488 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501
2502 else
2503 EMSG2(_(e_invarg2), arg);
2504
2505 return arg_end;
2506}
2507
2508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2510 */
2511 static int
2512check_changedtick(arg)
2513 char_u *arg;
2514{
2515 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2516 {
2517 EMSG2(_(e_readonlyvar), arg);
2518 return TRUE;
2519 }
2520 return FALSE;
2521}
2522
2523/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Get an lval: variable, Dict item or List item that can be assigned a value
2525 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2526 * "name.key", "name.key[expr]" etc.
2527 * Indexing only works if "name" is an existing List or Dictionary.
2528 * "name" points to the start of the name.
2529 * If "rettv" is not NULL it points to the value to be assigned.
2530 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2531 * wrong; must end in space or cmd separator.
2532 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533 * flags:
2534 * GLV_QUIET: do not give error messages
2535 * GLV_NO_AUTOLOAD: do not use script autoloading
2536 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002538 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
2541 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
2545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int unlet;
2547 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 char_u *expr_start, *expr_end;
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 dictitem_T *v;
2555 typval_T var1;
2556 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566
2567 if (skip)
2568 {
2569 /* When skipping just find the end of the name. */
2570 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 }
2573
2574 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (expr_start != NULL)
2577 {
2578 /* Don't expand the name when we already know there is an error. */
2579 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2580 && *p != '[' && *p != '.')
2581 {
2582 EMSG(_(e_trailing));
2583 return NULL;
2584 }
2585
2586 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2587 if (lp->ll_exp_name == NULL)
2588 {
2589 /* Report an invalid expression in braces, unless the
2590 * expression evaluation has been cancelled due to an
2591 * aborting error, an interrupt, or an exception. */
2592 if (!aborting() && !quiet)
2593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002594 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 EMSG2(_(e_invarg2), name);
2596 return NULL;
2597 }
2598 }
2599 lp->ll_name = lp->ll_exp_name;
2600 }
2601 else
2602 lp->ll_name = name;
2603
2604 /* Without [idx] or .key we are done. */
2605 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2606 return p;
2607
2608 cc = *p;
2609 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (v == NULL && !quiet)
2612 EMSG2(_(e_undefvar), lp->ll_name);
2613 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 if (v == NULL)
2615 return NULL;
2616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /*
2618 * Loop until no more [idx] or .key is following.
2619 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2624 && !(lp->ll_tv->v_type == VAR_DICT
2625 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E689: Can only index a List or Dictionary"));
2629 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E708: [:] must come last"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 len = -1;
2639 if (*p == '.')
2640 {
2641 key = p + 1;
2642 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2643 ;
2644 if (len == 0)
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_(e_emptykey));
2648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = key + len;
2651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 else
2653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*p == ':')
2657 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 empty1 = FALSE;
2661 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var1) == NULL)
2664 {
2665 /* not a number or string */
2666 clear_tv(&var1);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670
2671 /* Optionally get the second index [ :expr]. */
2672 if (*p == ':')
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (rettv != NULL && (rettv->v_type != VAR_LIST
2683 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = skipwhite(p + 1);
2692 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var2) == NULL)
2704 {
2705 /* not a number or string */
2706 if (!empty1)
2707 clear_tv(&var1);
2708 clear_tv(&var2);
2709 return NULL;
2710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Skip to past ']'. */
2729 ++p;
2730 }
2731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
2734 if (len == -1)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*key == NUL)
2739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_list = NULL;
2747 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 /* When assigning to a scope dictionary check that a function and
2751 * variable name is valid (only variable name unless it is l: or
2752 * g: dictionary). Disallow overwriting a builtin function. */
2753 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 int prevval;
2756 int wrong;
2757
2758 if (len != -1)
2759 {
2760 prevval = key[len];
2761 key[len] = NUL;
2762 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002763 else
2764 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2766 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 || !valid_varname(key);
2769 if (len != -1)
2770 key[len] = prevval;
2771 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 return NULL;
2773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* Can't add "v:" variable. */
2778 if (lp->ll_dict == &vimvardict)
2779 {
2780 EMSG2(_(e_illvar), name);
2781 return NULL;
2782 }
2783
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 p = NULL;
2801 break;
2802 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* existing variable, need to check if it can be changed */
2804 else if (var_check_ro(lp->ll_di->di_flags, name))
2805 return NULL;
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 else
2812 {
2813 /*
2814 * Get the number and item for the only or first index of the List.
2815 */
2816 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
2819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
2822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_list = lp->ll_tv->vval.v_list;
2825 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2826 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (lp->ll_n1 < 0)
2829 {
2830 lp->ll_n1 = 0;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 }
2833 }
2834 if (lp->ll_li == NULL)
2835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
2843 /*
2844 * May need to find the item or absolute index for the second
2845 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 * When no index given: "lp->ll_empty2" is TRUE.
2847 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 {
2858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2866 if (lp->ll_n1 < 0)
2867 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2868 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 {
2870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return p;
2881}
2882
2883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 */
2886 static void
2887clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 vim_free(lp->ll_exp_name);
2891 vim_free(lp->ll_newkey);
2892}
2893
2894/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906{
2907 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 listitem_T *ri;
2909 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 cc = *endp;
2916 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 if (op != NULL && *op != '=')
2918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002923 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 {
2925 if (tv_op(&tv, rettv, op) == OK)
2926 set_var(lp->ll_name, &tv, FALSE);
2927 clear_tv(&tv);
2928 }
2929 }
2930 else
2931 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 else if (tv_check_lock(lp->ll_newkey == NULL
2936 ? lp->ll_tv->v_lock
2937 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2938 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 else if (lp->ll_range)
2940 {
2941 /*
2942 * Assign the List values to the list items.
2943 */
2944 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2948 else
2949 {
2950 clear_tv(&lp->ll_li->li_tv);
2951 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ri = ri->li_next;
2954 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2955 break;
2956 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002959 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 ri = NULL;
2962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_li = lp->ll_li->li_next;
2966 ++lp->ll_n1;
2967 }
2968 if (ri != NULL)
2969 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else if (lp->ll_empty2
2971 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 : lp->ll_n1 != lp->ll_n2)
2973 EMSG(_("E711: List value has not enough items"));
2974 }
2975 else
2976 {
2977 /*
2978 * Assign to a List or Dictionary item.
2979 */
2980 if (lp->ll_newkey != NULL)
2981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
2984 EMSG2(_(e_letwrong), op);
2985 return;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 if (di == NULL)
2991 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2993 {
2994 vim_free(di);
2995 return;
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (op != NULL && *op != '=')
3000 {
3001 tv_op(lp->ll_tv, rettv, op);
3002 return;
3003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /*
3008 * Assign the value to the variable or list item.
3009 */
3010 if (copy)
3011 copy_tv(rettv, lp->ll_tv);
3012 else
3013 {
3014 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003015 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 }
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019}
3020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3023 * Returns OK or FAIL.
3024 */
3025 static int
3026tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T *tv1;
3028 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *op;
3030{
3031 long n;
3032 char_u numbuf[NUMBUFLEN];
3033 char_u *s;
3034
3035 /* Can't do anything with a Funcref or a Dict on the right. */
3036 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3037 {
3038 switch (tv1->v_type)
3039 {
3040 case VAR_DICT:
3041 case VAR_FUNC:
3042 break;
3043
3044 case VAR_LIST:
3045 if (*op != '+' || tv2->v_type != VAR_LIST)
3046 break;
3047 /* List += List */
3048 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3049 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3050 return OK;
3051
3052 case VAR_NUMBER:
3053 case VAR_STRING:
3054 if (tv2->v_type == VAR_LIST)
3055 break;
3056 if (*op == '+' || *op == '-')
3057 {
3058 /* nr += nr or nr -= nr*/
3059 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#ifdef FEAT_FLOAT
3061 if (tv2->v_type == VAR_FLOAT)
3062 {
3063 float_T f = n;
3064
3065 if (*op == '+')
3066 f += tv2->vval.v_float;
3067 else
3068 f -= tv2->vval.v_float;
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_FLOAT;
3071 tv1->vval.v_float = f;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
3075 {
3076 if (*op == '+')
3077 n += get_tv_number(tv2);
3078 else
3079 n -= get_tv_number(tv2);
3080 clear_tv(tv1);
3081 tv1->v_type = VAR_NUMBER;
3082 tv1->vval.v_number = n;
3083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 else
3086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 if (tv2->v_type == VAR_FLOAT)
3088 break;
3089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 /* str .= str */
3091 s = get_tv_string(tv1);
3092 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_STRING;
3095 tv1->vval.v_string = s;
3096 }
3097 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098
3099#ifdef FEAT_FLOAT
3100 case VAR_FLOAT:
3101 {
3102 float_T f;
3103
3104 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3105 && tv2->v_type != VAR_NUMBER
3106 && tv2->v_type != VAR_STRING))
3107 break;
3108 if (tv2->v_type == VAR_FLOAT)
3109 f = tv2->vval.v_float;
3110 else
3111 f = get_tv_number(tv2);
3112 if (*op == '+')
3113 tv1->vval.v_float += f;
3114 else
3115 tv1->vval.v_float -= f;
3116 }
3117 return OK;
3118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 }
3121
3122 EMSG2(_(e_letwrong), op);
3123 return FAIL;
3124}
3125
3126/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 * Add a watcher to a list.
3128 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003129 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
3134 lw->lw_next = l->lv_watch;
3135 l->lv_watch = lw;
3136}
3137
3138/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003139 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 * No warning when it isn't found...
3141 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 lwp = &l->lv_watch;
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 {
3152 if (lw == lwrem)
3153 {
3154 *lwp = lw->lw_next;
3155 break;
3156 }
3157 lwp = &lw->lw_next;
3158 }
3159}
3160
3161/*
3162 * Just before removing an item from a list: advance watchers to the next
3163 * item.
3164 */
3165 static void
3166list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 if (lw->lw_item == item)
3174 lw->lw_item = item->li_next;
3175}
3176
3177/*
3178 * Evaluate the expression used in a ":for var in expr" command.
3179 * "arg" points to "var".
3180 * Set "*errp" to TRUE for an error, FALSE otherwise;
3181 * Return a pointer that holds the info. Null when there is an error.
3182 */
3183 void *
3184eval_for_line(arg, errp, nextcmdp, skip)
3185 char_u *arg;
3186 int *errp;
3187 char_u **nextcmdp;
3188 int skip;
3189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 typval_T tv;
3193 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 *errp = TRUE; /* default: there is an error */
3196
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 if (fi == NULL)
3199 return NULL;
3200
3201 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3202 if (expr == NULL)
3203 return fi;
3204
3205 expr = skipwhite(expr);
3206 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003208 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 return fi;
3210 }
3211
3212 if (skip)
3213 ++emsg_skip;
3214 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3215 {
3216 *errp = FALSE;
3217 if (!skip)
3218 {
3219 l = tv.vval.v_list;
3220 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 clear_tv(&tv);
3224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 else
3226 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003227 /* No need to increment the refcount, it's already set for the
3228 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 fi->fi_list = l;
3230 list_add_watch(l, &fi->fi_lw);
3231 fi->fi_lw.lw_item = l->lv_first;
3232 }
3233 }
3234 }
3235 if (skip)
3236 --emsg_skip;
3237
3238 return fi;
3239}
3240
3241/*
3242 * Use the first item in a ":for" list. Advance to the next.
3243 * Assign the values to the variable (list). "arg" points to the first one.
3244 * Return TRUE when a valid item was found, FALSE when at end of list or
3245 * something wrong.
3246 */
3247 int
3248next_for_item(fi_void, arg)
3249 void *fi_void;
3250 char_u *arg;
3251{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 item = fi->fi_lw.lw_item;
3257 if (item == NULL)
3258 result = FALSE;
3259 else
3260 {
3261 fi->fi_lw.lw_item = item->li_next;
3262 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3263 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3264 }
3265 return result;
3266}
3267
3268/*
3269 * Free the structure used to store info used by ":for".
3270 */
3271 void
3272free_for_info(fi_void)
3273 void *fi_void;
3274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003277 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 list_unref(fi->fi_list);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 vim_free(fi);
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287 void
3288set_context_for_expression(xp, arg, cmdidx)
3289 expand_T *xp;
3290 char_u *arg;
3291 cmdidx_T cmdidx;
3292{
3293 int got_eq = FALSE;
3294 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (cmdidx == CMD_let)
3298 {
3299 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003303 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 {
3305 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 if (vim_iswhite(*p))
3308 break;
3309 }
3310 return;
3311 }
3312 }
3313 else
3314 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3315 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 while ((xp->xp_pattern = vim_strpbrk(arg,
3317 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3318 {
3319 c = *xp->xp_pattern;
3320 if (c == '&')
3321 {
3322 c = xp->xp_pattern[1];
3323 if (c == '&')
3324 {
3325 ++xp->xp_pattern;
3326 xp->xp_context = cmdidx != CMD_let || got_eq
3327 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3328 }
3329 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003332 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3333 xp->xp_pattern += 2;
3334
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (c == '$')
3338 {
3339 /* environment variable */
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 }
3342 else if (c == '=')
3343 {
3344 got_eq = TRUE;
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else if (c == '<'
3348 && xp->xp_context == EXPAND_FUNCTIONS
3349 && vim_strchr(xp->xp_pattern, '(') == NULL)
3350 {
3351 /* Function name can start with "<SNR>" */
3352 break;
3353 }
3354 else if (cmdidx != CMD_let || got_eq)
3355 {
3356 if (c == '"') /* string */
3357 {
3358 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3359 if (c == '\\' && xp->xp_pattern[1] != NUL)
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_NOTHING;
3362 }
3363 else if (c == '\'') /* literal string */
3364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3367 /* skip */ ;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '|')
3371 {
3372 if (xp->xp_pattern[1] == '|')
3373 {
3374 ++xp->xp_pattern;
3375 xp->xp_context = EXPAND_EXPRESSION;
3376 }
3377 else
3378 xp->xp_context = EXPAND_COMMANDS;
3379 }
3380 else
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 /* Doesn't look like something valid, expand as an expression
3385 * anyway. */
3386 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 arg = xp->xp_pattern;
3388 if (*arg != NUL)
3389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3390 /* skip */ ;
3391 }
3392 xp->xp_pattern = arg;
3393}
3394
3395#endif /* FEAT_CMDL_COMPL */
3396
3397/*
3398 * ":1,25call func(arg1, arg2)" function call.
3399 */
3400 void
3401ex_call(eap)
3402 exarg_T *eap;
3403{
3404 char_u *arg = eap->arg;
3405 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 linenr_T lnum;
3411 int doesrange;
3412 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 if (eap->skip)
3416 {
3417 /* trans_function_name() doesn't work well when skipping, use eval0()
3418 * instead to skip to any following command, e.g. for:
3419 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003420 ++emsg_skip;
3421 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3422 clear_tv(&rettv);
3423 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003424 return;
3425 }
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003428 if (fudi.fd_newkey != NULL)
3429 {
3430 /* Still need to give an error message for missing key. */
3431 EMSG2(_(e_dictkey), fudi.fd_newkey);
3432 vim_free(fudi.fd_newkey);
3433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 if (tofree == NULL)
3435 return;
3436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003437 /* Increase refcount on dictionary, it could get deleted when evaluating
3438 * the arguments. */
3439 if (fudi.fd_dict != NULL)
3440 ++fudi.fd_dict->dv_refcount;
3441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003444 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar532c7802005-01-27 14:44:31 +00003446 /* Skip white space to allow ":call func ()". Not good, but required for
3447 * backward compatibility. */
3448 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (*startarg != '(')
3452 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003453 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 goto end;
3455 }
3456
3457 /*
3458 * When skipping, evaluate the function once, to find the end of the
3459 * arguments.
3460 * When the function takes a range, this is discovered after the first
3461 * call, and the loop is broken.
3462 */
3463 if (eap->skip)
3464 {
3465 ++emsg_skip;
3466 lnum = eap->line2; /* do it once, also with an invalid range */
3467 }
3468 else
3469 lnum = eap->line1;
3470 for ( ; lnum <= eap->line2; ++lnum)
3471 {
3472 if (!eap->skip && eap->addr_count > 0)
3473 {
3474 curwin->w_cursor.lnum = lnum;
3475 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003476#ifdef FEAT_VIRTUALEDIT
3477 curwin->w_cursor.coladd = 0;
3478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 eap->line1, eap->line2, &doesrange,
3483 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 failed = TRUE;
3486 break;
3487 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
3489 /* Handle a function returning a Funcref, Dictionary or List. */
3490 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3491 {
3492 failed = TRUE;
3493 break;
3494 }
3495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (doesrange || eap->skip)
3498 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (aborting())
3505 break;
3506 }
3507 if (eap->skip)
3508 --emsg_skip;
3509
3510 if (!failed)
3511 {
3512 /* Check for trailing illegal characters and a following command. */
3513 if (!ends_excmd(*arg))
3514 {
3515 emsg_severe = TRUE;
3516 EMSG(_(e_trailing));
3517 }
3518 else
3519 eap->nextcmd = check_nextcmd(arg);
3520 }
3521
3522end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003524 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * ":unlet[!] var1 ... " command.
3529 */
3530 void
3531ex_unlet(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 ex_unletlock(eap, eap->arg, 0);
3535}
3536
3537/*
3538 * ":lockvar" and ":unlockvar" commands
3539 */
3540 void
3541ex_lockvar(eap)
3542 exarg_T *eap;
3543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 int deep = 2;
3546
3547 if (eap->forceit)
3548 deep = -1;
3549 else if (vim_isdigit(*arg))
3550 {
3551 deep = getdigits(&arg);
3552 arg = skipwhite(arg);
3553 }
3554
3555 ex_unletlock(eap, arg, deep);
3556}
3557
3558/*
3559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3560 */
3561 static void
3562ex_unletlock(eap, argstart, deep)
3563 exarg_T *eap;
3564 char_u *argstart;
3565 int deep;
3566{
3567 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 do
3573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003575 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003576 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 if (lv.ll_name == NULL)
3578 error = TRUE; /* error but continue parsing */
3579 if (name_end == NULL || (!vim_iswhite(*name_end)
3580 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (name_end != NULL)
3583 {
3584 emsg_severe = TRUE;
3585 EMSG(_(e_trailing));
3586 }
3587 if (!(eap->skip || error))
3588 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591
3592 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 {
3594 if (eap->cmdidx == CMD_unlet)
3595 {
3596 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3597 error = TRUE;
3598 }
3599 else
3600 {
3601 if (do_lock_var(&lv, name_end, deep,
3602 eap->cmdidx == CMD_lockvar) == FAIL)
3603 error = TRUE;
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (!eap->skip)
3608 clear_lval(&lv);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(name_end);
3611 } while (!ends_excmd(*arg));
3612
3613 eap->nextcmd = check_nextcmd(arg);
3614}
3615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 int forceit;
3621{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 int ret = OK;
3623 int cc;
3624
3625 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 cc = *name_end;
3628 *name_end = NUL;
3629
3630 /* Normal name or expanded name. */
3631 if (check_changedtick(lp->ll_name))
3632 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3638 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else if (lp->ll_range)
3640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642
3643 /* Delete a range of List items. */
3644 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 li = lp->ll_li->li_next;
3647 listitem_remove(lp->ll_list, lp->ll_li);
3648 lp->ll_li = li;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else
3653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a List item. */
3656 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003659 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 }
3661
3662 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663}
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hashtab_T *ht;
3675 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003677 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 ht = find_var_ht(name, &varname);
3680 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = hash_find(ht, varname);
3683 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003685 di = HI2DI(hi);
3686 if (var_check_fixed(di->di_flags, name)
3687 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 return FAIL;
3689 delete_var(ht, hi);
3690 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 if (forceit)
3694 return OK;
3695 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697}
3698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699/*
3700 * Lock or unlock variable indicated by "lp".
3701 * "deep" is the levels to go (-1 for unlimited);
3702 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3703 */
3704 static int
3705do_lock_var(lp, name_end, deep, lock)
3706 lval_T *lp;
3707 char_u *name_end;
3708 int deep;
3709 int lock;
3710{
3711 int ret = OK;
3712 int cc;
3713 dictitem_T *di;
3714
3715 if (deep == 0) /* nothing to do */
3716 return OK;
3717
3718 if (lp->ll_tv == NULL)
3719 {
3720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003728 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 if (di == NULL)
3730 ret = FAIL;
3731 else
3732 {
3733 if (lock)
3734 di->di_flags |= DI_FLAGS_LOCK;
3735 else
3736 di->di_flags &= ~DI_FLAGS_LOCK;
3737 item_lock(&di->di_tv, deep, lock);
3738 }
3739 }
3740 *name_end = cc;
3741 }
3742 else if (lp->ll_range)
3743 {
3744 listitem_T *li = lp->ll_li;
3745
3746 /* (un)lock a range of List items. */
3747 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3748 {
3749 item_lock(&li->li_tv, deep, lock);
3750 li = li->li_next;
3751 ++lp->ll_n1;
3752 }
3753 }
3754 else if (lp->ll_list != NULL)
3755 /* (un)lock a List item. */
3756 item_lock(&lp->ll_li->li_tv, deep, lock);
3757 else
3758 /* un(lock) a Dictionary item. */
3759 item_lock(&lp->ll_di->di_tv, deep, lock);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Lock or unlock an item. "deep" is nr of levels to go.
3766 */
3767 static void
3768item_lock(tv, deep, lock)
3769 typval_T *tv;
3770 int deep;
3771 int lock;
3772{
3773 static int recurse = 0;
3774 list_T *l;
3775 listitem_T *li;
3776 dict_T *d;
3777 hashitem_T *hi;
3778 int todo;
3779
3780 if (recurse >= DICT_MAXNEST)
3781 {
3782 EMSG(_("E743: variable nested too deep for (un)lock"));
3783 return;
3784 }
3785 if (deep == 0)
3786 return;
3787 ++recurse;
3788
3789 /* lock/unlock the item itself */
3790 if (lock)
3791 tv->v_lock |= VAR_LOCKED;
3792 else
3793 tv->v_lock &= ~VAR_LOCKED;
3794
3795 switch (tv->v_type)
3796 {
3797 case VAR_LIST:
3798 if ((l = tv->vval.v_list) != NULL)
3799 {
3800 if (lock)
3801 l->lv_lock |= VAR_LOCKED;
3802 else
3803 l->lv_lock &= ~VAR_LOCKED;
3804 if (deep < 0 || deep > 1)
3805 /* recursive: lock/unlock the items the List contains */
3806 for (li = l->lv_first; li != NULL; li = li->li_next)
3807 item_lock(&li->li_tv, deep - 1, lock);
3808 }
3809 break;
3810 case VAR_DICT:
3811 if ((d = tv->vval.v_dict) != NULL)
3812 {
3813 if (lock)
3814 d->dv_lock |= VAR_LOCKED;
3815 else
3816 d->dv_lock &= ~VAR_LOCKED;
3817 if (deep < 0 || deep > 1)
3818 {
3819 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
3826 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3827 }
3828 }
3829 }
3830 }
3831 }
3832 --recurse;
3833}
3834
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003836 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3837 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003838 */
3839 static int
3840tv_islocked(tv)
3841 typval_T *tv;
3842{
3843 return (tv->v_lock & VAR_LOCKED)
3844 || (tv->v_type == VAR_LIST
3845 && tv->vval.v_list != NULL
3846 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3847 || (tv->v_type == VAR_DICT
3848 && tv->vval.v_dict != NULL
3849 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3853/*
3854 * Delete all "menutrans_" variables.
3855 */
3856 void
3857del_menutrans_vars()
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3870 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 }
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875#endif
3876
3877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3878
3879/*
3880 * Local string buffer for the next two functions to store a variable name
3881 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3882 * get_user_var_name().
3883 */
3884
3885static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3886
3887static char_u *varnamebuf = NULL;
3888static int varnamebuflen = 0;
3889
3890/*
3891 * Function to concatenate a prefix and a variable name.
3892 */
3893 static char_u *
3894cat_prefix_varname(prefix, name)
3895 int prefix;
3896 char_u *name;
3897{
3898 int len;
3899
3900 len = (int)STRLEN(name) + 3;
3901 if (len > varnamebuflen)
3902 {
3903 vim_free(varnamebuf);
3904 len += 10; /* some additional space */
3905 varnamebuf = alloc(len);
3906 if (varnamebuf == NULL)
3907 {
3908 varnamebuflen = 0;
3909 return NULL;
3910 }
3911 varnamebuflen = len;
3912 }
3913 *varnamebuf = prefix;
3914 varnamebuf[1] = ':';
3915 STRCPY(varnamebuf + 2, name);
3916 return varnamebuf;
3917}
3918
3919/*
3920 * Function given to ExpandGeneric() to obtain the list of user defined
3921 * (global/buffer/window/built-in) variable names.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *
3924get_user_var_name(xp, idx)
3925 expand_T *xp;
3926 int idx;
3927{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 static long_u gdone;
3929 static long_u bdone;
3930 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 static long_u tdone;
3933#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static int vidx;
3935 static hashitem_T *hi;
3936 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941#ifdef FEAT_WINDOWS
3942 tdone = 0;
3943#endif
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* Global variables */
3947 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003951 else
3952 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3956 return cat_prefix_varname('g', hi->hi_key);
3957 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return (char_u *)"b:changedtick";
3976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977
3978 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003979 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003982 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 else
3985 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003993 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 if (tdone < ht->ht_used)
3995 {
3996 if (tdone++ == 0)
3997 hi = ht->ht_array;
3998 else
3999 ++hi;
4000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 return cat_prefix_varname('t', hi->hi_key);
4003 }
4004#endif
4005
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 /* v: variables */
4007 if (vidx < VV_LEN)
4008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 vim_free(varnamebuf);
4011 varnamebuf = NULL;
4012 varnamebuflen = 0;
4013 return NULL;
4014}
4015
4016#endif /* FEAT_CMDL_COMPL */
4017
4018/*
4019 * types for expressions.
4020 */
4021typedef enum
4022{
4023 TYPE_UNKNOWN = 0
4024 , TYPE_EQUAL /* == */
4025 , TYPE_NEQUAL /* != */
4026 , TYPE_GREATER /* > */
4027 , TYPE_GEQUAL /* >= */
4028 , TYPE_SMALLER /* < */
4029 , TYPE_SEQUAL /* <= */
4030 , TYPE_MATCH /* =~ */
4031 , TYPE_NOMATCH /* !~ */
4032} exptype_T;
4033
4034/*
4035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4038 */
4039
4040/*
4041 * Handle zero level expression.
4042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **nextcmd;
4052 int evaluate;
4053{
4054 int ret;
4055 char_u *p;
4056
4057 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (ret == FAIL || !ends_excmd(*p))
4060 {
4061 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Report the invalid expression unless the expression evaluation has
4065 * been cancelled due to an aborting error, an interrupt, or an
4066 * exception.
4067 */
4068 if (!aborting())
4069 EMSG2(_(e_invexpr2), arg);
4070 ret = FAIL;
4071 }
4072 if (nextcmd != NULL)
4073 *nextcmd = check_nextcmd(p);
4074
4075 return ret;
4076}
4077
4078/*
4079 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004080 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * "arg" must point to the first non-white of the expression.
4083 * "arg" is advanced to the next non-white after the recognized expression.
4084 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
4086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
4095 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Get the first variable.
4100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 if ((*arg)[0] == '?')
4105 {
4106 result = FALSE;
4107 if (evaluate)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int error = FALSE;
4110
4111 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Check for the ":".
4127 */
4128 if ((*arg)[0] != ':')
4129 {
4130 EMSG(_("E109: Missing ':' after '?'"));
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 /*
4137 * Get the third variable.
4138 */
4139 *arg = skipwhite(*arg + 1);
4140 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4141 {
4142 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 return OK;
4151}
4152
4153/*
4154 * Handle first level expression:
4155 * expr2 || expr2 || expr2 logical OR
4156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 long result;
4170 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat until there is no following "||".
4181 */
4182 first = TRUE;
4183 result = FALSE;
4184 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4185 {
4186 if (evaluate && first)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 first = FALSE;
4194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 2);
4200 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4201 return FAIL;
4202
4203 /*
4204 * Compute the result.
4205 */
4206 if (evaluate && !result)
4207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (error)
4212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (evaluate)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * Handle second level expression:
4226 * expr3 && expr3 && expr3 logical AND
4227 *
4228 * "arg" must point to the first non-white of the expression.
4229 * "arg" is advanced to the next non-white after the recognized expression.
4230 *
4231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int evaluate;
4238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "&&".
4252 */
4253 first = TRUE;
4254 result = TRUE;
4255 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval4(arg, &var2, evaluate && result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle third level expression:
4297 * var1 == var2
4298 * var1 =~ var2
4299 * var1 != var2
4300 * var1 !~ var2
4301 * var1 > var2
4302 * var1 >= var2
4303 * var1 < var2
4304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 * var1 is var2
4306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *p;
4321 int i;
4322 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int len = 2;
4325 long n1, n2;
4326 char_u *s1, *s2;
4327 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4328 regmatch_T regmatch;
4329 int ic;
4330 char_u *save_cpo;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 p = *arg;
4339 switch (p[0])
4340 {
4341 case '=': if (p[1] == '=')
4342 type = TYPE_EQUAL;
4343 else if (p[1] == '~')
4344 type = TYPE_MATCH;
4345 break;
4346 case '!': if (p[1] == '=')
4347 type = TYPE_NEQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_NOMATCH;
4350 break;
4351 case '>': if (p[1] != '=')
4352 {
4353 type = TYPE_GREATER;
4354 len = 1;
4355 }
4356 else
4357 type = TYPE_GEQUAL;
4358 break;
4359 case '<': if (p[1] != '=')
4360 {
4361 type = TYPE_SMALLER;
4362 len = 1;
4363 }
4364 else
4365 type = TYPE_SEQUAL;
4366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 case 'i': if (p[1] == 's')
4368 {
4369 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4370 len = 5;
4371 if (!vim_isIDc(p[len]))
4372 {
4373 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4374 type_is = TRUE;
4375 }
4376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 if (type != TYPE_UNKNOWN)
4384 {
4385 /* extra question mark appended: ignore case */
4386 if (p[len] == '?')
4387 {
4388 ic = TRUE;
4389 ++len;
4390 }
4391 /* extra '#' appended: match case */
4392 else if (p[len] == '#')
4393 {
4394 ic = FALSE;
4395 ++len;
4396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 ic = p_ic;
4400
4401 /*
4402 * Get the second variable.
4403 */
4404 *arg = skipwhite(p + len);
4405 if (eval5(arg, &var2, evaluate) == FAIL)
4406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410
4411 if (evaluate)
4412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type_is && rettv->v_type != var2.v_type)
4414 {
4415 /* For "is" a different type always means FALSE, for "notis"
4416 * it means TRUE. */
4417 n1 = (type == TYPE_NEQUAL);
4418 }
4419 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4420 {
4421 if (type_is)
4422 {
4423 n1 = (rettv->v_type == var2.v_type
4424 && rettv->vval.v_list == var2.vval.v_list);
4425 if (type == TYPE_NEQUAL)
4426 n1 = !n1;
4427 }
4428 else if (rettv->v_type != var2.v_type
4429 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4430 {
4431 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004434 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 clear_tv(rettv);
4436 clear_tv(&var2);
4437 return FAIL;
4438 }
4439 else
4440 {
4441 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004442 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4443 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_dict == var2.vval.v_dict);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
4462 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4463 else
4464 EMSG(_("E736: Invalid operation for Dictionary"));
4465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4473 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4480 {
4481 if (rettv->v_type != var2.v_type
4482 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4483 {
4484 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 clear_tv(rettv);
4489 clear_tv(&var2);
4490 return FAIL;
4491 }
4492 else
4493 {
4494 /* Compare two Funcrefs for being equal or unequal. */
4495 if (rettv->vval.v_string == NULL
4496 || var2.vval.v_string == NULL)
4497 n1 = FALSE;
4498 else
4499 n1 = STRCMP(rettv->vval.v_string,
4500 var2.vval.v_string) == 0;
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506#ifdef FEAT_FLOAT
4507 /*
4508 * If one of the two variables is a float, compare as a float.
4509 * When using "=~" or "!~", always compare as string.
4510 */
4511 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4512 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4513 {
4514 float_T f1, f2;
4515
4516 if (rettv->v_type == VAR_FLOAT)
4517 f1 = rettv->vval.v_float;
4518 else
4519 f1 = get_tv_number(rettv);
4520 if (var2.v_type == VAR_FLOAT)
4521 f2 = var2.vval.v_float;
4522 else
4523 f2 = get_tv_number(&var2);
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (f1 == f2); break;
4528 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4529 case TYPE_GREATER: n1 = (f1 > f2); break;
4530 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4531 case TYPE_SMALLER: n1 = (f1 < f2); break;
4532 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /*
4541 * If one of the two variables is a number, compare as a number.
4542 * When using "=~" or "!~", always compare as string.
4543 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(rettv);
4548 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (n1 == n2); break;
4552 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4553 case TYPE_GREATER: n1 = (n1 > n2); break;
4554 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4555 case TYPE_SMALLER: n1 = (n1 < n2); break;
4556 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4557 case TYPE_UNKNOWN:
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH: break; /* avoid gcc warning */
4560 }
4561 }
4562 else
4563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 s1 = get_tv_string_buf(rettv, buf1);
4565 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4567 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4568 else
4569 i = 0;
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (i == 0); break;
4574 case TYPE_NEQUAL: n1 = (i != 0); break;
4575 case TYPE_GREATER: n1 = (i > 0); break;
4576 case TYPE_GEQUAL: n1 = (i >= 0); break;
4577 case TYPE_SMALLER: n1 = (i < 0); break;
4578 case TYPE_SEQUAL: n1 = (i <= 0); break;
4579
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH:
4582 /* avoid 'l' flag in 'cpoptions' */
4583 save_cpo = p_cpo;
4584 p_cpo = (char_u *)"";
4585 regmatch.regprog = vim_regcomp(s2,
4586 RE_MAGIC + RE_STRING);
4587 regmatch.rm_ic = ic;
4588 if (regmatch.regprog != NULL)
4589 {
4590 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (type == TYPE_NOMATCH)
4593 n1 = !n1;
4594 }
4595 p_cpo = save_cpo;
4596 break;
4597
4598 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4599 }
4600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 clear_tv(rettv);
4602 clear_tv(&var2);
4603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * Handle fourth level expression:
4613 * + number addition
4614 * - number subtraction
4615 * . string concatenation
4616 *
4617 * "arg" must point to the first non-white of the expression.
4618 * "arg" is advanced to the next non-white after the recognized expression.
4619 *
4620 * Return OK or FAIL.
4621 */
4622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int evaluate;
4627{
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 typval_T var2;
4629 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int op;
4631 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632#ifdef FEAT_FLOAT
4633 float_T f1 = 0, f2 = 0;
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *s1, *s2;
4636 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4637 char_u *p;
4638
4639 /*
4640 * Get the first variable.
4641 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
4644
4645 /*
4646 * Repeat computing, until no '+', '-' or '.' is following.
4647 */
4648 for (;;)
4649 {
4650 op = **arg;
4651 if (op != '+' && op != '-' && op != '.')
4652 break;
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 if ((op != '+' || rettv->v_type != VAR_LIST)
4655#ifdef FEAT_FLOAT
4656 && (op == '.' || rettv->v_type != VAR_FLOAT)
4657#endif
4658 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 {
4660 /* For "list + ...", an illegal use of the first operand as
4661 * a number cannot be determined before evaluating the 2nd
4662 * operand: if this is also a list, all is ok.
4663 * For "something . ...", "something - ..." or "non-list + ...",
4664 * we know that the first operand needs to be a string or number
4665 * without evaluating the 2nd operand. So check before to avoid
4666 * side effects after an error. */
4667 if (evaluate && get_tv_string_chk(rettv) == NULL)
4668 {
4669 clear_tv(rettv);
4670 return FAIL;
4671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
4675 * Get the second variable.
4676 */
4677 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004678 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683
4684 if (evaluate)
4685 {
4686 /*
4687 * Compute the result.
4688 */
4689 if (op == '.')
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4692 s2 = get_tv_string_buf_chk(&var2, buf2);
4693 if (s2 == NULL) /* type error ? */
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 rettv->v_type = VAR_STRING;
4702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 else if (op == '+' && rettv->v_type == VAR_LIST
4705 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 {
4707 /* concatenate Lists */
4708 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4709 &var3) == FAIL)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715 clear_tv(rettv);
4716 *rettv = var3;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 f1 = rettv->vval.v_float;
4726 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 else
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 n1 = get_tv_number_chk(rettv, &error);
4732 if (error)
4733 {
4734 /* This can only happen for "list + non-list". For
4735 * "non-list + ..." or "something - ...", we returned
4736 * before evaluating the 2nd operand. */
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (var2.v_type == VAR_FLOAT)
4742 f1 = n1;
4743#endif
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 {
4748 f2 = var2.vval.v_float;
4749 n2 = 0;
4750 }
4751 else
4752#endif
4753 {
4754 n2 = get_tv_number_chk(&var2, &error);
4755 if (error)
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
4761#ifdef FEAT_FLOAT
4762 if (rettv->v_type == VAR_FLOAT)
4763 f2 = n2;
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767
4768#ifdef FEAT_FLOAT
4769 /* If there is a float on either side the result is a float. */
4770 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4771 {
4772 if (op == '+')
4773 f1 = f1 + f2;
4774 else
4775 f1 = f1 - f2;
4776 rettv->v_type = VAR_FLOAT;
4777 rettv->vval.v_float = f1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#endif
4781 {
4782 if (op == '+')
4783 n1 = n1 + n2;
4784 else
4785 n1 = n1 - n2;
4786 rettv->v_type = VAR_NUMBER;
4787 rettv->vval.v_number = n1;
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793 return OK;
4794}
4795
4796/*
4797 * Handle fifth level expression:
4798 * * number multiplication
4799 * / number division
4800 * % number modulo
4801 *
4802 * "arg" must point to the first non-white of the expression.
4803 * "arg" is advanced to the next non-white after the recognized expression.
4804 *
4805 * Return OK or FAIL.
4806 */
4807 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int op;
4816 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 int use_float = FALSE;
4819 float_T f1 = 0, f2;
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 /*
4824 * Get the first variable.
4825 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 /*
4830 * Repeat computing, until no '*', '/' or '%' is following.
4831 */
4832 for (;;)
4833 {
4834 op = **arg;
4835 if (op != '*' && op != '/' && op != '%')
4836 break;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 {
4843 f1 = rettv->vval.v_float;
4844 use_float = TRUE;
4845 n1 = 0;
4846 }
4847 else
4848#endif
4849 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 if (error)
4852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else
4855 n1 = 0;
4856
4857 /*
4858 * Get the second variable.
4859 */
4860 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 if (evaluate)
4865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (var2.v_type == VAR_FLOAT)
4868 {
4869 if (!use_float)
4870 {
4871 f1 = n1;
4872 use_float = TRUE;
4873 }
4874 f2 = var2.vval.v_float;
4875 n2 = 0;
4876 }
4877 else
4878#endif
4879 {
4880 n2 = get_tv_number_chk(&var2, &error);
4881 clear_tv(&var2);
4882 if (error)
4883 return FAIL;
4884#ifdef FEAT_FLOAT
4885 if (use_float)
4886 f2 = n2;
4887#endif
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 if (op == '*')
4898 f1 = f1 * f2;
4899 else if (op == '/')
4900 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# ifdef VMS
4902 /* VMS crashes on divide by zero, work around it */
4903 if (f2 == 0.0)
4904 {
4905 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004910 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004911 }
4912 else
4913 f1 = f1 / f2;
4914# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 /* We rely on the floating point library to handle divide
4916 * by zero to result in "inf" and not a crash. */
4917 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004922 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 return FAIL;
4924 }
4925 rettv->v_type = VAR_FLOAT;
4926 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 n1 = n1 * n2;
4933 else if (op == '/')
4934 {
4935 if (n2 == 0) /* give an error message? */
4936 {
4937 if (n1 == 0)
4938 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4939 else if (n1 < 0)
4940 n1 = -0x7fffffffL;
4941 else
4942 n1 = 0x7fffffffL;
4943 }
4944 else
4945 n1 = n1 / n2;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
4949 if (n2 == 0) /* give an error message? */
4950 n1 = 0;
4951 else
4952 n1 = n1 % n2;
4953 }
4954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 }
4959
4960 return OK;
4961}
4962
4963/*
4964 * Handle sixth level expression:
4965 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004966 * "string" string constant
4967 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * &option-name option value
4969 * @r register contents
4970 * identifier variable value
4971 * function() function call
4972 * $VAR environment variable
4973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004974 * [expr, expr] List
4975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 *
4977 * Also handle:
4978 * ! in front logical NOT
4979 * - in front unary minus
4980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * trailing [] subscript in String or List
4982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * "arg" must point to the first non-white of the expression.
4985 * "arg" is advanced to the next non-white after the recognized expression.
4986 *
4987 * Return OK or FAIL.
4988 */
4989 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004990eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004994 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 long n;
4997 int len;
4998 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 char_u *start_leader, *end_leader;
5000 int ret = OK;
5001 char_u *alias;
5002
5003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Skip '!' and '-' characters. They are handled later.
5011 */
5012 start_leader = *arg;
5013 while (**arg == '!' || **arg == '-' || **arg == '+')
5014 *arg = skipwhite(*arg + 1);
5015 end_leader = *arg;
5016
5017 switch (**arg)
5018 {
5019 /*
5020 * Number constant.
5021 */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
5029 case '7':
5030 case '8':
5031 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 {
5033#ifdef FEAT_FLOAT
5034 char_u *p = skipdigits(*arg + 1);
5035 int get_float = FALSE;
5036
5037 /* We accept a float when the format matches
5038 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005039 * strict to avoid backwards compatibility problems.
5040 * Don't look for a float after the "." operator, so that
5041 * ":let vers = 1.2.3" doesn't fail. */
5042 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 get_float = TRUE;
5045 p = skipdigits(p + 2);
5046 if (*p == 'e' || *p == 'E')
5047 {
5048 ++p;
5049 if (*p == '-' || *p == '+')
5050 ++p;
5051 if (!vim_isdigit(*p))
5052 get_float = FALSE;
5053 else
5054 p = skipdigits(p + 1);
5055 }
5056 if (ASCII_ISALPHA(*p) || *p == '.')
5057 get_float = FALSE;
5058 }
5059 if (get_float)
5060 {
5061 float_T f;
5062
5063 *arg += string2float(*arg, &f);
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_FLOAT;
5067 rettv->vval.v_float = f;
5068 }
5069 }
5070 else
5071#endif
5072 {
5073 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5074 *arg += len;
5075 if (evaluate)
5076 {
5077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = n;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * String constant: "string".
5086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 break;
5095
5096 /*
5097 * List: [expr, expr]
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Dictionary: {key: val, key: val}
5104 */
5105 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5106 break;
5107
5108 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
5115 * Environment variable: $VAR.
5116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Register contents: @r.
5122 */
5123 case '@': ++*arg;
5124 if (evaluate)
5125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005127 rettv->vval.v_string = get_reg_contents(**arg,
5128 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 if (**arg != NUL)
5131 ++*arg;
5132 break;
5133
5134 /*
5135 * nested expression: (expression).
5136 */
5137 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (**arg == ')')
5140 ++*arg;
5141 else if (ret == OK)
5142 {
5143 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ret = FAIL;
5146 }
5147 break;
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 if (ret == NOTDONE)
5154 {
5155 /*
5156 * Must be a variable or function name.
5157 * Can also be a curly-braces kind of name: {expr}.
5158 */
5159 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (alias != NULL)
5162 s = alias;
5163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 ret = FAIL;
5166 else
5167 {
5168 if (**arg == '(') /* recursive! */
5169 {
5170 /* If "s" is the name of a variable of type VAR_FUNC
5171 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005172 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173
5174 /* Invoke the function. */
5175 ret = get_func_tv(s, len, rettv, arg,
5176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005178
5179 /* If evaluate is FALSE rettv->v_type was not set in
5180 * get_func_tv, but it's needed in handle_subscript() to parse
5181 * what follows. So set it here. */
5182 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5183 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005184 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185 rettv->v_type = VAR_FUNC;
5186 }
5187
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /* Stop the expression evaluation when immediately
5189 * aborting on error, or when an interrupt occurred or
5190 * an exception was thrown but not caught. */
5191 if (aborting())
5192 {
5193 if (ret == OK)
5194 clear_tv(rettv);
5195 ret = FAIL;
5196 }
5197 }
5198 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005199 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005200 else
5201 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005203 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *arg = skipwhite(*arg);
5207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5209 * expr(expr). */
5210 if (ret == OK)
5211 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /*
5214 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5215 */
5216 if (ret == OK && evaluate && end_leader > start_leader)
5217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 int val = 0;
5220#ifdef FEAT_FLOAT
5221 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 if (rettv->v_type == VAR_FLOAT)
5224 f = rettv->vval.v_float;
5225 else
5226#endif
5227 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 clear_tv(rettv);
5231 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else
5234 {
5235 while (end_leader > start_leader)
5236 {
5237 --end_leader;
5238 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 {
5240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 f = !f;
5243 else
5244#endif
5245 val = !val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 {
5249#ifdef FEAT_FLOAT
5250 if (rettv->v_type == VAR_FLOAT)
5251 f = -f;
5252 else
5253#endif
5254 val = -val;
5255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257#ifdef FEAT_FLOAT
5258 if (rettv->v_type == VAR_FLOAT)
5259 {
5260 clear_tv(rettv);
5261 rettv->vval.v_float = f;
5262 }
5263 else
5264#endif
5265 {
5266 clear_tv(rettv);
5267 rettv->v_type = VAR_NUMBER;
5268 rettv->vval.v_number = val;
5269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
5273 return ret;
5274}
5275
5276/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005277 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5278 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5280 */
5281 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
5288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 long len = -1;
5292 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005296 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 return FAIL;
5301 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302#ifdef FEAT_FLOAT
5303 else if (rettv->v_type == VAR_FLOAT)
5304 {
5305 if (verbose)
5306 EMSG(_(e_float_as_string));
5307 return FAIL;
5308 }
5309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 /*
5314 * dict.name
5315 */
5316 key = *arg + 1;
5317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5318 ;
5319 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /*
5326 * something[idx]
5327 *
5328 * Get the (first) variable from inside the [].
5329 */
5330 *arg = skipwhite(*arg + 1);
5331 if (**arg == ':')
5332 empty1 = TRUE;
5333 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5334 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5336 {
5337 /* not a number or string */
5338 clear_tv(&var1);
5339 return FAIL;
5340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 /*
5343 * Get the second variable from inside the [:].
5344 */
5345 if (**arg == ':')
5346 {
5347 range = TRUE;
5348 *arg = skipwhite(*arg + 1);
5349 if (**arg == ']')
5350 empty2 = TRUE;
5351 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (!empty1)
5354 clear_tv(&var1);
5355 return FAIL;
5356 }
5357 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5358 {
5359 /* not a number or string */
5360 if (!empty1)
5361 clear_tv(&var1);
5362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 return FAIL;
5364 }
5365 }
5366
5367 /* Check for the ']'. */
5368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
5377 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 n1 = 0;
5383 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 n1 = get_tv_number(&var1);
5386 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 if (range)
5389 {
5390 if (empty2)
5391 n2 = -1;
5392 else
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 n2 = get_tv_number(&var2);
5395 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
5401 case VAR_NUMBER:
5402 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 len = (long)STRLEN(s);
5405 if (range)
5406 {
5407 /* The resulting variable is a substring. If the indexes
5408 * are out of range the result is empty. */
5409 if (n1 < 0)
5410 {
5411 n1 = len + n1;
5412 if (n1 < 0)
5413 n1 = 0;
5414 }
5415 if (n2 < 0)
5416 n2 = len + n2;
5417 else if (n2 >= len)
5418 n2 = len;
5419 if (n1 >= len || n2 < 0 || n1 > n2)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5423 }
5424 else
5425 {
5426 /* The resulting variable is a string of a single
5427 * character. If the index is too big or negative the
5428 * result is empty. */
5429 if (n1 >= len || n1 < 0)
5430 s = NULL;
5431 else
5432 s = vim_strnsave(s + n1, 1);
5433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 clear_tv(rettv);
5435 rettv->v_type = VAR_STRING;
5436 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 break;
5438
5439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 if (n1 < 0)
5442 n1 = len + n1;
5443 if (!empty1 && (n1 < 0 || n1 >= len))
5444 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005445 /* For a range we allow invalid values and return an empty
5446 * list. A list index out of range is an error. */
5447 if (!range)
5448 {
5449 if (verbose)
5450 EMSGN(_(e_listidx), n1);
5451 return FAIL;
5452 }
5453 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 if (range)
5456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 list_T *l;
5458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459
5460 if (n2 < 0)
5461 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005462 else if (n2 >= len)
5463 n2 = len - 1;
5464 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 l = list_alloc();
5467 if (l == NULL)
5468 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 n1 <= n2; ++n1)
5471 {
5472 if (list_append_tv(l, &item->li_tv) == FAIL)
5473 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005474 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
5476 }
5477 item = item->li_next;
5478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 clear_tv(rettv);
5480 rettv->v_type = VAR_LIST;
5481 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005482 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 else
5485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 clear_tv(rettv);
5488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
5492 case VAR_DICT:
5493 if (range)
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (len == -1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 if (len == -1)
5505 {
5506 key = get_tv_string(&var1);
5507 if (*key == NUL)
5508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005509 if (verbose)
5510 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 }
5515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 if (len == -1)
5521 clear_tv(&var1);
5522 if (item == NULL)
5523 return FAIL;
5524
5525 copy_tv(&item->di_tv, &var1);
5526 clear_tv(rettv);
5527 *rettv = var1;
5528 }
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Get an option value.
5538 * "arg" points to the '&' or '+' before the option name.
5539 * "arg" is advanced to character after the option name.
5540 * Return OK or FAIL.
5541 */
5542 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int evaluate;
5547{
5548 char_u *option_end;
5549 long numval;
5550 char_u *stringval;
5551 int opt_type;
5552 int c;
5553 int working = (**arg == '+'); /* has("+option") */
5554 int ret = OK;
5555 int opt_flags;
5556
5557 /*
5558 * Isolate the option name and find its value.
5559 */
5560 option_end = find_option_end(arg, &opt_flags);
5561 if (option_end == NULL)
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 EMSG2(_("E112: Option name missing: %s"), *arg);
5565 return FAIL;
5566 }
5567
5568 if (!evaluate)
5569 {
5570 *arg = option_end;
5571 return OK;
5572 }
5573
5574 c = *option_end;
5575 *option_end = NUL;
5576 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (opt_type == -3) /* invalid name */
5580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 EMSG2(_("E113: Unknown option: %s"), *arg);
5583 ret = FAIL;
5584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (opt_type == -2) /* hidden string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == -1) /* hidden number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == 1) /* number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else /* string option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 }
5608 else if (working && (opt_type == -2 || opt_type == -1))
5609 ret = FAIL;
5610
5611 *option_end = c; /* put back for error messages */
5612 *arg = option_end;
5613
5614 return ret;
5615}
5616
5617/*
5618 * Allocate a variable for a string constant.
5619 * Return OK or FAIL.
5620 */
5621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int evaluate;
5626{
5627 char_u *p;
5628 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int extra = 0;
5630
5631 /*
5632 * Find the end of the string, skipping backslashed characters.
5633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (*p == '\\' && p[1] != NUL)
5637 {
5638 ++p;
5639 /* A "\<x>" form occupies at least 4 characters, and produces up
5640 * to 6 characters: reserve space for 2 extra */
5641 if (*p == '<')
5642 extra += 2;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
5646 if (*p != '"')
5647 {
5648 EMSG2(_("E114: Missing quote: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 /* If only parsing, set *arg and return here */
5653 if (!evaluate)
5654 {
5655 *arg = p + 1;
5656 return OK;
5657 }
5658
5659 /*
5660 * Copy the string into allocated memory, handling backslashed
5661 * characters.
5662 */
5663 name = alloc((unsigned)(p - *arg + extra));
5664 if (name == NULL)
5665 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 rettv->v_type = VAR_STRING;
5667 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\')
5672 {
5673 switch (*++p)
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case 'b': *name++ = BS; ++p; break;
5676 case 'e': *name++ = ESC; ++p; break;
5677 case 'f': *name++ = FF; ++p; break;
5678 case 'n': *name++ = NL; ++p; break;
5679 case 'r': *name++ = CAR; ++p; break;
5680 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 case 'X': /* hex: "\x1", "\x12" */
5683 case 'x':
5684 case 'u': /* Unicode: "\u0023" */
5685 case 'U':
5686 if (vim_isxdigit(p[1]))
5687 {
5688 int n, nr;
5689 int c = toupper(*p);
5690
5691 if (c == 'X')
5692 n = 2;
5693 else
5694 n = 4;
5695 nr = 0;
5696 while (--n >= 0 && vim_isxdigit(p[1]))
5697 {
5698 ++p;
5699 nr = (nr << 4) + hex2nr(*p);
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#ifdef FEAT_MBYTE
5703 /* For "\u" store the number according to
5704 * 'encoding'. */
5705 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712
5713 /* octal: "\1", "\12", "\123" */
5714 case '0':
5715 case '1':
5716 case '2':
5717 case '3':
5718 case '4':
5719 case '5':
5720 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 case '7': *name = *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = (*name << 3) + *p++ - '0';
5725 if (*p >= '0' && *p <= '7')
5726 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 break;
5730
5731 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (extra != 0)
5734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 /* FALLTHROUGH */
5739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 }
5744 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg = p + 1;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return OK;
5752}
5753
5754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Return OK or FAIL.
5757 */
5758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 int evaluate;
5763{
5764 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 int reduce = 0;
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++reduce;
5778 ++p;
5779 }
5780 }
5781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return FAIL;
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 if (!evaluate)
5790 {
5791 *arg = p + 1;
5792 return OK;
5793 }
5794
5795 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 */
5798 str = alloc((unsigned)((p - *arg) - reduce));
5799 if (str == NULL)
5800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 rettv->v_type = VAR_STRING;
5802 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 break;
5810 ++p;
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 *arg = p + 1;
5816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 return OK;
5818}
5819
5820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 * Allocate a variable for a List and fill it from "*arg".
5822 * Return OK or FAIL.
5823 */
5824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 int evaluate;
5829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 list_T *l = NULL;
5831 typval_T tv;
5832 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
5834 if (evaluate)
5835 {
5836 l = list_alloc();
5837 if (l == NULL)
5838 return FAIL;
5839 }
5840
5841 *arg = skipwhite(*arg + 1);
5842 while (**arg != ']' && **arg != NUL)
5843 {
5844 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5845 goto failret;
5846 if (evaluate)
5847 {
5848 item = listitem_alloc();
5849 if (item != NULL)
5850 {
5851 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 list_append(l, item);
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 else
5856 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
5859 if (**arg == ']')
5860 break;
5861 if (**arg != ',')
5862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 goto failret;
5865 }
5866 *arg = skipwhite(*arg + 1);
5867 }
5868
5869 if (**arg != ']')
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872failret:
5873 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 return FAIL;
5876 }
5877
5878 *arg = skipwhite(*arg + 1);
5879 if (evaluate)
5880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 rettv->v_type = VAR_LIST;
5882 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 ++l->lv_refcount;
5884 }
5885
5886 return OK;
5887}
5888
5889/*
5890 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005891 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005893 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894list_alloc()
5895{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 list_T *l;
5897
5898 l = (list_T *)alloc_clear(sizeof(list_T));
5899 if (l != NULL)
5900 {
5901 /* Prepend the list to the list of lists for garbage collection. */
5902 if (first_list != NULL)
5903 first_list->lv_used_prev = l;
5904 l->lv_used_prev = NULL;
5905 l->lv_used_next = first_list;
5906 first_list = l;
5907 }
5908 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005912 * Allocate an empty list for a return value.
5913 * Returns OK or FAIL.
5914 */
5915 static int
5916rettv_list_alloc(rettv)
5917 typval_T *rettv;
5918{
5919 list_T *l = list_alloc();
5920
5921 if (l == NULL)
5922 return FAIL;
5923
5924 rettv->vval.v_list = l;
5925 rettv->v_type = VAR_LIST;
5926 ++l->lv_refcount;
5927 return OK;
5928}
5929
5930/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 * Unreference a list: decrement the reference count and free it when it
5932 * becomes zero.
5933 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005934 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (l != NULL && --l->lv_refcount <= 0)
5939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940}
5941
5942/*
5943 * Free a list, including all items it points to.
5944 * Ignores the reference count.
5945 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005946 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947list_free(l, recurse)
5948 list_T *l;
5949 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 /* Remove the list from the list of lists for garbage collection. */
5954 if (l->lv_used_prev == NULL)
5955 first_list = l->lv_used_next;
5956 else
5957 l->lv_used_prev->lv_used_next = l->lv_used_next;
5958 if (l->lv_used_next != NULL)
5959 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5960
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it. */
5964 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005965 if (recurse || (item->li_tv.v_type != VAR_LIST
5966 && item->li_tv.v_type != VAR_DICT))
5967 clear_tv(&item->li_tv);
5968 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 }
5970 vim_free(l);
5971}
5972
5973/*
5974 * Allocate a list item.
5975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977listitem_alloc()
5978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980}
5981
5982/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005983 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 vim_free(item);
5991}
5992
5993/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994 * Remove a list item from a List and free it. Also clears the value.
5995 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l;
5999 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006001 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002 listitem_free(item);
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Get the number of items in a list.
6007 */
6008 static long
6009list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 if (l == NULL)
6013 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006014 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * Return TRUE when two lists have exactly the same values.
6019 */
6020 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 list_T *l1;
6023 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006029 if (l1 == NULL || l2 == NULL)
6030 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 if (l1 == l2)
6032 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 if (list_len(l1) != list_len(l2))
6034 return FALSE;
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 for (item1 = l1->lv_first, item2 = l2->lv_first;
6037 item1 != NULL && item2 != NULL;
6038 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return FALSE;
6041 return item1 == NULL && item2 == NULL;
6042}
6043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006044#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6045 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046/*
6047 * Return the dictitem that an entry in a hashtable points to.
6048 */
6049 dictitem_T *
6050dict_lookup(hi)
6051 hashitem_T *hi;
6052{
6053 return HI2DI(hi);
6054}
6055#endif
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 * Return TRUE when two dictionaries have exactly the same key/values.
6059 */
6060 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d1;
6063 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 hashitem_T *hi;
6068 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (d1 == NULL || d2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (d1 == d2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (dict_len(d1) != dict_len(d2))
6076 return FALSE;
6077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006078 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006081 if (!HASHITEM_EMPTY(hi))
6082 {
6083 item2 = dict_find(d2, hi->hi_key, -1);
6084 if (item2 == NULL)
6085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 return FALSE;
6088 --todo;
6089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006090 }
6091 return TRUE;
6092}
6093
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094static int tv_equal_recurse_limit;
6095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 typval_T *tv1;
6104 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 int ic; /* ignore case */
6106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
6108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 * recursiveness to a limit. We guess they are equal then.
6118 * A fixed limit has the problem of still taking an awful long time.
6119 * Reduce the limit every time running into it. That should work fine for
6120 * deeply linked structures that are not recursively linked and catch
6121 * recursiveness quickly. */
6122 if (!recursive)
6123 tv_equal_recurse_limit = 1000;
6124 if (recursive_cnt >= tv_equal_recurse_limit)
6125 {
6126 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 ++recursive_cnt;
6134 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6135 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006136 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_FUNC:
6145 return (tv1->vval.v_string != NULL
6146 && tv2->vval.v_string != NULL
6147 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6148
6149 case VAR_NUMBER:
6150 return tv1->vval.v_number == tv2->vval.v_number;
6151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152#ifdef FEAT_FLOAT
6153 case VAR_FLOAT:
6154 return tv1->vval.v_float == tv2->vval.v_float;
6155#endif
6156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 case VAR_STRING:
6158 s1 = get_tv_string_buf(tv1, buf1);
6159 s2 = get_tv_string_buf(tv2, buf2);
6160 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 return TRUE;
6165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Locate item with index "n" in list "l" and return it.
6169 * A negative index is counted from the end; -1 is the last item.
6170 * Returns NULL when "n" is out of range.
6171 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long n;
6176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 long idx;
6179
6180 if (l == NULL)
6181 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 n = l->lv_len + n;
6186
6187 /* Check for index out of range. */
6188 if (n < 0 || n >= l->lv_len)
6189 return NULL;
6190
6191 /* When there is a cached index may start search from there. */
6192 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_idx / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else if (n > (l->lv_idx + l->lv_len) / 2)
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
6206 else
6207 {
6208 /* closest to the cached index */
6209 item = l->lv_idx_item;
6210 idx = l->lv_idx;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
6213 else
6214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 if (n < l->lv_len / 2)
6216 {
6217 /* closest to the start of the list */
6218 item = l->lv_first;
6219 idx = 0;
6220 }
6221 else
6222 {
6223 /* closest to the end of the list */
6224 item = l->lv_last;
6225 idx = l->lv_len - 1;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228
6229 while (n > idx)
6230 {
6231 /* search forward */
6232 item = item->li_next;
6233 ++idx;
6234 }
6235 while (n < idx)
6236 {
6237 /* search backward */
6238 item = item->li_prev;
6239 --idx;
6240 }
6241
6242 /* cache the used index */
6243 l->lv_idx = idx;
6244 l->lv_idx_item = item;
6245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return item;
6247}
6248
6249/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 * Get list item "l[idx]" as a number.
6251 */
6252 static long
6253list_find_nr(l, idx, errorp)
6254 list_T *l;
6255 long idx;
6256 int *errorp; /* set to TRUE when something wrong */
6257{
6258 listitem_T *li;
6259
6260 li = list_find(l, idx);
6261 if (li == NULL)
6262 {
6263 if (errorp != NULL)
6264 *errorp = TRUE;
6265 return -1L;
6266 }
6267 return get_tv_number_chk(&li->li_tv, errorp);
6268}
6269
6270/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006271 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6272 */
6273 char_u *
6274list_find_str(l, idx)
6275 list_T *l;
6276 long idx;
6277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx - 1);
6281 if (li == NULL)
6282 {
6283 EMSGN(_(e_listidx), idx);
6284 return NULL;
6285 }
6286 return get_tv_string(&li->li_tv);
6287}
6288
6289/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290 * Locate "item" list "l" and return its index.
6291 * Returns -1 when "item" is not in the list.
6292 */
6293 static long
6294list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297{
6298 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006300
6301 if (l == NULL)
6302 return -1;
6303 idx = 0;
6304 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6305 ++idx;
6306 if (li == NULL)
6307 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006308 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Append item "item" to the end of list "l".
6313 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
6319 if (l->lv_last == NULL)
6320 {
6321 /* empty list */
6322 l->lv_first = item;
6323 l->lv_last = item;
6324 item->li_prev = NULL;
6325 }
6326 else
6327 {
6328 l->lv_last->li_next = item;
6329 item->li_prev = l->lv_last;
6330 l->lv_last = item;
6331 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 item->li_next = NULL;
6334}
6335
6336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006340 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l;
6343 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006349 copy_tv(tv, &li->li_tv);
6350 list_append(l, li);
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 * Return FAIL when out of memory.
6357 */
6358 int
6359list_append_dict(list, dict)
6360 list_T *list;
6361 dict_T *dict;
6362{
6363 listitem_T *li = listitem_alloc();
6364
6365 if (li == NULL)
6366 return FAIL;
6367 li->li_tv.v_type = VAR_DICT;
6368 li->li_tv.v_lock = 0;
6369 li->li_tv.vval.v_dict = dict;
6370 list_append(list, li);
6371 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 * Returns FAIL when out of memory.
6379 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 list_T *l;
6383 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385{
6386 listitem_T *li = listitem_alloc();
6387
6388 if (li == NULL)
6389 return FAIL;
6390 list_append(l, li);
6391 li->li_tv.v_type = VAR_STRING;
6392 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 if (str == NULL)
6394 li->li_tv.vval.v_string = NULL;
6395 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006396 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006397 return FAIL;
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006402 * Append "n" to list "l".
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_append_number(l, n)
6407 list_T *l;
6408 varnumber_T n;
6409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l;
6430 typval_T *tv;
6431 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
6443list_insert(l, ni, item)
6444 list_T *l;
6445 listitem_T *ni;
6446 listitem_T *item;
6447{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (item == NULL)
6449 /* Append new item at end of list. */
6450 list_append(l, ni);
6451 else
6452 {
6453 /* Insert new item before existing item. */
6454 ni->li_prev = item->li_prev;
6455 ni->li_next = item;
6456 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 ++l->lv_idx;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 l->lv_idx_item = NULL;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469}
6470
6471/*
6472 * Extend "l1" with "l2".
6473 * If "bef" is NULL append at the end, otherwise insert before this item.
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
6477list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l1;
6479 list_T *l2;
6480 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006485 /* We also quit the loop when we have inserted the original item count of
6486 * the list, avoid a hang when we extend a list with itself. */
6487 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6489 return FAIL;
6490 return OK;
6491}
6492
6493/*
6494 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6495 * Return FAIL when out of memory.
6496 */
6497 static int
6498list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l1;
6500 list_T *l2;
6501 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006505 if (l1 == NULL || l2 == NULL)
6506 return FAIL;
6507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (l == NULL)
6511 return FAIL;
6512 tv->v_type = VAR_LIST;
6513 tv->vval.v_list = l;
6514
6515 /* append all items from the second list */
6516 return list_extend(l, l2, NULL);
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Returns NULL when out of memory.
6524 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *copy;
6532 listitem_T *item;
6533 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 if (orig == NULL)
6536 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 copy = list_alloc();
6539 if (copy != NULL)
6540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (copyID != 0)
6542 {
6543 /* Do this before adding the items, because one of the items may
6544 * refer back to this list. */
6545 orig->lv_copyID = copyID;
6546 orig->lv_copylist = copy;
6547 }
6548 for (item = orig->lv_first; item != NULL && !got_int;
6549 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
6551 ni = listitem_alloc();
6552 if (ni == NULL)
6553 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6557 {
6558 vim_free(ni);
6559 break;
6560 }
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006563 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 list_append(copy, ni);
6565 }
6566 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (item != NULL)
6568 {
6569 list_unref(copy);
6570 copy = NULL;
6571 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 return copy;
6575}
6576
6577/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006580 * This used to be called list_remove, but that conflicts with a Sun header
6581 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006583 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006584vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *l;
6586 listitem_T *item;
6587 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588{
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 /* notify watchers */
6592 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006594 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 list_fix_watch(l, ip);
6596 if (ip == item2)
6597 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006599
6600 if (item2->li_next == NULL)
6601 l->lv_last = item->li_prev;
6602 else
6603 item2->li_next->li_prev = item->li_prev;
6604 if (item->li_prev == NULL)
6605 l->lv_first = item2->li_next;
6606 else
6607 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609}
6610
6611/*
6612 * Return an allocated string with the string representation of a list.
6613 * May return NULL.
6614 */
6615 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006618 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619{
6620 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621
6622 if (tv->vval.v_list == NULL)
6623 return NULL;
6624 ga_init2(&ga, (int)sizeof(char), 80);
6625 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 {
6628 vim_free(ga.ga_data);
6629 return NULL;
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 ga_append(&ga, ']');
6632 ga_append(&ga, NUL);
6633 return (char_u *)ga.ga_data;
6634}
6635
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006636typedef struct join_S {
6637 char_u *s;
6638 char_u *tofree;
6639} join_T;
6640
6641 static int
6642list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6643 garray_T *gap; /* to store the result in */
6644 list_T *l;
6645 char_u *sep;
6646 int echo_style;
6647 int copyID;
6648 garray_T *join_gap; /* to keep each list item string */
6649{
6650 int i;
6651 join_T *p;
6652 int len;
6653 int sumlen = 0;
6654 int first = TRUE;
6655 char_u *tofree;
6656 char_u numbuf[NUMBUFLEN];
6657 listitem_T *item;
6658 char_u *s;
6659
6660 /* Stringify each item in the list. */
6661 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6662 {
6663 if (echo_style)
6664 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6665 else
6666 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6667 if (s == NULL)
6668 return FAIL;
6669
6670 len = (int)STRLEN(s);
6671 sumlen += len;
6672
6673 ga_grow(join_gap, 1);
6674 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6675 if (tofree != NULL || s != numbuf)
6676 {
6677 p->s = s;
6678 p->tofree = tofree;
6679 }
6680 else
6681 {
6682 p->s = vim_strnsave(s, len);
6683 p->tofree = p->s;
6684 }
6685
6686 line_breakcheck();
6687 }
6688
6689 /* Allocate result buffer with its total size, avoid re-allocation and
6690 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6691 if (join_gap->ga_len >= 2)
6692 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6693 if (ga_grow(gap, sumlen + 2) == FAIL)
6694 return FAIL;
6695
6696 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6697 {
6698 if (first)
6699 first = FALSE;
6700 else
6701 ga_concat(gap, sep);
6702 p = ((join_T *)join_gap->ga_data) + i;
6703
6704 if (p->s != NULL)
6705 ga_concat(gap, p->s);
6706 line_breakcheck();
6707 }
6708
6709 return OK;
6710}
6711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006714 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006717 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006718list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006721 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006722 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006723 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 garray_T join_ga;
6726 int retval;
6727 join_T *p;
6728 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6731 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6732
6733 /* Dispose each item in join_ga. */
6734 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 p = (join_T *)join_ga.ga_data;
6737 for (i = 0; i < join_ga.ga_len; ++i)
6738 {
6739 vim_free(p->tofree);
6740 ++p;
6741 }
6742 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006744
6745 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746}
6747
6748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 * Garbage collection for lists and dictionaries.
6750 *
6751 * We use reference counts to be able to free most items right away when they
6752 * are no longer used. But for composite items it's possible that it becomes
6753 * unused while the reference count is > 0: When there is a recursive
6754 * reference. Example:
6755 * :let l = [1, 2, 3]
6756 * :let d = {9: l}
6757 * :let l[1] = d
6758 *
6759 * Since this is quite unusual we handle this with garbage collection: every
6760 * once in a while find out which lists and dicts are not referenced from any
6761 * variable.
6762 *
6763 * Here is a good reference text about garbage collection (refers to Python
6764 * but it applies to all reference-counting mechanisms):
6765 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767
6768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 * Do garbage collection for lists and dicts.
6770 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006771 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 int
6773garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006775 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 buf_T *buf;
6777 win_T *wp;
6778 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006779 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006780 int did_free;
6781 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 tabpage_T *tp;
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786 /* Only do this once. */
6787 want_garbage_collect = FALSE;
6788 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006789 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006791 /* We advance by two because we add one for items referenced through
6792 * previous_funccal. */
6793 current_copyID += COPYID_INC;
6794 copyID = current_copyID;
6795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /*
6797 * 1. Go through all accessible variables and mark all lists and dicts
6798 * with copyID.
6799 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800
6801 /* Don't free variables in the previous_funccal list unless they are only
6802 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006803 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006804 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6805 {
6806 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6807 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6808 }
6809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 /* script-local variables */
6811 for (i = 1; i <= ga_scripts.ga_len; ++i)
6812 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6813
6814 /* buffer-local variables */
6815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006816 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817
6818 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006820 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006821#ifdef FEAT_AUTOCMD
6822 if (aucmd_win != NULL)
6823 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006829 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830#endif
6831
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 /* global variables */
6833 set_ref_in_ht(&globvarht, copyID);
6834
6835 /* function-local variables */
6836 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6837 {
6838 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6839 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6840 }
6841
Bram Moolenaard812df62008-11-09 12:46:09 +00006842 /* v: vars */
6843 set_ref_in_ht(&vimvarht, copyID);
6844
Bram Moolenaar1dced572012-04-05 16:54:08 +02006845#ifdef FEAT_LUA
6846 set_ref_in_lua(copyID);
6847#endif
6848
Bram Moolenaardb913952012-06-29 12:54:53 +02006849#ifdef FEAT_PYTHON
6850 set_ref_in_python(copyID);
6851#endif
6852
6853#ifdef FEAT_PYTHON3
6854 set_ref_in_python3(copyID);
6855#endif
6856
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006857 /*
6858 * 2. Free lists and dictionaries that are not referenced.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 did_free = free_unref_items(copyID);
6861
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006862 /*
6863 * 3. Check if any funccal can be freed now.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 for (pfc = &previous_funccal; *pfc != NULL; )
6866 {
6867 if (can_free_funccal(*pfc, copyID))
6868 {
6869 fc = *pfc;
6870 *pfc = fc->caller;
6871 free_funccal(fc, TRUE);
6872 did_free = TRUE;
6873 did_free_funccal = TRUE;
6874 }
6875 else
6876 pfc = &(*pfc)->caller;
6877 }
6878 if (did_free_funccal)
6879 /* When a funccal was freed some more items might be garbage
6880 * collected, so run again. */
6881 (void)garbage_collect();
6882
6883 return did_free;
6884}
6885
6886/*
6887 * Free lists and dictionaries that are no longer referenced.
6888 */
6889 static int
6890free_unref_items(copyID)
6891 int copyID;
6892{
6893 dict_T *dd;
6894 list_T *ll;
6895 int did_free = FALSE;
6896
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 */
6900 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006901 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006903 /* Free the Dictionary and ordinary items it contains, but don't
6904 * recurse into Lists and Dictionaries, they will be in the list
6905 * of dicts or list of lists. */
6906 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 did_free = TRUE;
6908
6909 /* restart, next dict may also have been freed */
6910 dd = first_dict;
6911 }
6912 else
6913 dd = dd->dv_used_next;
6914
6915 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006916 * Go through the list of lists and free items without the copyID.
6917 * But don't free a list that has a watcher (used in a for loop), these
6918 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 */
6920 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6922 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006924 /* Free the List and ordinary items it contains, but don't recurse
6925 * into Lists and Dictionaries, they will be in the list of dicts
6926 * or list of lists. */
6927 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 did_free = TRUE;
6929
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006930 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 ll = first_list;
6932 }
6933 else
6934 ll = ll->lv_used_next;
6935
6936 return did_free;
6937}
6938
6939/*
6940 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6941 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006942 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943set_ref_in_ht(ht, copyID)
6944 hashtab_T *ht;
6945 int copyID;
6946{
6947 int todo;
6948 hashitem_T *hi;
6949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 for (hi = ht->ht_array; todo > 0; ++hi)
6952 if (!HASHITEM_EMPTY(hi))
6953 {
6954 --todo;
6955 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6956 }
6957}
6958
6959/*
6960 * Mark all lists and dicts referenced through list "l" with "copyID".
6961 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006962 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963set_ref_in_list(l, copyID)
6964 list_T *l;
6965 int copyID;
6966{
6967 listitem_T *li;
6968
6969 for (li = l->lv_first; li != NULL; li = li->li_next)
6970 set_ref_in_item(&li->li_tv, copyID);
6971}
6972
6973/*
6974 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006976 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977set_ref_in_item(tv, copyID)
6978 typval_T *tv;
6979 int copyID;
6980{
6981 dict_T *dd;
6982 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 switch (tv->v_type)
6985 {
6986 case VAR_DICT:
6987 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006988 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 /* Didn't see this dict yet. */
6991 dd->dv_copyID = copyID;
6992 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995
6996 case VAR_LIST:
6997 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006998 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /* Didn't see this list yet. */
7001 ll->lv_copyID = copyID;
7002 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Allocate an empty header for a dictionary.
7011 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013dict_alloc()
7014{
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007019 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007020 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 if (first_dict != NULL)
7022 first_dict->dv_used_prev = d;
7023 d->dv_used_next = first_dict;
7024 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007025 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007029 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034}
7035
7036/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007037 * Allocate an empty dict for a return value.
7038 * Returns OK or FAIL.
7039 */
7040 static int
7041rettv_dict_alloc(rettv)
7042 typval_T *rettv;
7043{
7044 dict_T *d = dict_alloc();
7045
7046 if (d == NULL)
7047 return FAIL;
7048
7049 rettv->vval.v_dict = d;
7050 rettv->v_type = VAR_DICT;
7051 ++d->dv_refcount;
7052 return OK;
7053}
7054
7055
7056/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057 * Unreference a Dictionary: decrement the reference count and free it when it
7058 * becomes zero.
7059 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007060 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007064 if (d != NULL && --d->dv_refcount <= 0)
7065 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066}
7067
7068/*
7069 * Free a Dictionary, including all items it contains.
7070 * Ignores the reference count.
7071 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007072 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073dict_free(d, recurse)
7074 dict_T *d;
7075 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007079 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /* Remove the dict from the list of dicts for garbage collection. */
7082 if (d->dv_used_prev == NULL)
7083 first_dict = d->dv_used_next;
7084 else
7085 d->dv_used_prev->dv_used_next = d->dv_used_next;
7086 if (d->dv_used_next != NULL)
7087 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7088
7089 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007090 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (!HASHITEM_EMPTY(hi))
7095 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007096 /* Remove the item before deleting it, just in case there is
7097 * something recursive causing trouble. */
7098 di = HI2DI(hi);
7099 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007100 if (recurse || (di->di_tv.v_type != VAR_LIST
7101 && di->di_tv.v_type != VAR_DICT))
7102 clear_tv(&di->di_tv);
7103 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 --todo;
7105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 vim_free(d);
7109}
7110
7111/*
7112 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * The "key" is copied to the new item.
7114 * Note that the value of the item "di_tv" still needs to be initialized!
7115 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007117 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118dictitem_alloc(key)
7119 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120{
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007123 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 di->di_flags = 0;
7128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130}
7131
7132/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 * Make a copy of a Dictionary item.
7134 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138{
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007141 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7142 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 if (di != NULL)
7144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007146 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 copy_tv(&org->di_tv, &di->di_tv);
7148 }
7149 return di;
7150}
7151
7152/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 * Remove item "item" from Dictionary "dict" and free it.
7154 */
7155 static void
7156dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *dict;
7158 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (HASHITEM_EMPTY(hi))
7164 EMSG2(_(e_intern2), "dictitem_remove()");
7165 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 dictitem_free(item);
7168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Free a dict item. Also clears the value.
7172 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007173 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 clear_tv(&item->di_tv);
7178 vim_free(item);
7179}
7180
7181/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7183 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 * Returns NULL when out of memory.
7186 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192{
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 dict_T *copy;
7194 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197
7198 if (orig == NULL)
7199 return NULL;
7200
7201 copy = dict_alloc();
7202 if (copy != NULL)
7203 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 if (copyID != 0)
7205 {
7206 orig->dv_copyID = copyID;
7207 orig->dv_copydict = copy;
7208 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007209 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007210 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 --todo;
7215
7216 di = dictitem_alloc(hi->hi_key);
7217 if (di == NULL)
7218 break;
7219 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 {
7221 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7222 copyID) == FAIL)
7223 {
7224 vim_free(di);
7225 break;
7226 }
7227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007228 else
7229 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7230 if (dict_add(copy, di) == FAIL)
7231 {
7232 dictitem_free(di);
7233 break;
7234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239 if (todo > 0)
7240 {
7241 dict_unref(copy);
7242 copy = NULL;
7243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
7245
7246 return copy;
7247}
7248
7249/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007251 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007253 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 dict_T *d;
7256 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259}
7260
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007262 * Add a number or string entry to dictionary "d".
7263 * When "str" is NULL use number "nr", otherwise use "str".
7264 * Returns FAIL when out of memory and when key already exists.
7265 */
7266 int
7267dict_add_nr_str(d, key, nr, str)
7268 dict_T *d;
7269 char *key;
7270 long nr;
7271 char_u *str;
7272{
7273 dictitem_T *item;
7274
7275 item = dictitem_alloc((char_u *)key);
7276 if (item == NULL)
7277 return FAIL;
7278 item->di_tv.v_lock = 0;
7279 if (str == NULL)
7280 {
7281 item->di_tv.v_type = VAR_NUMBER;
7282 item->di_tv.vval.v_number = nr;
7283 }
7284 else
7285 {
7286 item->di_tv.v_type = VAR_STRING;
7287 item->di_tv.vval.v_string = vim_strsave(str);
7288 }
7289 if (dict_add(d, item) == FAIL)
7290 {
7291 dictitem_free(item);
7292 return FAIL;
7293 }
7294 return OK;
7295}
7296
7297/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007298 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007299 * Returns FAIL when out of memory and when key already exists.
7300 */
7301 int
7302dict_add_list(d, key, list)
7303 dict_T *d;
7304 char *key;
7305 list_T *list;
7306{
7307 dictitem_T *item;
7308
7309 item = dictitem_alloc((char_u *)key);
7310 if (item == NULL)
7311 return FAIL;
7312 item->di_tv.v_lock = 0;
7313 item->di_tv.v_type = VAR_LIST;
7314 item->di_tv.vval.v_list = list;
7315 if (dict_add(d, item) == FAIL)
7316 {
7317 dictitem_free(item);
7318 return FAIL;
7319 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007320 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007321 return OK;
7322}
7323
7324/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 * Get the number of items in a Dictionary.
7326 */
7327 static long
7328dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 if (d == NULL)
7332 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334}
7335
7336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 * Find item "key[len]" in Dictionary "d".
7338 * If "len" is negative use strlen(key).
7339 * Returns NULL when not found.
7340 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007341 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 char_u *key;
7345 int len;
7346{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347#define AKEYLEN 200
7348 char_u buf[AKEYLEN];
7349 char_u *akey;
7350 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (len < 0)
7354 akey = key;
7355 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 tofree = akey = vim_strnsave(key, len);
7358 if (akey == NULL)
7359 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 else
7362 {
7363 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007364 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 akey = buf;
7366 }
7367
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 vim_free(tofree);
7370 if (HASHITEM_EMPTY(hi))
7371 return NULL;
7372 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373}
7374
7375/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007376 * Get a string item from a dictionary.
7377 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007378 * Returns NULL if the entry doesn't exist or out of memory.
7379 */
7380 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007382 dict_T *d;
7383 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007384 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007385{
7386 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007387 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007388
7389 di = dict_find(d, key, -1);
7390 if (di == NULL)
7391 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007392 s = get_tv_string(&di->di_tv);
7393 if (save && s != NULL)
7394 s = vim_strsave(s);
7395 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007396}
7397
7398/*
7399 * Get a number item from a dictionary.
7400 * Returns 0 if the entry doesn't exist or out of memory.
7401 */
7402 long
7403get_dict_number(d, key)
7404 dict_T *d;
7405 char_u *key;
7406{
7407 dictitem_T *di;
7408
7409 di = dict_find(d, key, -1);
7410 if (di == NULL)
7411 return 0;
7412 return get_tv_number(&di->di_tv);
7413}
7414
7415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 * Return an allocated string with the string representation of a Dictionary.
7417 * May return NULL.
7418 */
7419 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007422 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423{
7424 garray_T ga;
7425 int first = TRUE;
7426 char_u *tofree;
7427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 return NULL;
7435 ga_init2(&ga, (int)sizeof(char), 80);
7436 ga_append(&ga, '{');
7437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007439 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007443 --todo;
7444
7445 if (first)
7446 first = FALSE;
7447 else
7448 ga_concat(&ga, (char_u *)", ");
7449
7450 tofree = string_quote(hi->hi_key, FALSE);
7451 if (tofree != NULL)
7452 {
7453 ga_concat(&ga, tofree);
7454 vim_free(tofree);
7455 }
7456 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 if (s != NULL)
7459 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 if (s == NULL)
7462 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007465 if (todo > 0)
7466 {
7467 vim_free(ga.ga_data);
7468 return NULL;
7469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 ga_append(&ga, '}');
7472 ga_append(&ga, NUL);
7473 return (char_u *)ga.ga_data;
7474}
7475
7476/*
7477 * Allocate a variable for a Dictionary and fill it from "*arg".
7478 * Return OK or FAIL. Returns NOTDONE for {expr}.
7479 */
7480 static int
7481get_dict_tv(arg, rettv, evaluate)
7482 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 int evaluate;
7485{
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dict_T *d = NULL;
7487 typval_T tvkey;
7488 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007489 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
7494 /*
7495 * First check if it's not a curly-braces thing: {expr}.
7496 * Must do this without evaluating, otherwise a function may be called
7497 * twice. Unfortunately this means we need to call eval1() twice for the
7498 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 if (*start != '}')
7502 {
7503 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7504 return FAIL;
7505 if (*start == '}')
7506 return NOTDONE;
7507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 if (evaluate)
7510 {
7511 d = dict_alloc();
7512 if (d == NULL)
7513 return FAIL;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 tvkey.v_type = VAR_UNKNOWN;
7516 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517
7518 *arg = skipwhite(*arg + 1);
7519 while (**arg != '}' && **arg != NUL)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 goto failret;
7523 if (**arg != ':')
7524 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007525 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 goto failret;
7528 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007531 key = get_tv_string_buf_chk(&tvkey, buf);
7532 if (key == NULL || *key == NUL)
7533 {
7534 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7535 if (key != NULL)
7536 EMSG(_(e_emptykey));
7537 clear_tv(&tvkey);
7538 goto failret;
7539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541
7542 *arg = skipwhite(*arg + 1);
7543 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7544 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007545 if (evaluate)
7546 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 goto failret;
7548 }
7549 if (evaluate)
7550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 if (item != NULL)
7553 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007554 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 clear_tv(&tv);
7557 goto failret;
7558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 item = dictitem_alloc(key);
7560 clear_tv(&tvkey);
7561 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007564 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 if (dict_add(d, item) == FAIL)
7566 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 }
7568 }
7569
7570 if (**arg == '}')
7571 break;
7572 if (**arg != ',')
7573 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007574 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 goto failret;
7576 }
7577 *arg = skipwhite(*arg + 1);
7578 }
7579
7580 if (**arg != '}')
7581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007582 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583failret:
7584 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007585 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 return FAIL;
7587 }
7588
7589 *arg = skipwhite(*arg + 1);
7590 if (evaluate)
7591 {
7592 rettv->v_type = VAR_DICT;
7593 rettv->vval.v_dict = d;
7594 ++d->dv_refcount;
7595 }
7596
7597 return OK;
7598}
7599
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 * Return a string with the string representation of a variable.
7602 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007603 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007604 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007606 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 */
7608 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007612 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 static int recurse = 0;
7616 char_u *r = NULL;
7617
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007620 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 *tofree = NULL;
7622 return NULL;
7623 }
7624 ++recurse;
7625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 switch (tv->v_type)
7627 {
7628 case VAR_FUNC:
7629 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 r = tv->vval.v_string;
7631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634 if (tv->vval.v_list == NULL)
7635 {
7636 *tofree = NULL;
7637 r = NULL;
7638 }
7639 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7640 {
7641 *tofree = NULL;
7642 r = (char_u *)"[...]";
7643 }
7644 else
7645 {
7646 tv->vval.v_list->lv_copyID = copyID;
7647 *tofree = list2string(tv, copyID);
7648 r = *tofree;
7649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 if (tv->vval.v_dict == NULL)
7654 {
7655 *tofree = NULL;
7656 r = NULL;
7657 }
7658 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7659 {
7660 *tofree = NULL;
7661 r = (char_u *)"{...}";
7662 }
7663 else
7664 {
7665 tv->vval.v_dict->dv_copyID = copyID;
7666 *tofree = dict2string(tv, copyID);
7667 r = *tofree;
7668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 case VAR_STRING:
7672 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 *tofree = NULL;
7674 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007677#ifdef FEAT_FLOAT
7678 case VAR_FLOAT:
7679 *tofree = NULL;
7680 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7681 r = numbuf;
7682 break;
7683#endif
7684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689
7690 --recurse;
7691 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692}
7693
7694/*
7695 * Return a string with the string representation of a variable.
7696 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7697 * "numbuf" is used for a number.
7698 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007699 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 */
7701 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007702tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 char_u **tofree;
7705 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007706 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007707{
7708 switch (tv->v_type)
7709 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 case VAR_FUNC:
7711 *tofree = string_quote(tv->vval.v_string, TRUE);
7712 return *tofree;
7713 case VAR_STRING:
7714 *tofree = string_quote(tv->vval.v_string, FALSE);
7715 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
7717 case VAR_FLOAT:
7718 *tofree = NULL;
7719 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7720 return numbuf;
7721#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007729 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730}
7731
7732/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 * Return string "str" in ' quotes, doubling ' characters.
7734 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 */
7737 static char_u *
7738string_quote(str, function)
7739 char_u *str;
7740 int function;
7741{
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 char_u *p, *r, *s;
7744
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 len = (function ? 13 : 3);
7746 if (str != NULL)
7747 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007748 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 for (p = str; *p != NUL; mb_ptr_adv(p))
7750 if (*p == '\'')
7751 ++len;
7752 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 s = r = alloc(len);
7754 if (r != NULL)
7755 {
7756 if (function)
7757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 r += 10;
7760 }
7761 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 if (str != NULL)
7764 for (p = str; *p != NUL; )
7765 {
7766 if (*p == '\'')
7767 *r++ = '\'';
7768 MB_COPY_CHAR(p, r);
7769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 if (function)
7772 *r++ = ')';
7773 *r++ = NUL;
7774 }
7775 return s;
7776}
7777
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778#ifdef FEAT_FLOAT
7779/*
7780 * Convert the string "text" to a floating point number.
7781 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7782 * this always uses a decimal point.
7783 * Returns the length of the text that was consumed.
7784 */
7785 static int
7786string2float(text, value)
7787 char_u *text;
7788 float_T *value; /* result stored here */
7789{
7790 char *s = (char *)text;
7791 float_T f;
7792
7793 f = strtod(s, &s);
7794 *value = f;
7795 return (int)((char_u *)s - text);
7796}
7797#endif
7798
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 * Get the value of an environment variable.
7801 * "arg" is pointing to the '$'. It is advanced to after the name.
7802 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007803 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 */
7805 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 int evaluate;
7810{
7811 char_u *string = NULL;
7812 int len;
7813 int cc;
7814 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007815 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 ++*arg;
7818 name = *arg;
7819 len = get_env_len(arg);
7820 if (evaluate)
7821 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007822 if (len == 0)
7823 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007825 cc = name[len];
7826 name[len] = NUL;
7827 /* first try vim_getenv(), fast for normal environment vars */
7828 string = vim_getenv(name, &mustfree);
7829 if (string != NULL && *string != NUL)
7830 {
7831 if (!mustfree)
7832 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007834 else
7835 {
7836 if (mustfree)
7837 vim_free(string);
7838
7839 /* next try expanding things like $VIM and ${HOME} */
7840 string = expand_env_save(name - 1);
7841 if (string != NULL && *string == '$')
7842 {
7843 vim_free(string);
7844 string = NULL;
7845 }
7846 }
7847 name[len] = cc;
7848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->v_type = VAR_STRING;
7850 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
7853 return OK;
7854}
7855
7856/*
7857 * Array with names and number of arguments of all internal functions
7858 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7859 */
7860static struct fst
7861{
7862 char *f_name; /* function name */
7863 char f_min_argc; /* minimal number of arguments */
7864 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007866 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867} functions[] =
7868{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#ifdef FEAT_FLOAT
7870 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007871 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007873 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007874 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"append", 2, 2, f_append},
7876 {"argc", 0, 0, f_argc},
7877 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007878 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007885 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"bufexists", 1, 1, f_bufexists},
7887 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7888 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7889 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7890 {"buflisted", 1, 1, f_buflisted},
7891 {"bufloaded", 1, 1, f_bufloaded},
7892 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007893 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"bufwinnr", 1, 1, f_bufwinnr},
7895 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007896 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007897 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
7900 {"ceil", 1, 1, f_ceil},
7901#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007902 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007903 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007905 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007907#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007908 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007909 {"complete_add", 1, 1, f_complete_add},
7910 {"complete_check", 0, 0, f_complete_check},
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#ifdef FEAT_FLOAT
7915 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007916 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007920 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007921 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"delete", 1, 1, f_delete},
7923 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007924 {"diff_filler", 1, 1, f_diff_filler},
7925 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007926 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"eventhandler", 0, 0, f_eventhandler},
7930 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007931 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007933#ifdef FEAT_FLOAT
7934 {"exp", 1, 1, f_exp},
7935#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007936 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007938 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7940 {"filereadable", 1, 1, f_filereadable},
7941 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007943 {"finddir", 1, 3, f_finddir},
7944 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
7946 {"float2nr", 1, 1, f_float2nr},
7947 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007948 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007950 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"fnamemodify", 2, 2, f_fnamemodify},
7952 {"foldclosed", 1, 1, f_foldclosed},
7953 {"foldclosedend", 1, 1, f_foldclosedend},
7954 {"foldlevel", 1, 1, f_foldlevel},
7955 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007956 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007959 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007961 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007962 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"getchar", 0, 1, f_getchar},
7964 {"getcharmod", 0, 0, f_getcharmod},
7965 {"getcmdline", 0, 0, f_getcmdline},
7966 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007967 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007969 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"getfsize", 1, 1, f_getfsize},
7972 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007973 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007974 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007975 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007976 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007977 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007978 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007979 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007980 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007982 {"gettabvar", 2, 3, f_gettabvar},
7983 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"getwinposx", 0, 0, f_getwinposx},
7985 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007986 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007987 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02007988 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007991 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7994 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7995 {"histadd", 2, 2, f_histadd},
7996 {"histdel", 1, 2, f_histdel},
7997 {"histget", 1, 2, f_histget},
7998 {"histnr", 1, 1, f_histnr},
7999 {"hlID", 1, 1, f_hlID},
8000 {"hlexists", 1, 1, f_hlexists},
8001 {"hostname", 0, 0, f_hostname},
8002 {"iconv", 3, 3, f_iconv},
8003 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008005 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008007 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"inputrestore", 0, 0, f_inputrestore},
8009 {"inputsave", 0, 0, f_inputsave},
8010 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008011 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008012 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008014 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"libcall", 3, 3, f_libcall},
8021 {"libcallnr", 3, 3, f_libcallnr},
8022 {"line", 1, 1, f_line},
8023 {"line2byte", 1, 1, f_line2byte},
8024 {"lispindent", 1, 1, f_lispindent},
8025 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008027 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028 {"log10", 1, 1, f_log10},
8029#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008030#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008031 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008032#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008033 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008034 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008035 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008036 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008037 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008038 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008039 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008040 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008041 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008042 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008043 {"max", 1, 1, f_max},
8044 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008045#ifdef vim_mkdir
8046 {"mkdir", 1, 3, f_mkdir},
8047#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008049#ifdef FEAT_MZSCHEME
8050 {"mzeval", 1, 1, f_mzeval},
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008053 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008054 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008055 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008056#ifdef FEAT_FLOAT
8057 {"pow", 2, 2, f_pow},
8058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008060 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008061 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008062#ifdef FEAT_PYTHON3
8063 {"py3eval", 1, 1, f_py3eval},
8064#endif
8065#ifdef FEAT_PYTHON
8066 {"pyeval", 1, 1, f_pyeval},
8067#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008069 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008070 {"reltime", 0, 2, f_reltime},
8071 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"remote_expr", 2, 3, f_remote_expr},
8073 {"remote_foreground", 1, 1, f_remote_foreground},
8074 {"remote_peek", 1, 2, f_remote_peek},
8075 {"remote_read", 1, 1, f_remote_read},
8076 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008079 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008081 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"round", 1, 1, f_round},
8084#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008085 {"screenattr", 2, 2, f_screenattr},
8086 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008087 {"screencol", 0, 0, f_screencol},
8088 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008089 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008090 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008091 {"searchpair", 3, 7, f_searchpair},
8092 {"searchpairpos", 3, 7, f_searchpairpos},
8093 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"server2client", 2, 2, f_server2client},
8095 {"serverlist", 0, 0, f_serverlist},
8096 {"setbufvar", 3, 3, f_setbufvar},
8097 {"setcmdpos", 1, 1, f_setcmdpos},
8098 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008099 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008100 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008101 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008102 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008104 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008105 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008107#ifdef FEAT_CRYPT
8108 {"sha256", 1, 1, f_sha256},
8109#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008110 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008111 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008115 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008117 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008118 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008119 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008120 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008121 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"sqrt", 1, 1, f_sqrt},
8124 {"str2float", 1, 1, f_str2float},
8125#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008126 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008127 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008128 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129#ifdef HAVE_STRFTIME
8130 {"strftime", 1, 2, f_strftime},
8131#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strlen", 1, 1, f_strlen},
8135 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008136 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008138 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008139 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"substitute", 4, 4, f_substitute},
8141 {"synID", 3, 3, f_synID},
8142 {"synIDattr", 2, 3, f_synIDattr},
8143 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008144 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008145 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008146 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008147 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008148 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008149 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008150 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008151 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008152 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153#ifdef FEAT_FLOAT
8154 {"tan", 1, 1, f_tan},
8155 {"tanh", 1, 1, f_tanh},
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008158 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"tolower", 1, 1, f_tolower},
8160 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008161 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#ifdef FEAT_FLOAT
8163 {"trunc", 1, 1, f_trunc},
8164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008166 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008167 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008168 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008169 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"virtcol", 1, 1, f_virtcol},
8171 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008172 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"winbufnr", 1, 1, f_winbufnr},
8174 {"wincol", 0, 0, f_wincol},
8175 {"winheight", 1, 1, f_winheight},
8176 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008177 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008179 {"winrestview", 1, 1, f_winrestview},
8180 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008182 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008183 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184};
8185
8186#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8187
8188/*
8189 * Function given to ExpandGeneric() to obtain the list of internal
8190 * or user defined function names.
8191 */
8192 char_u *
8193get_function_name(xp, idx)
8194 expand_T *xp;
8195 int idx;
8196{
8197 static int intidx = -1;
8198 char_u *name;
8199
8200 if (idx == 0)
8201 intidx = -1;
8202 if (intidx < 0)
8203 {
8204 name = get_user_func_name(xp, idx);
8205 if (name != NULL)
8206 return name;
8207 }
8208 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8209 {
8210 STRCPY(IObuff, functions[intidx].f_name);
8211 STRCAT(IObuff, "(");
8212 if (functions[intidx].f_max_argc == 0)
8213 STRCAT(IObuff, ")");
8214 return IObuff;
8215 }
8216
8217 return NULL;
8218}
8219
8220/*
8221 * Function given to ExpandGeneric() to obtain the list of internal or
8222 * user defined variable or function names.
8223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 char_u *
8225get_expr_name(xp, idx)
8226 expand_T *xp;
8227 int idx;
8228{
8229 static int intidx = -1;
8230 char_u *name;
8231
8232 if (idx == 0)
8233 intidx = -1;
8234 if (intidx < 0)
8235 {
8236 name = get_function_name(xp, idx);
8237 if (name != NULL)
8238 return name;
8239 }
8240 return get_user_var_name(xp, ++intidx);
8241}
8242
8243#endif /* FEAT_CMDL_COMPL */
8244
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008245#if defined(EBCDIC) || defined(PROTO)
8246/*
8247 * Compare struct fst by function name.
8248 */
8249 static int
8250compare_func_name(s1, s2)
8251 const void *s1;
8252 const void *s2;
8253{
8254 struct fst *p1 = (struct fst *)s1;
8255 struct fst *p2 = (struct fst *)s2;
8256
8257 return STRCMP(p1->f_name, p2->f_name);
8258}
8259
8260/*
8261 * Sort the function table by function name.
8262 * The sorting of the table above is ASCII dependant.
8263 * On machines using EBCDIC we have to sort it.
8264 */
8265 static void
8266sortFunctions()
8267{
8268 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269
8270 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8271}
8272#endif
8273
8274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275/*
8276 * Find internal function in table above.
8277 * Return index, or -1 if not found
8278 */
8279 static int
8280find_internal_func(name)
8281 char_u *name; /* name of the function */
8282{
8283 int first = 0;
8284 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8285 int cmp;
8286 int x;
8287
8288 /*
8289 * Find the function name in the table. Binary search.
8290 */
8291 while (first <= last)
8292 {
8293 x = first + ((unsigned)(last - first) >> 1);
8294 cmp = STRCMP(name, functions[x].f_name);
8295 if (cmp < 0)
8296 last = x - 1;
8297 else if (cmp > 0)
8298 first = x + 1;
8299 else
8300 return x;
8301 }
8302 return -1;
8303}
8304
8305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8307 * name it contains, otherwise return "name".
8308 */
8309 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 char_u *name;
8312 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008313 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314{
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 int cc;
8317
8318 cc = name[*lenp];
8319 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008320 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 {
8326 *lenp = 0;
8327 return (char_u *)""; /* just in case */
8328 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008329 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 }
8332
8333 return name;
8334}
8335
8336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 * Allocate a variable for the result of a function.
8338 * Return OK or FAIL.
8339 */
8340 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8342 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u *name; /* name of the function */
8344 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 char_u **arg; /* argument, pointing to the '(' */
8347 linenr_T firstline; /* first line of range */
8348 linenr_T lastline; /* last line of range */
8349 int *doesrange; /* return: function handled range */
8350 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 char_u *argp;
8354 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008355 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 int argcount = 0; /* number of arguments found */
8357
8358 /*
8359 * Get the arguments.
8360 */
8361 argp = *arg;
8362 while (argcount < MAX_FUNC_ARGS)
8363 {
8364 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8365 if (*argp == ')' || *argp == ',' || *argp == NUL)
8366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8368 {
8369 ret = FAIL;
8370 break;
8371 }
8372 ++argcount;
8373 if (*argp != ',')
8374 break;
8375 }
8376 if (*argp == ')')
8377 ++argp;
8378 else
8379 ret = FAIL;
8380
8381 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 {
8386 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008387 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008389 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 *arg = skipwhite(argp);
8396 return ret;
8397}
8398
8399
8400/*
8401 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008402 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008403 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 */
8405 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008406call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008407 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008408 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008412 typval_T *argvars; /* vars for arguments, must have "argcount"
8413 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 linenr_T firstline; /* first line of range */
8415 linenr_T lastline; /* last line of range */
8416 int *doesrange; /* return: function handled range */
8417 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#define ERROR_UNKNOWN 0
8422#define ERROR_TOOMANY 1
8423#define ERROR_TOOFEW 2
8424#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425#define ERROR_DICT 4
8426#define ERROR_NONE 5
8427#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int error = ERROR_NONE;
8429 int i;
8430 int llen;
8431 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#define FLEN_FIXED 40
8433 char_u fname_buf[FLEN_FIXED + 1];
8434 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008435 char_u *name;
8436
8437 /* Make a copy of the name, if it comes from a funcref variable it could
8438 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008439 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008440 if (name == NULL)
8441 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
8443 /*
8444 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8445 * Change <SNR>123_name() to K_SNR 123_name().
8446 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 llen = eval_fname_script(name);
8449 if (llen > 0)
8450 {
8451 fname_buf[0] = K_SPECIAL;
8452 fname_buf[1] = KS_EXTRA;
8453 fname_buf[2] = (int)KE_SNR;
8454 i = 3;
8455 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8456 {
8457 if (current_SID <= 0)
8458 error = ERROR_SCRIPT;
8459 else
8460 {
8461 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8462 i = (int)STRLEN(fname_buf);
8463 }
8464 }
8465 if (i + STRLEN(name + llen) < FLEN_FIXED)
8466 {
8467 STRCPY(fname_buf + i, name + llen);
8468 fname = fname_buf;
8469 }
8470 else
8471 {
8472 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8473 if (fname == NULL)
8474 error = ERROR_OTHER;
8475 else
8476 {
8477 mch_memmove(fname, fname_buf, (size_t)i);
8478 STRCPY(fname + i, name + llen);
8479 }
8480 }
8481 }
8482 else
8483 fname = name;
8484
8485 *doesrange = FALSE;
8486
8487
8488 /* execute the function if no errors detected and executing */
8489 if (evaluate && error == ERROR_NONE)
8490 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008491 char_u *rfname = fname;
8492
8493 /* Ignore "g:" before a function name. */
8494 if (fname[0] == 'g' && fname[1] == ':')
8495 rfname = fname + 2;
8496
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008497 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 error = ERROR_UNKNOWN;
8500
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008501 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {
8503 /*
8504 * User defined function.
8505 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008506 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008509 /* Trigger FuncUndefined event, may load the function. */
8510 if (fp == NULL
8511 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008512 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008513 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008515 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008516 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008519 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008520 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008521 {
8522 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008523 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008524 }
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (fp != NULL)
8527 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008528 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008530 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 else
8537 {
8538 /*
8539 * Call the user function.
8540 * Save and restore search patterns, script variables and
8541 * redo buffer.
8542 */
8543 save_search_patterns();
8544 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008545 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008548 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8549 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8550 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008551 /* Function was unreferenced while being used, free it
8552 * now. */
8553 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 restoreRedobuff();
8555 restore_search_patterns();
8556 error = ERROR_NONE;
8557 }
8558 }
8559 }
8560 else
8561 {
8562 /*
8563 * Find the function name in the table, call its implementation.
8564 */
8565 i = find_internal_func(fname);
8566 if (i >= 0)
8567 {
8568 if (argcount < functions[i].f_min_argc)
8569 error = ERROR_TOOFEW;
8570 else if (argcount > functions[i].f_max_argc)
8571 error = ERROR_TOOMANY;
8572 else
8573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 error = ERROR_NONE;
8577 }
8578 }
8579 }
8580 /*
8581 * The function call (or "FuncUndefined" autocommand sequence) might
8582 * have been aborted by an error, an interrupt, or an explicitly thrown
8583 * exception that has not been caught so far. This situation can be
8584 * tested for by calling aborting(). For an error in an internal
8585 * function or for the "E132" error in call_user_func(), however, the
8586 * throw point at which the "force_abort" flag (temporarily reset by
8587 * emsg()) is normally updated has not been reached yet. We need to
8588 * update that flag first to make aborting() reliable.
8589 */
8590 update_force_abort();
8591 }
8592 if (error == ERROR_NONE)
8593 ret = OK;
8594
8595 /*
8596 * Report an error unless the argument evaluation or function call has been
8597 * cancelled due to an aborting error, an interrupt, or an exception.
8598 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 if (!aborting())
8600 {
8601 switch (error)
8602 {
8603 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008605 break;
8606 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008607 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008608 break;
8609 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008610 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008611 name);
8612 break;
8613 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008614 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008615 name);
8616 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008618 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 name);
8620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008621 }
8622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (fname != name && fname != fname_buf)
8625 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008626 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 return ret;
8629}
8630
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008631/*
8632 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008633 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008634 */
8635 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008636emsg_funcname(ermsg, name)
8637 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008638 char_u *name;
8639{
8640 char_u *p;
8641
8642 if (*name == K_SPECIAL)
8643 p = concat_str((char_u *)"<SNR>", name + 3);
8644 else
8645 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008646 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008647 if (p != name)
8648 vim_free(p);
8649}
8650
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008651/*
8652 * Return TRUE for a non-zero Number and a non-empty String.
8653 */
8654 static int
8655non_zero_arg(argvars)
8656 typval_T *argvars;
8657{
8658 return ((argvars[0].v_type == VAR_NUMBER
8659 && argvars[0].vval.v_number != 0)
8660 || (argvars[0].v_type == VAR_STRING
8661 && argvars[0].vval.v_string != NULL
8662 && *argvars[0].vval.v_string != NUL));
8663}
8664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665/*********************************************
8666 * Implementation of the built-in functions
8667 */
8668
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008669#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8671
8672/*
8673 * Get the float value of "argvars[0]" into "f".
8674 * Returns FAIL when the argument is not a Number or Float.
8675 */
8676 static int
8677get_float_arg(argvars, f)
8678 typval_T *argvars;
8679 float_T *f;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 *f = argvars[0].vval.v_float;
8684 return OK;
8685 }
8686 if (argvars[0].v_type == VAR_NUMBER)
8687 {
8688 *f = (float_T)argvars[0].vval.v_number;
8689 return OK;
8690 }
8691 EMSG(_("E808: Number or Float required"));
8692 return FAIL;
8693}
8694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695/*
8696 * "abs(expr)" function
8697 */
8698 static void
8699f_abs(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 if (argvars[0].v_type == VAR_FLOAT)
8704 {
8705 rettv->v_type = VAR_FLOAT;
8706 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8707 }
8708 else
8709 {
8710 varnumber_T n;
8711 int error = FALSE;
8712
8713 n = get_tv_number_chk(&argvars[0], &error);
8714 if (error)
8715 rettv->vval.v_number = -1;
8716 else if (n > 0)
8717 rettv->vval.v_number = n;
8718 else
8719 rettv->vval.v_number = -n;
8720 }
8721}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008722
8723/*
8724 * "acos()" function
8725 */
8726 static void
8727f_acos(argvars, rettv)
8728 typval_T *argvars;
8729 typval_T *rettv;
8730{
8731 float_T f;
8732
8733 rettv->v_type = VAR_FLOAT;
8734 if (get_float_arg(argvars, &f) == OK)
8735 rettv->vval.v_float = acos(f);
8736 else
8737 rettv->vval.v_float = 0.0;
8738}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008739#endif
8740
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 */
8744 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008754 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008755 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008756 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008758 }
8759 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 EMSG(_(e_listreq));
8761}
8762
8763/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008764 * "and(expr, expr)" function
8765 */
8766 static void
8767f_and(argvars, rettv)
8768 typval_T *argvars;
8769 typval_T *rettv;
8770{
8771 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8772 & get_tv_number_chk(&argvars[1], NULL);
8773}
8774
8775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 * "append(lnum, string/list)" function
8777 */
8778 static void
8779f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 typval_T *argvars;
8781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008782{
8783 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 list_T *l = NULL;
8786 listitem_T *li = NULL;
8787 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 long added = 0;
8789
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008790 /* When coming here from Insert mode, sync undo, so that this can be
8791 * undone separately from what was previously inserted. */
8792 if (u_sync_once == 2)
8793 {
8794 u_sync_once = 1; /* notify that u_sync() was called */
8795 u_sync(TRUE);
8796 }
8797
Bram Moolenaar0d660222005-01-07 21:51:51 +00008798 lnum = get_tv_lnum(argvars);
8799 if (lnum >= 0
8800 && lnum <= curbuf->b_ml.ml_line_count
8801 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805 l = argvars[1].vval.v_list;
8806 if (l == NULL)
8807 return;
8808 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008810 for (;;)
8811 {
8812 if (l == NULL)
8813 tv = &argvars[1]; /* append a string */
8814 else if (li == NULL)
8815 break; /* end of list */
8816 else
8817 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 line = get_tv_string_chk(tv);
8819 if (line == NULL) /* type error */
8820 {
8821 rettv->vval.v_number = 1; /* Failed */
8822 break;
8823 }
8824 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008825 ++added;
8826 if (l == NULL)
8827 break;
8828 li = li->li_next;
8829 }
8830
8831 appended_lines_mark(lnum, added);
8832 if (curwin->w_cursor.lnum > lnum)
8833 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 else
8836 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argc()" function
8841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008844 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
8850/*
8851 * "argidx()" function
8852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "argv(nr)" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 int idx;
8870
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008871 if (argvars[0].v_type != VAR_UNKNOWN)
8872 {
8873 idx = get_tv_number_chk(&argvars[0], NULL);
8874 if (idx >= 0 && idx < ARGCOUNT)
8875 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8876 else
8877 rettv->vval.v_string = NULL;
8878 rettv->v_type = VAR_STRING;
8879 }
8880 else if (rettv_list_alloc(rettv) == OK)
8881 for (idx = 0; idx < ARGCOUNT; ++idx)
8882 list_append_string(rettv->vval.v_list,
8883 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008888 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008890 static void
8891f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008892 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008893 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008895 float_T f;
8896
8897 rettv->v_type = VAR_FLOAT;
8898 if (get_float_arg(argvars, &f) == OK)
8899 rettv->vval.v_float = asin(f);
8900 else
8901 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008902}
8903
8904/*
8905 * "atan()" function
8906 */
8907 static void
8908f_atan(argvars, rettv)
8909 typval_T *argvars;
8910 typval_T *rettv;
8911{
8912 float_T f;
8913
8914 rettv->v_type = VAR_FLOAT;
8915 if (get_float_arg(argvars, &f) == OK)
8916 rettv->vval.v_float = atan(f);
8917 else
8918 rettv->vval.v_float = 0.0;
8919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920
8921/*
8922 * "atan2()" function
8923 */
8924 static void
8925f_atan2(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 float_T fx, fy;
8930
8931 rettv->v_type = VAR_FLOAT;
8932 if (get_float_arg(argvars, &fx) == OK
8933 && get_float_arg(&argvars[1], &fy) == OK)
8934 rettv->vval.v_float = atan2(fx, fy);
8935 else
8936 rettv->vval.v_float = 0.0;
8937}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#endif
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
8941 * "browse(save, title, initdir, default)" function
8942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008945 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948#ifdef FEAT_BROWSE
8949 int save;
8950 char_u *title;
8951 char_u *initdir;
8952 char_u *defname;
8953 char_u buf[NUMBUFLEN];
8954 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 save = get_tv_number_chk(&argvars[0], &error);
8958 title = get_tv_string_chk(&argvars[1]);
8959 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8960 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 if (error || title == NULL || initdir == NULL || defname == NULL)
8963 rettv->vval.v_string = NULL;
8964 else
8965 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966 do_browse(save ? BROWSE_SAVE : 0,
8967 title, defname, NULL, initdir, NULL, curbuf);
8968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972}
8973
8974/*
8975 * "browsedir(title, initdir)" function
8976 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008981{
8982#ifdef FEAT_BROWSE
8983 char_u *title;
8984 char_u *initdir;
8985 char_u buf[NUMBUFLEN];
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 title = get_tv_string_chk(&argvars[0]);
8988 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (title == NULL || initdir == NULL)
8991 rettv->vval.v_string = NULL;
8992 else
8993 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008994 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
Bram Moolenaar33570922005-01-25 22:26:29 +00009001static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*
9004 * Find a buffer by number or exact name.
9005 */
9006 static buf_T *
9007find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 if (avar->v_type == VAR_NUMBER)
9013 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009014 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009017 if (buf == NULL)
9018 {
9019 /* No full path name match, try a match with a URL or a "nofile"
9020 * buffer, these don't use the full path. */
9021 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9022 if (buf->b_fname != NULL
9023 && (path_with_url(buf->b_fname)
9024#ifdef FEAT_QUICKFIX
9025 || bt_nofile(buf)
9026#endif
9027 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009029 break;
9030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 return buf;
9033}
9034
9035/*
9036 * "bufexists(expr)" function
9037 */
9038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "buflisted(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
9056 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * "bufloaded(expr)" function
9062 */
9063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *argvars;
9066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 buf_T *buf;
9069
9070 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009074static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*
9077 * Get buffer by number or pattern.
9078 */
9079 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009080get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009082 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 int save_magic;
9086 char_u *save_cpo;
9087 buf_T *buf;
9088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 if (tv->v_type == VAR_NUMBER)
9090 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009091 if (tv->v_type != VAR_STRING)
9092 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 if (name == NULL || *name == NUL)
9094 return curbuf;
9095 if (name[0] == '$' && name[1] == NUL)
9096 return lastbuf;
9097
9098 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9099 save_magic = p_magic;
9100 p_magic = TRUE;
9101 save_cpo = p_cpo;
9102 p_cpo = (char_u *)"";
9103
9104 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009105 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106
9107 p_magic = save_magic;
9108 p_cpo = save_cpo;
9109
9110 /* If not found, try expanding the name, like done for bufexists(). */
9111 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 return buf;
9115}
9116
9117/*
9118 * "bufname(expr)" function
9119 */
9120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T *argvars;
9123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 buf_T *buf;
9126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009129 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 --emsg_off;
9136}
9137
9138/*
9139 * "bufnr(expr)" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009147 int error = FALSE;
9148 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009152 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009153 --emsg_off;
9154
9155 /* If the buffer isn't found and the second argument is not zero create a
9156 * new buffer. */
9157 if (buf == NULL
9158 && argvars[1].v_type != VAR_UNKNOWN
9159 && get_tv_number_chk(&argvars[1], &error) != 0
9160 && !error
9161 && (name = get_tv_string_chk(&argvars[0])) != NULL
9162 && !error)
9163 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171/*
9172 * "bufwinnr(nr)" function
9173 */
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179#ifdef FEAT_WINDOWS
9180 win_T *wp;
9181 int winnr = 0;
9182#endif
9183 buf_T *buf;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009187 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_WINDOWS
9189 for (wp = firstwin; wp; wp = wp->w_next)
9190 {
9191 ++winnr;
9192 if (wp->w_buffer == buf)
9193 break;
9194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#endif
9199 --emsg_off;
9200}
9201
9202/*
9203 * "byte2line(byte)" function
9204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#else
9213 long boff = 0;
9214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 (linenr_T)0, &boff);
9221#endif
9222}
9223
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009224 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009225byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009228 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009229{
9230#ifdef FEAT_MBYTE
9231 char_u *t;
9232#endif
9233 char_u *str;
9234 long idx;
9235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 str = get_tv_string_chk(&argvars[0]);
9237 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240 return;
9241
9242#ifdef FEAT_MBYTE
9243 t = str;
9244 for ( ; idx > 0; idx--)
9245 {
9246 if (*t == NUL) /* EOL reached */
9247 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009248 if (enc_utf8 && comp)
9249 t += utf_ptr2len(t);
9250 else
9251 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009252 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009253 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009255 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009257#endif
9258}
9259
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009260/*
9261 * "byteidx()" function
9262 */
9263 static void
9264f_byteidx(argvars, rettv)
9265 typval_T *argvars;
9266 typval_T *rettv;
9267{
9268 byteidx(argvars, rettv, FALSE);
9269}
9270
9271/*
9272 * "byteidxcomp()" function
9273 */
9274 static void
9275f_byteidxcomp(argvars, rettv)
9276 typval_T *argvars;
9277 typval_T *rettv;
9278{
9279 byteidx(argvars, rettv, TRUE);
9280}
9281
Bram Moolenaardb913952012-06-29 12:54:53 +02009282 int
9283func_call(name, args, selfdict, rettv)
9284 char_u *name;
9285 typval_T *args;
9286 dict_T *selfdict;
9287 typval_T *rettv;
9288{
9289 listitem_T *item;
9290 typval_T argv[MAX_FUNC_ARGS + 1];
9291 int argc = 0;
9292 int dummy;
9293 int r = 0;
9294
9295 for (item = args->vval.v_list->lv_first; item != NULL;
9296 item = item->li_next)
9297 {
9298 if (argc == MAX_FUNC_ARGS)
9299 {
9300 EMSG(_("E699: Too many arguments"));
9301 break;
9302 }
9303 /* Make a copy of each argument. This is needed to be able to set
9304 * v_lock to VAR_FIXED in the copy without changing the original list.
9305 */
9306 copy_tv(&item->li_tv, &argv[argc++]);
9307 }
9308
9309 if (item == NULL)
9310 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9311 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9312 &dummy, TRUE, selfdict);
9313
9314 /* Free the arguments. */
9315 while (argc > 0)
9316 clear_tv(&argv[--argc]);
9317
9318 return r;
9319}
9320
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322 * "call(func, arglist)" function
9323 */
9324 static void
9325f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328{
9329 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332 if (argvars[1].v_type != VAR_LIST)
9333 {
9334 EMSG(_(e_listreq));
9335 return;
9336 }
9337 if (argvars[1].vval.v_list == NULL)
9338 return;
9339
9340 if (argvars[0].v_type == VAR_FUNC)
9341 func = argvars[0].vval.v_string;
9342 else
9343 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 if (*func == NUL)
9345 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009346
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 if (argvars[2].v_type != VAR_UNKNOWN)
9348 {
9349 if (argvars[2].v_type != VAR_DICT)
9350 {
9351 EMSG(_(e_dictreq));
9352 return;
9353 }
9354 selfdict = argvars[2].vval.v_dict;
9355 }
9356
Bram Moolenaardb913952012-06-29 12:54:53 +02009357 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009358}
9359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#ifdef FEAT_FLOAT
9361/*
9362 * "ceil({float})" function
9363 */
9364 static void
9365f_ceil(argvars, rettv)
9366 typval_T *argvars;
9367 typval_T *rettv;
9368{
9369 float_T f;
9370
9371 rettv->v_type = VAR_FLOAT;
9372 if (get_float_arg(argvars, &f) == OK)
9373 rettv->vval.v_float = ceil(f);
9374 else
9375 rettv->vval.v_float = 0.0;
9376}
9377#endif
9378
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009379/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009380 * "changenr()" function
9381 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009382 static void
9383f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009384 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009385 typval_T *rettv;
9386{
9387 rettv->vval.v_number = curbuf->b_u_seq_cur;
9388}
9389
9390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 * "char2nr(string)" function
9392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398#ifdef FEAT_MBYTE
9399 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009400 {
9401 int utf8 = 0;
9402
9403 if (argvars[1].v_type != VAR_UNKNOWN)
9404 utf8 = get_tv_number_chk(&argvars[1], NULL);
9405
9406 if (utf8)
9407 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9408 else
9409 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
9412#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415
9416/*
9417 * "cindent(lnum)" function
9418 */
9419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#ifdef FEAT_CINDENT
9425 pos_T pos;
9426 linenr_T lnum;
9427
9428 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9431 {
9432 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 curwin->w_cursor = pos;
9435 }
9436 else
9437#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009442 * "clearmatches()" function
9443 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009444 static void
9445f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009446 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009448{
9449#ifdef FEAT_SEARCH_EXTRA
9450 clear_matches(curwin);
9451#endif
9452}
9453
9454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 * "col(string)" function
9456 */
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 colnr_T col = 0;
9463 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009464 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009466 fp = var2fpos(&argvars[0], FALSE, &fnum);
9467 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
9469 if (fp->col == MAXCOL)
9470 {
9471 /* '> can be MAXCOL, get the length of the line then */
9472 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009473 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 else
9475 col = MAXCOL;
9476 }
9477 else
9478 {
9479 col = fp->col + 1;
9480#ifdef FEAT_VIRTUALEDIT
9481 /* col(".") when the cursor is on the NUL at the end of the line
9482 * because of "coladd" can be seen as an extra column. */
9483 if (virtual_active() && fp == &curwin->w_cursor)
9484 {
9485 char_u *p = ml_get_cursor();
9486
9487 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9488 curwin->w_virtcol - curwin->w_cursor.coladd))
9489 {
9490# ifdef FEAT_MBYTE
9491 int l;
9492
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009493 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 col += l;
9495# else
9496 if (*p != NUL && p[1] == NUL)
9497 ++col;
9498# endif
9499 }
9500 }
9501#endif
9502 }
9503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar572cb562005-08-05 21:35:02 +00009507#if defined(FEAT_INS_EXPAND)
9508/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009509 * "complete()" function
9510 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009511 static void
9512f_complete(argvars, rettv)
9513 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009514 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009515{
9516 int startcol;
9517
9518 if ((State & INSERT) == 0)
9519 {
9520 EMSG(_("E785: complete() can only be used in Insert mode"));
9521 return;
9522 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009523
9524 /* Check for undo allowed here, because if something was already inserted
9525 * the line was already saved for undo and this check isn't done. */
9526 if (!undo_allowed())
9527 return;
9528
Bram Moolenaarade00832006-03-10 21:46:58 +00009529 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9530 {
9531 EMSG(_(e_invarg));
9532 return;
9533 }
9534
9535 startcol = get_tv_number_chk(&argvars[0], NULL);
9536 if (startcol <= 0)
9537 return;
9538
9539 set_completion(startcol - 1, argvars[1].vval.v_list);
9540}
9541
9542/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 * "complete_add()" function
9544 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009545 static void
9546f_complete_add(argvars, rettv)
9547 typval_T *argvars;
9548 typval_T *rettv;
9549{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009550 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009551}
9552
9553/*
9554 * "complete_check()" function
9555 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009556 static void
9557f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009558 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009559 typval_T *rettv;
9560{
9561 int saved = RedrawingDisabled;
9562
9563 RedrawingDisabled = 0;
9564 ins_compl_check_keys(0);
9565 rettv->vval.v_number = compl_interrupted;
9566 RedrawingDisabled = saved;
9567}
9568#endif
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570/*
9571 * "confirm(message, buttons[, default [, type]])" function
9572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009575 typval_T *argvars UNUSED;
9576 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9579 char_u *message;
9580 char_u *buttons = NULL;
9581 char_u buf[NUMBUFLEN];
9582 char_u buf2[NUMBUFLEN];
9583 int def = 1;
9584 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 char_u *typestr;
9586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 message = get_tv_string_chk(&argvars[0]);
9589 if (message == NULL)
9590 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9594 if (buttons == NULL)
9595 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9602 if (typestr == NULL)
9603 error = TRUE;
9604 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 switch (TOUPPER_ASC(*typestr))
9607 {
9608 case 'E': type = VIM_ERROR; break;
9609 case 'Q': type = VIM_QUESTION; break;
9610 case 'I': type = VIM_INFO; break;
9611 case 'W': type = VIM_WARNING; break;
9612 case 'G': type = VIM_GENERIC; break;
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615 }
9616 }
9617 }
9618
9619 if (buttons == NULL || *buttons == NUL)
9620 buttons = (char_u *)_("&Ok");
9621
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009622 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009624 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626}
9627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628/*
9629 * "copy()" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 typval_T *argvars;
9634 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009636 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009639#ifdef FEAT_FLOAT
9640/*
9641 * "cos()" function
9642 */
9643 static void
9644f_cos(argvars, rettv)
9645 typval_T *argvars;
9646 typval_T *rettv;
9647{
9648 float_T f;
9649
9650 rettv->v_type = VAR_FLOAT;
9651 if (get_float_arg(argvars, &f) == OK)
9652 rettv->vval.v_float = cos(f);
9653 else
9654 rettv->vval.v_float = 0.0;
9655}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009656
9657/*
9658 * "cosh()" function
9659 */
9660 static void
9661f_cosh(argvars, rettv)
9662 typval_T *argvars;
9663 typval_T *rettv;
9664{
9665 float_T f;
9666
9667 rettv->v_type = VAR_FLOAT;
9668 if (get_float_arg(argvars, &f) == OK)
9669 rettv->vval.v_float = cosh(f);
9670 else
9671 rettv->vval.v_float = 0.0;
9672}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009673#endif
9674
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009676 * "count()" function
9677 */
9678 static void
9679f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009682{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009683 long n = 0;
9684 int ic = FALSE;
9685
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009688 listitem_T *li;
9689 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692 if ((l = argvars[0].vval.v_list) != NULL)
9693 {
9694 li = l->lv_first;
9695 if (argvars[2].v_type != VAR_UNKNOWN)
9696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 int error = FALSE;
9698
9699 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 if (argvars[3].v_type != VAR_UNKNOWN)
9701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 idx = get_tv_number_chk(&argvars[3], &error);
9703 if (!error)
9704 {
9705 li = list_find(l, idx);
9706 if (li == NULL)
9707 EMSGN(_(e_listidx), idx);
9708 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 if (error)
9711 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009712 }
9713
9714 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009715 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 ++n;
9717 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009718 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009719 else if (argvars[0].v_type == VAR_DICT)
9720 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 int todo;
9722 dict_T *d;
9723 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009725 if ((d = argvars[0].vval.v_dict) != NULL)
9726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 int error = FALSE;
9728
Bram Moolenaare9a41262005-01-15 22:18:47 +00009729 if (argvars[2].v_type != VAR_UNKNOWN)
9730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 if (argvars[3].v_type != VAR_UNKNOWN)
9733 EMSG(_(e_invarg));
9734 }
9735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009736 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009738 {
9739 if (!HASHITEM_EMPTY(hi))
9740 {
9741 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009742 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009743 ++n;
9744 }
9745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 }
9747 }
9748 else
9749 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009750 rettv->vval.v_number = n;
9751}
9752
9753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9755 *
9756 * Checks the existence of a cscope connection.
9757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009760 typval_T *argvars UNUSED;
9761 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
9763#ifdef FEAT_CSCOPE
9764 int num = 0;
9765 char_u *dbpath = NULL;
9766 char_u *prepend = NULL;
9767 char_u buf[NUMBUFLEN];
9768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009769 if (argvars[0].v_type != VAR_UNKNOWN
9770 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 num = (int)get_tv_number(&argvars[0]);
9773 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 }
9777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#endif
9780}
9781
9782/*
9783 * "cursor(lnum, col)" function
9784 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009785 * Moves the cursor to the specified line and column.
9786 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 typval_T *argvars;
9791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009794#ifdef FEAT_VIRTUALEDIT
9795 long coladd = 0;
9796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009798 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009799 if (argvars[1].v_type == VAR_UNKNOWN)
9800 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009801 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009802 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009803
Bram Moolenaar493c1782014-05-28 14:34:46 +02009804 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009805 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009806 line = pos.lnum;
9807 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009808#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009809 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009810#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009811 if (curswant >= 0)
9812 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009813 }
9814 else
9815 {
9816 line = get_tv_lnum(argvars);
9817 col = get_tv_number_chk(&argvars[1], NULL);
9818#ifdef FEAT_VIRTUALEDIT
9819 if (argvars[2].v_type != VAR_UNKNOWN)
9820 coladd = get_tv_number_chk(&argvars[2], NULL);
9821#endif
9822 }
9823 if (line < 0 || col < 0
9824#ifdef FEAT_VIRTUALEDIT
9825 || coladd < 0
9826#endif
9827 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009828 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 if (line > 0)
9830 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 if (col > 0)
9832 curwin->w_cursor.col = col - 1;
9833#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009834 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835#endif
9836
9837 /* Make sure the cursor is in a valid position. */
9838 check_cursor();
9839#ifdef FEAT_MBYTE
9840 /* Correct cursor for multi-byte character. */
9841 if (has_mbyte)
9842 mb_adjust_cursor();
9843#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009844
9845 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009846 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847}
9848
9849/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009850 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 */
9852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009854 typval_T *argvars;
9855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009857 int noref = 0;
9858
9859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009860 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009861 if (noref < 0 || noref > 1)
9862 EMSG(_(e_invarg));
9863 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009864 {
9865 current_copyID += COPYID_INC;
9866 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868}
9869
9870/*
9871 * "delete()" function
9872 */
9873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 typval_T *argvars;
9876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
9884/*
9885 * "did_filetype()" function
9886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009889 typval_T *argvars UNUSED;
9890 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894#endif
9895}
9896
9897/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009898 * "diff_filler()" function
9899 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009902 typval_T *argvars UNUSED;
9903 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009904{
9905#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009907#endif
9908}
9909
9910/*
9911 * "diff_hlID()" function
9912 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009915 typval_T *argvars UNUSED;
9916 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009917{
9918#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009920 static linenr_T prev_lnum = 0;
9921 static int changedtick = 0;
9922 static int fnum = 0;
9923 static int change_start = 0;
9924 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009925 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009926 int filler_lines;
9927 int col;
9928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 if (lnum < 0) /* ignore type error in {lnum} arg */
9930 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009931 if (lnum != prev_lnum
9932 || changedtick != curbuf->b_changedtick
9933 || fnum != curbuf->b_fnum)
9934 {
9935 /* New line, buffer, change: need to get the values. */
9936 filler_lines = diff_check(curwin, lnum);
9937 if (filler_lines < 0)
9938 {
9939 if (filler_lines == -1)
9940 {
9941 change_start = MAXCOL;
9942 change_end = -1;
9943 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9944 hlID = HLF_ADD; /* added line */
9945 else
9946 hlID = HLF_CHD; /* changed line */
9947 }
9948 else
9949 hlID = HLF_ADD; /* added line */
9950 }
9951 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009952 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009953 prev_lnum = lnum;
9954 changedtick = curbuf->b_changedtick;
9955 fnum = curbuf->b_fnum;
9956 }
9957
9958 if (hlID == HLF_CHD || hlID == HLF_TXD)
9959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009960 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009961 if (col >= change_start && col <= change_end)
9962 hlID = HLF_TXD; /* changed text */
9963 else
9964 hlID = HLF_CHD; /* changed line */
9965 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009966 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009967#endif
9968}
9969
9970/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009971 * "empty({expr})" function
9972 */
9973 static void
9974f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 typval_T *argvars;
9976 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009977{
9978 int n;
9979
9980 switch (argvars[0].v_type)
9981 {
9982 case VAR_STRING:
9983 case VAR_FUNC:
9984 n = argvars[0].vval.v_string == NULL
9985 || *argvars[0].vval.v_string == NUL;
9986 break;
9987 case VAR_NUMBER:
9988 n = argvars[0].vval.v_number == 0;
9989 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009990#ifdef FEAT_FLOAT
9991 case VAR_FLOAT:
9992 n = argvars[0].vval.v_float == 0.0;
9993 break;
9994#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009995 case VAR_LIST:
9996 n = argvars[0].vval.v_list == NULL
9997 || argvars[0].vval.v_list->lv_first == NULL;
9998 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 case VAR_DICT:
10000 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010002 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010003 default:
10004 EMSG2(_(e_intern2), "f_empty()");
10005 n = 0;
10006 }
10007
10008 rettv->vval.v_number = n;
10009}
10010
10011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 * "escape({string}, {chars})" function
10013 */
10014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010016 typval_T *argvars;
10017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
10019 char_u buf[NUMBUFLEN];
10020
Bram Moolenaar758711c2005-02-02 23:11:38 +000010021 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10022 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024}
10025
10026/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010027 * "eval()" function
10028 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010029 static void
10030f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010033{
10034 char_u *s;
10035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 s = get_tv_string_chk(&argvars[0]);
10037 if (s != NULL)
10038 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10041 {
10042 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010043 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010045 else if (*s != NUL)
10046 EMSG(_(e_trailing));
10047}
10048
10049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 * "eventhandler()" function
10051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058}
10059
10060/*
10061 * "executable()" function
10062 */
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010068 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10069}
10070
10071/*
10072 * "exepath()" function
10073 */
10074 static void
10075f_exepath(argvars, rettv)
10076 typval_T *argvars;
10077 typval_T *rettv;
10078{
10079 char_u *p = NULL;
10080
10081 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10082 rettv->v_type = VAR_STRING;
10083 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086/*
10087 * "exists()" function
10088 */
10089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 typval_T *argvars;
10092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 char_u *p;
10095 char_u *name;
10096 int n = FALSE;
10097 int len = 0;
10098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (*p == '$') /* environment variable */
10101 {
10102 /* first try "normal" environment variables (fast) */
10103 if (mch_getenv(p + 1) != NULL)
10104 n = TRUE;
10105 else
10106 {
10107 /* try expanding things like $VIM and ${HOME} */
10108 p = expand_env_save(p);
10109 if (p != NULL && *p != '$')
10110 n = TRUE;
10111 vim_free(p);
10112 }
10113 }
10114 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010117 if (*skipwhite(p) != NUL)
10118 n = FALSE; /* trailing garbage */
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else if (*p == '*') /* internal or user defined function */
10121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
10124 else if (*p == ':')
10125 {
10126 n = cmd_exists(p + 1);
10127 }
10128 else if (*p == '#')
10129 {
10130#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010131 if (p[1] == '#')
10132 n = autocmd_supported(p + 2);
10133 else
10134 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#endif
10136 }
10137 else /* internal variable */
10138 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010139 char_u *tofree;
10140 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010142 /* get_name_len() takes care of expanding curly braces */
10143 name = p;
10144 len = get_name_len(&p, &tofree, TRUE, FALSE);
10145 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010147 if (tofree != NULL)
10148 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010149 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010150 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010152 /* handle d.key, l[idx], f(expr) */
10153 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10154 if (n)
10155 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010158 if (*p != NUL)
10159 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010161 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 }
10163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165}
10166
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010167#ifdef FEAT_FLOAT
10168/*
10169 * "exp()" function
10170 */
10171 static void
10172f_exp(argvars, rettv)
10173 typval_T *argvars;
10174 typval_T *rettv;
10175{
10176 float_T f;
10177
10178 rettv->v_type = VAR_FLOAT;
10179 if (get_float_arg(argvars, &f) == OK)
10180 rettv->vval.v_float = exp(f);
10181 else
10182 rettv->vval.v_float = 0.0;
10183}
10184#endif
10185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186/*
10187 * "expand()" function
10188 */
10189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010191 typval_T *argvars;
10192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193{
10194 char_u *s;
10195 int len;
10196 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010197 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010200 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010202 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010203 if (argvars[1].v_type != VAR_UNKNOWN
10204 && argvars[2].v_type != VAR_UNKNOWN
10205 && get_tv_number_chk(&argvars[2], &error)
10206 && !error)
10207 {
10208 rettv->v_type = VAR_LIST;
10209 rettv->vval.v_list = NULL;
10210 }
10211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (*s == '%' || *s == '#' || *s == '<')
10214 {
10215 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010216 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010218 if (rettv->v_type == VAR_LIST)
10219 {
10220 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10221 list_append_string(rettv->vval.v_list, result, -1);
10222 else
10223 vim_free(result);
10224 }
10225 else
10226 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 }
10228 else
10229 {
10230 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010231 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 if (argvars[1].v_type != VAR_UNKNOWN
10233 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010234 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 if (!error)
10236 {
10237 ExpandInit(&xpc);
10238 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010239 if (p_wic)
10240 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010241 if (rettv->v_type == VAR_STRING)
10242 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10243 options, WILD_ALL);
10244 else if (rettv_list_alloc(rettv) != FAIL)
10245 {
10246 int i;
10247
10248 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10249 for (i = 0; i < xpc.xp_numfiles; i++)
10250 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10251 ExpandCleanup(&xpc);
10252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 }
10254 else
10255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257}
10258
10259/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010260 * Go over all entries in "d2" and add them to "d1".
10261 * When "action" is "error" then a duplicate key is an error.
10262 * When "action" is "force" then a duplicate key is overwritten.
10263 * Otherwise duplicate keys are ignored ("action" is "keep").
10264 */
10265 void
10266dict_extend(d1, d2, action)
10267 dict_T *d1;
10268 dict_T *d2;
10269 char_u *action;
10270{
10271 dictitem_T *di1;
10272 hashitem_T *hi2;
10273 int todo;
10274
10275 todo = (int)d2->dv_hashtab.ht_used;
10276 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10277 {
10278 if (!HASHITEM_EMPTY(hi2))
10279 {
10280 --todo;
10281 di1 = dict_find(d1, hi2->hi_key, -1);
10282 if (d1->dv_scope != 0)
10283 {
10284 /* Disallow replacing a builtin function in l: and g:.
10285 * Check the key to be valid when adding to any
10286 * scope. */
10287 if (d1->dv_scope == VAR_DEF_SCOPE
10288 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10289 && var_check_func_name(hi2->hi_key,
10290 di1 == NULL))
10291 break;
10292 if (!valid_varname(hi2->hi_key))
10293 break;
10294 }
10295 if (di1 == NULL)
10296 {
10297 di1 = dictitem_copy(HI2DI(hi2));
10298 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10299 dictitem_free(di1);
10300 }
10301 else if (*action == 'e')
10302 {
10303 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10304 break;
10305 }
10306 else if (*action == 'f' && HI2DI(hi2) != di1)
10307 {
10308 clear_tv(&di1->di_tv);
10309 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10310 }
10311 }
10312 }
10313}
10314
10315/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010316 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010318 */
10319 static void
10320f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010323{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010324 char *arg_errmsg = N_("extend() argument");
10325
Bram Moolenaare9a41262005-01-15 22:18:47 +000010326 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010327 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010328 list_T *l1, *l2;
10329 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010332
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 l1 = argvars[0].vval.v_list;
10334 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010335 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010336 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010337 {
10338 if (argvars[2].v_type != VAR_UNKNOWN)
10339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 before = get_tv_number_chk(&argvars[2], &error);
10341 if (error)
10342 return; /* type error; errmsg already given */
10343
Bram Moolenaar758711c2005-02-02 23:11:38 +000010344 if (before == l1->lv_len)
10345 item = NULL;
10346 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010347 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010348 item = list_find(l1, before);
10349 if (item == NULL)
10350 {
10351 EMSGN(_(e_listidx), before);
10352 return;
10353 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 }
10355 }
10356 else
10357 item = NULL;
10358 list_extend(l1, l2, item);
10359
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360 copy_tv(&argvars[0], rettv);
10361 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010362 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10364 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010365 dict_T *d1, *d2;
10366 char_u *action;
10367 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368
10369 d1 = argvars[0].vval.v_dict;
10370 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010371 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010372 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010373 {
10374 /* Check the third argument. */
10375 if (argvars[2].v_type != VAR_UNKNOWN)
10376 {
10377 static char *(av[]) = {"keep", "force", "error"};
10378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 action = get_tv_string_chk(&argvars[2]);
10380 if (action == NULL)
10381 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010382 for (i = 0; i < 3; ++i)
10383 if (STRCMP(action, av[i]) == 0)
10384 break;
10385 if (i == 3)
10386 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010387 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 return;
10389 }
10390 }
10391 else
10392 action = (char_u *)"force";
10393
Bram Moolenaara9922d62013-05-30 13:01:18 +020010394 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010395
Bram Moolenaare9a41262005-01-15 22:18:47 +000010396 copy_tv(&argvars[0], rettv);
10397 }
10398 }
10399 else
10400 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010401}
10402
10403/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010404 * "feedkeys()" function
10405 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 static void
10407f_feedkeys(argvars, rettv)
10408 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010409 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010410{
10411 int remap = TRUE;
10412 char_u *keys, *flags;
10413 char_u nbuf[NUMBUFLEN];
10414 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010415 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010416
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010417 /* This is not allowed in the sandbox. If the commands would still be
10418 * executed in the sandbox it would be OK, but it probably happens later,
10419 * when "sandbox" is no longer set. */
10420 if (check_secure())
10421 return;
10422
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010423 keys = get_tv_string(&argvars[0]);
10424 if (*keys != NUL)
10425 {
10426 if (argvars[1].v_type != VAR_UNKNOWN)
10427 {
10428 flags = get_tv_string_buf(&argvars[1], nbuf);
10429 for ( ; *flags != NUL; ++flags)
10430 {
10431 switch (*flags)
10432 {
10433 case 'n': remap = FALSE; break;
10434 case 'm': remap = TRUE; break;
10435 case 't': typed = TRUE; break;
10436 }
10437 }
10438 }
10439
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010440 /* Need to escape K_SPECIAL and CSI before putting the string in the
10441 * typeahead buffer. */
10442 keys_esc = vim_strsave_escape_csi(keys);
10443 if (keys_esc != NULL)
10444 {
10445 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010446 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010447 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010448 if (vgetc_busy)
10449 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010450 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010451 }
10452}
10453
10454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 * "filereadable()" function
10456 */
10457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *argvars;
10460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010462 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 char_u *p;
10464 int n;
10465
Bram Moolenaarc236c162008-07-13 17:41:49 +000010466#ifndef O_NONBLOCK
10467# define O_NONBLOCK 0
10468#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010469 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010470 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10471 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
10473 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010474 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 }
10476 else
10477 n = FALSE;
10478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480}
10481
10482/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010483 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 * rights to write into.
10485 */
10486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010491 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010494static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010495
10496 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010497findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010498 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010500 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010501{
10502#ifdef FEAT_SEARCHPATH
10503 char_u *fname;
10504 char_u *fresult = NULL;
10505 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10506 char_u *p;
10507 char_u pathbuf[NUMBUFLEN];
10508 int count = 1;
10509 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010510 int error = FALSE;
10511#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010512
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010513 rettv->vval.v_string = NULL;
10514 rettv->v_type = VAR_STRING;
10515
10516#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010518
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010519 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10522 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010523 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 else
10525 {
10526 if (*p != NUL)
10527 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010530 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010532 }
10533
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010534 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10535 error = TRUE;
10536
10537 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 do
10540 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010541 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010542 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 fresult = find_file_in_path_option(first ? fname : NULL,
10544 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010545 0, first, path,
10546 find_what,
10547 curbuf->b_ffname,
10548 find_what == FINDFILE_DIR
10549 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010551
10552 if (fresult != NULL && rettv->v_type == VAR_LIST)
10553 list_append_string(rettv->vval.v_list, fresult, -1);
10554
10555 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010557
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010558 if (rettv->v_type == VAR_STRING)
10559 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010560#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010561}
10562
Bram Moolenaar33570922005-01-25 22:26:29 +000010563static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10564static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565
10566/*
10567 * Implementation of map() and filter().
10568 */
10569 static void
10570filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010573 int map;
10574{
10575 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 listitem_T *li, *nli;
10578 list_T *l = NULL;
10579 dictitem_T *di;
10580 hashtab_T *ht;
10581 hashitem_T *hi;
10582 dict_T *d = NULL;
10583 typval_T save_val;
10584 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010586 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010587 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10588 char *arg_errmsg = (map ? N_("map() argument")
10589 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010590 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010591 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010592
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010595 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010596 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 return;
10598 }
10599 else if (argvars[0].v_type == VAR_DICT)
10600 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010601 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010602 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 return;
10604 }
10605 else
10606 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010607 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 return;
10609 }
10610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 expr = get_tv_string_buf_chk(&argvars[1], buf);
10612 /* On type errors, the preceding call has already displayed an error
10613 * message. Avoid a misleading error message for an empty string that
10614 * was not passed as argument. */
10615 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 prepare_vimvar(VV_VAL, &save_val);
10618 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010619
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010620 /* We reset "did_emsg" to be able to detect whether an error
10621 * occurred during evaluation of the expression. */
10622 save_did_emsg = did_emsg;
10623 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010624
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010625 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 vimvars[VV_KEY].vv_type = VAR_STRING;
10629
10630 ht = &d->dv_hashtab;
10631 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010632 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 if (!HASHITEM_EMPTY(hi))
10636 {
10637 --todo;
10638 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010639 if (tv_check_lock(di->di_tv.v_lock,
10640 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010641 break;
10642 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010643 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010644 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 break;
10646 if (!map && rem)
10647 dictitem_remove(d, di);
10648 clear_tv(&vimvars[VV_KEY].vv_tv);
10649 }
10650 }
10651 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 }
10653 else
10654 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010655 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010657 for (li = l->lv_first; li != NULL; li = nli)
10658 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010659 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010660 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010662 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010663 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010664 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010665 break;
10666 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010668 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010669 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010670 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010671
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010672 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010674
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010675 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677
10678 copy_tv(&argvars[0], rettv);
10679}
10680
10681 static int
10682filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010683 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010684 char_u *expr;
10685 int map;
10686 int *remp;
10687{
Bram Moolenaar33570922005-01-25 22:26:29 +000010688 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010689 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010690 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010691
Bram Moolenaar33570922005-01-25 22:26:29 +000010692 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 s = expr;
10694 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010695 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 if (*s != NUL) /* check for trailing chars after expr */
10697 {
10698 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010699 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010700 }
10701 if (map)
10702 {
10703 /* map(): replace the list item value */
10704 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010705 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010706 *tv = rettv;
10707 }
10708 else
10709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 int error = FALSE;
10711
Bram Moolenaare9a41262005-01-15 22:18:47 +000010712 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010714 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010715 /* On type error, nothing has been removed; return FAIL to stop the
10716 * loop. The error message was given by get_tv_number_chk(). */
10717 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010718 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010719 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010720 retval = OK;
10721theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010723 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010724}
10725
10726/*
10727 * "filter()" function
10728 */
10729 static void
10730f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010733{
10734 filter_map(argvars, rettv, FALSE);
10735}
10736
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010737/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010738 * "finddir({fname}[, {path}[, {count}]])" function
10739 */
10740 static void
10741f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010744{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010745 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010746}
10747
10748/*
10749 * "findfile({fname}[, {path}[, {count}]])" function
10750 */
10751 static void
10752f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010753 typval_T *argvars;
10754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010755{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010756 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010757}
10758
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010759#ifdef FEAT_FLOAT
10760/*
10761 * "float2nr({float})" function
10762 */
10763 static void
10764f_float2nr(argvars, rettv)
10765 typval_T *argvars;
10766 typval_T *rettv;
10767{
10768 float_T f;
10769
10770 if (get_float_arg(argvars, &f) == OK)
10771 {
10772 if (f < -0x7fffffff)
10773 rettv->vval.v_number = -0x7fffffff;
10774 else if (f > 0x7fffffff)
10775 rettv->vval.v_number = 0x7fffffff;
10776 else
10777 rettv->vval.v_number = (varnumber_T)f;
10778 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010779}
10780
10781/*
10782 * "floor({float})" function
10783 */
10784 static void
10785f_floor(argvars, rettv)
10786 typval_T *argvars;
10787 typval_T *rettv;
10788{
10789 float_T f;
10790
10791 rettv->v_type = VAR_FLOAT;
10792 if (get_float_arg(argvars, &f) == OK)
10793 rettv->vval.v_float = floor(f);
10794 else
10795 rettv->vval.v_float = 0.0;
10796}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010797
10798/*
10799 * "fmod()" function
10800 */
10801 static void
10802f_fmod(argvars, rettv)
10803 typval_T *argvars;
10804 typval_T *rettv;
10805{
10806 float_T fx, fy;
10807
10808 rettv->v_type = VAR_FLOAT;
10809 if (get_float_arg(argvars, &fx) == OK
10810 && get_float_arg(&argvars[1], &fy) == OK)
10811 rettv->vval.v_float = fmod(fx, fy);
10812 else
10813 rettv->vval.v_float = 0.0;
10814}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010815#endif
10816
Bram Moolenaar0d660222005-01-07 21:51:51 +000010817/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010818 * "fnameescape({string})" function
10819 */
10820 static void
10821f_fnameescape(argvars, rettv)
10822 typval_T *argvars;
10823 typval_T *rettv;
10824{
10825 rettv->vval.v_string = vim_strsave_fnameescape(
10826 get_tv_string(&argvars[0]), FALSE);
10827 rettv->v_type = VAR_STRING;
10828}
10829
10830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 * "fnamemodify({fname}, {mods})" function
10832 */
10833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010835 typval_T *argvars;
10836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838 char_u *fname;
10839 char_u *mods;
10840 int usedlen = 0;
10841 int len;
10842 char_u *fbuf = NULL;
10843 char_u buf[NUMBUFLEN];
10844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 fname = get_tv_string_chk(&argvars[0]);
10846 mods = get_tv_string_buf_chk(&argvars[1], buf);
10847 if (fname == NULL || mods == NULL)
10848 fname = NULL;
10849 else
10850 {
10851 len = (int)STRLEN(fname);
10852 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 vim_free(fbuf);
10861}
10862
Bram Moolenaar33570922005-01-25 22:26:29 +000010863static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
10865/*
10866 * "foldclosed()" function
10867 */
10868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010870 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010872 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
10874#ifdef FEAT_FOLDING
10875 linenr_T lnum;
10876 linenr_T first, last;
10877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10880 {
10881 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10882 {
10883 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 return;
10888 }
10889 }
10890#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892}
10893
10894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895 * "foldclosed()" function
10896 */
10897 static void
10898f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010899 typval_T *argvars;
10900 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010901{
10902 foldclosed_both(argvars, rettv, FALSE);
10903}
10904
10905/*
10906 * "foldclosedend()" function
10907 */
10908 static void
10909f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010910 typval_T *argvars;
10911 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010912{
10913 foldclosed_both(argvars, rettv, TRUE);
10914}
10915
10916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 * "foldlevel()" function
10918 */
10919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010921 typval_T *argvars UNUSED;
10922 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924#ifdef FEAT_FOLDING
10925 linenr_T lnum;
10926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931}
10932
10933/*
10934 * "foldtext()" function
10935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010938 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
10941#ifdef FEAT_FOLDING
10942 linenr_T lnum;
10943 char_u *s;
10944 char_u *r;
10945 int len;
10946 char *txt;
10947#endif
10948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->v_type = VAR_STRING;
10950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10953 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10954 <= curbuf->b_ml.ml_line_count
10955 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 {
10957 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10959 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 {
10961 if (!linewhite(lnum))
10962 break;
10963 ++lnum;
10964 }
10965
10966 /* Find interesting text in this line. */
10967 s = skipwhite(ml_get(lnum));
10968 /* skip C comment-start */
10969 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010972 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010973 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010974 {
10975 s = skipwhite(ml_get(lnum + 1));
10976 if (*s == '*')
10977 s = skipwhite(s + 1);
10978 }
10979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 txt = _("+-%s%3ld lines: ");
10981 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 + 20 /* for %3ld */
10984 + STRLEN(s))); /* concatenated */
10985 if (r != NULL)
10986 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10988 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10989 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 len = (int)STRLEN(r);
10991 STRCAT(r, s);
10992 /* remove 'foldmarker' and 'commentstring' */
10993 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 }
10996 }
10997#endif
10998}
10999
11000/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011001 * "foldtextresult(lnum)" function
11002 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011005 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011007{
11008#ifdef FEAT_FOLDING
11009 linenr_T lnum;
11010 char_u *text;
11011 char_u buf[51];
11012 foldinfo_T foldinfo;
11013 int fold_count;
11014#endif
11015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->v_type = VAR_STRING;
11017 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011018#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 /* treat illegal types and illegal string values for {lnum} the same */
11021 if (lnum < 0)
11022 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011023 fold_count = foldedCount(curwin, lnum, &foldinfo);
11024 if (fold_count > 0)
11025 {
11026 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11027 &foldinfo, buf);
11028 if (text == buf)
11029 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011031 }
11032#endif
11033}
11034
11035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * "foreground()" function
11037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011040 typval_T *argvars UNUSED;
11041 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043#ifdef FEAT_GUI
11044 if (gui.in_use)
11045 gui_mch_set_foreground();
11046#else
11047# ifdef WIN32
11048 win32_set_foreground();
11049# endif
11050#endif
11051}
11052
11053/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054 * "function()" function
11055 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011058 typval_T *argvars;
11059 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060{
11061 char_u *s;
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011064 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011065 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011066 /* Don't check an autoload name for existence here. */
11067 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011068 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011069 else
11070 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011071 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011072 {
11073 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011074 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011075
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011076 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11077 * also be called from another script. Using trans_function_name()
11078 * would also work, but some plugins depend on the name being
11079 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011080 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011081 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011082 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011083 if (rettv->vval.v_string != NULL)
11084 {
11085 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011086 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011087 }
11088 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011089 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011090 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092 }
11093}
11094
11095/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011096 * "garbagecollect()" function
11097 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011098 static void
11099f_garbagecollect(argvars, rettv)
11100 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011101 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011102{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011103 /* This is postponed until we are back at the toplevel, because we may be
11104 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11105 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011106
11107 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11108 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011109}
11110
11111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112 * "get()" function
11113 */
11114 static void
11115f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118{
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 listitem_T *li;
11120 list_T *l;
11121 dictitem_T *di;
11122 dict_T *d;
11123 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011124
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 int error = FALSE;
11130
11131 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11132 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 else if (argvars[0].v_type == VAR_DICT)
11137 {
11138 if ((d = argvars[0].vval.v_dict) != NULL)
11139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011140 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011141 if (di != NULL)
11142 tv = &di->di_tv;
11143 }
11144 }
11145 else
11146 EMSG2(_(e_listdictarg), "get()");
11147
11148 if (tv == NULL)
11149 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 copy_tv(&argvars[2], rettv);
11152 }
11153 else
11154 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155}
11156
Bram Moolenaar342337a2005-07-21 21:11:17 +000011157static 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 +000011158
11159/*
11160 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011161 * Return a range (from start to end) of lines in rettv from the specified
11162 * buffer.
11163 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 */
11165 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011166get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011167 buf_T *buf;
11168 linenr_T start;
11169 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011170 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011171 typval_T *rettv;
11172{
11173 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011174
Bram Moolenaar959a1432013-12-14 12:17:38 +010011175 rettv->v_type = VAR_STRING;
11176 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011177 if (retlist && rettv_list_alloc(rettv) == FAIL)
11178 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011179
11180 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11181 return;
11182
11183 if (!retlist)
11184 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011185 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11186 p = ml_get_buf(buf, start, FALSE);
11187 else
11188 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011189 rettv->vval.v_string = vim_strsave(p);
11190 }
11191 else
11192 {
11193 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011194 return;
11195
11196 if (start < 1)
11197 start = 1;
11198 if (end > buf->b_ml.ml_line_count)
11199 end = buf->b_ml.ml_line_count;
11200 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011201 if (list_append_string(rettv->vval.v_list,
11202 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011203 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011204 }
11205}
11206
11207/*
11208 * "getbufline()" function
11209 */
11210 static void
11211f_getbufline(argvars, rettv)
11212 typval_T *argvars;
11213 typval_T *rettv;
11214{
11215 linenr_T lnum;
11216 linenr_T end;
11217 buf_T *buf;
11218
11219 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11220 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011221 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011222 --emsg_off;
11223
Bram Moolenaar661b1822005-07-28 22:36:45 +000011224 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011225 if (argvars[2].v_type == VAR_UNKNOWN)
11226 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011227 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011228 end = get_tv_lnum_buf(&argvars[2], buf);
11229
Bram Moolenaar342337a2005-07-21 21:11:17 +000011230 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011231}
11232
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233/*
11234 * "getbufvar()" function
11235 */
11236 static void
11237f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 typval_T *argvars;
11239 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240{
11241 buf_T *buf;
11242 buf_T *save_curbuf;
11243 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011245 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11248 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011250 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011252 rettv->v_type = VAR_STRING;
11253 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011254
11255 if (buf != NULL && varname != NULL)
11256 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011257 /* set curbuf to be our buf, temporarily */
11258 save_curbuf = curbuf;
11259 curbuf = buf;
11260
Bram Moolenaar0d660222005-01-07 21:51:51 +000011261 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011262 {
11263 if (get_option_tv(&varname, rettv, TRUE) == OK)
11264 done = TRUE;
11265 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011266 else if (STRCMP(varname, "changedtick") == 0)
11267 {
11268 rettv->v_type = VAR_NUMBER;
11269 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011270 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011272 else
11273 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011274 /* Look up the variable. */
11275 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11276 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11277 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011278 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011279 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011281 done = TRUE;
11282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011284
11285 /* restore previous notion of curbuf */
11286 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011287 }
11288
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011289 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11290 /* use the default value */
11291 copy_tv(&argvars[2], rettv);
11292
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293 --emsg_off;
11294}
11295
11296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 * "getchar()" function
11298 */
11299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T *argvars;
11302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
11304 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011307 /* Position the cursor. Needed after a message that ends in a space. */
11308 windgoto(msg_row, msg_col);
11309
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 ++no_mapping;
11311 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011312 for (;;)
11313 {
11314 if (argvars[0].v_type == VAR_UNKNOWN)
11315 /* getchar(): blocking wait. */
11316 n = safe_vgetc();
11317 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11318 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011319 n = vpeekc_any();
11320 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011321 /* illegal argument or getchar(0) and no char avail: return zero */
11322 n = 0;
11323 else
11324 /* getchar(0) and char avail: return char */
11325 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011326
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011327 if (n == K_IGNORE)
11328 continue;
11329 break;
11330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 --no_mapping;
11332 --allow_keys;
11333
Bram Moolenaar219b8702006-11-01 14:32:36 +000011334 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11335 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11336 vimvars[VV_MOUSE_COL].vv_nr = 0;
11337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 if (IS_SPECIAL(n) || mod_mask != 0)
11340 {
11341 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11342 int i = 0;
11343
11344 /* Turn a special key into three bytes, plus modifier. */
11345 if (mod_mask != 0)
11346 {
11347 temp[i++] = K_SPECIAL;
11348 temp[i++] = KS_MODIFIER;
11349 temp[i++] = mod_mask;
11350 }
11351 if (IS_SPECIAL(n))
11352 {
11353 temp[i++] = K_SPECIAL;
11354 temp[i++] = K_SECOND(n);
11355 temp[i++] = K_THIRD(n);
11356 }
11357#ifdef FEAT_MBYTE
11358 else if (has_mbyte)
11359 i += (*mb_char2bytes)(n, temp + i);
11360#endif
11361 else
11362 temp[i++] = n;
11363 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->v_type = VAR_STRING;
11365 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011366
11367#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011368 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011369 {
11370 int row = mouse_row;
11371 int col = mouse_col;
11372 win_T *win;
11373 linenr_T lnum;
11374# ifdef FEAT_WINDOWS
11375 win_T *wp;
11376# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011377 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011378
11379 if (row >= 0 && col >= 0)
11380 {
11381 /* Find the window at the mouse coordinates and compute the
11382 * text position. */
11383 win = mouse_find_win(&row, &col);
11384 (void)mouse_comp_pos(win, &row, &col, &lnum);
11385# ifdef FEAT_WINDOWS
11386 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011387 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011388# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011389 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011390 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11391 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11392 }
11393 }
11394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 }
11396}
11397
11398/*
11399 * "getcharmod()" function
11400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011403 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407}
11408
11409/*
11410 * "getcmdline()" function
11411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011414 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->v_type = VAR_STRING;
11418 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419}
11420
11421/*
11422 * "getcmdpos()" function
11423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011426 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431
11432/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011433 * "getcmdtype()" function
11434 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011435 static void
11436f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011437 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011438 typval_T *rettv;
11439{
11440 rettv->v_type = VAR_STRING;
11441 rettv->vval.v_string = alloc(2);
11442 if (rettv->vval.v_string != NULL)
11443 {
11444 rettv->vval.v_string[0] = get_cmdline_type();
11445 rettv->vval.v_string[1] = NUL;
11446 }
11447}
11448
11449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 * "getcwd()" function
11451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011454 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011457 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011460 rettv->vval.v_string = NULL;
11461 cwd = alloc(MAXPATHL);
11462 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011464 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11465 {
11466 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011468 if (rettv->vval.v_string != NULL)
11469 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011471 }
11472 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474}
11475
11476/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011477 * "getfontname()" function
11478 */
11479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->v_type = VAR_STRING;
11485 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011486#ifdef FEAT_GUI
11487 if (gui.in_use)
11488 {
11489 GuiFont font;
11490 char_u *name = NULL;
11491
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011493 {
11494 /* Get the "Normal" font. Either the name saved by
11495 * hl_set_font_name() or from the font ID. */
11496 font = gui.norm_font;
11497 name = hl_get_font_name();
11498 }
11499 else
11500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011502 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11503 return;
11504 font = gui_mch_get_font(name, FALSE);
11505 if (font == NOFONT)
11506 return; /* Invalid font name, return empty string. */
11507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011509 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011510 gui_mch_free_font(font);
11511 }
11512#endif
11513}
11514
11515/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011516 * "getfperm({fname})" function
11517 */
11518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *argvars;
11521 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011522{
11523 char_u *fname;
11524 struct stat st;
11525 char_u *perm = NULL;
11526 char_u flags[] = "rwx";
11527 int i;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011532 if (mch_stat((char *)fname, &st) >= 0)
11533 {
11534 perm = vim_strsave((char_u *)"---------");
11535 if (perm != NULL)
11536 {
11537 for (i = 0; i < 9; i++)
11538 {
11539 if (st.st_mode & (1 << (8 - i)))
11540 perm[i] = flags[i % 3];
11541 }
11542 }
11543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011545}
11546
11547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 * "getfsize({fname})" function
11549 */
11550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554{
11555 char_u *fname;
11556 struct stat st;
11557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561
11562 if (mch_stat((char *)fname, &st) >= 0)
11563 {
11564 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011568 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011569
11570 /* non-perfect check for overflow */
11571 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11572 rettv->vval.v_number = -2;
11573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 }
11575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577}
11578
11579/*
11580 * "getftime({fname})" function
11581 */
11582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011584 typval_T *argvars;
11585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
11587 char_u *fname;
11588 struct stat st;
11589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591
11592 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596}
11597
11598/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011599 * "getftype({fname})" function
11600 */
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011605{
11606 char_u *fname;
11607 struct stat st;
11608 char_u *type = NULL;
11609 char *t;
11610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011614 if (mch_lstat((char *)fname, &st) >= 0)
11615 {
11616#ifdef S_ISREG
11617 if (S_ISREG(st.st_mode))
11618 t = "file";
11619 else if (S_ISDIR(st.st_mode))
11620 t = "dir";
11621# ifdef S_ISLNK
11622 else if (S_ISLNK(st.st_mode))
11623 t = "link";
11624# endif
11625# ifdef S_ISBLK
11626 else if (S_ISBLK(st.st_mode))
11627 t = "bdev";
11628# endif
11629# ifdef S_ISCHR
11630 else if (S_ISCHR(st.st_mode))
11631 t = "cdev";
11632# endif
11633# ifdef S_ISFIFO
11634 else if (S_ISFIFO(st.st_mode))
11635 t = "fifo";
11636# endif
11637# ifdef S_ISSOCK
11638 else if (S_ISSOCK(st.st_mode))
11639 t = "fifo";
11640# endif
11641 else
11642 t = "other";
11643#else
11644# ifdef S_IFMT
11645 switch (st.st_mode & S_IFMT)
11646 {
11647 case S_IFREG: t = "file"; break;
11648 case S_IFDIR: t = "dir"; break;
11649# ifdef S_IFLNK
11650 case S_IFLNK: t = "link"; break;
11651# endif
11652# ifdef S_IFBLK
11653 case S_IFBLK: t = "bdev"; break;
11654# endif
11655# ifdef S_IFCHR
11656 case S_IFCHR: t = "cdev"; break;
11657# endif
11658# ifdef S_IFIFO
11659 case S_IFIFO: t = "fifo"; break;
11660# endif
11661# ifdef S_IFSOCK
11662 case S_IFSOCK: t = "socket"; break;
11663# endif
11664 default: t = "other";
11665 }
11666# else
11667 if (mch_isdir(fname))
11668 t = "dir";
11669 else
11670 t = "file";
11671# endif
11672#endif
11673 type = vim_strsave((char_u *)t);
11674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011676}
11677
11678/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011679 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 */
11681 static void
11682f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685{
11686 linenr_T lnum;
11687 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011688 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011689
11690 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011691 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011692 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011693 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011694 retlist = FALSE;
11695 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011697 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011698 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011699 retlist = TRUE;
11700 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011701
Bram Moolenaar342337a2005-07-21 21:11:17 +000011702 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703}
11704
11705/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011706 * "getmatches()" function
11707 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011708 static void
11709f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011710 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011711 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011712{
11713#ifdef FEAT_SEARCH_EXTRA
11714 dict_T *dict;
11715 matchitem_T *cur = curwin->w_match_head;
11716
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011717 if (rettv_list_alloc(rettv) == OK)
11718 {
11719 while (cur != NULL)
11720 {
11721 dict = dict_alloc();
11722 if (dict == NULL)
11723 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011724 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11725 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11726 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11727 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11728 list_append_dict(rettv->vval.v_list, dict);
11729 cur = cur->next;
11730 }
11731 }
11732#endif
11733}
11734
11735/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011736 * "getpid()" function
11737 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011738 static void
11739f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011740 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011741 typval_T *rettv;
11742{
11743 rettv->vval.v_number = mch_get_pid();
11744}
11745
11746/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011747 * "getpos(string)" function
11748 */
11749 static void
11750f_getpos(argvars, rettv)
11751 typval_T *argvars;
11752 typval_T *rettv;
11753{
11754 pos_T *fp;
11755 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011756 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011757
11758 if (rettv_list_alloc(rettv) == OK)
11759 {
11760 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011761 fp = var2fpos(&argvars[0], TRUE, &fnum);
11762 if (fnum != -1)
11763 list_append_number(l, (varnumber_T)fnum);
11764 else
11765 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011766 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11767 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011768 list_append_number(l, (fp != NULL)
11769 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011770 : (varnumber_T)0);
11771 list_append_number(l,
11772#ifdef FEAT_VIRTUALEDIT
11773 (fp != NULL) ? (varnumber_T)fp->coladd :
11774#endif
11775 (varnumber_T)0);
Bram Moolenaar493c1782014-05-28 14:34:46 +020011776 if (fp == &curwin->w_cursor)
11777 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011778 }
11779 else
11780 rettv->vval.v_number = FALSE;
11781}
11782
11783/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011784 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011785 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011786 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011787f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011788 typval_T *argvars UNUSED;
11789 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011790{
11791#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011792 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011793#endif
11794
Bram Moolenaar2641f772005-03-25 21:58:17 +000011795#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011796 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011797 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011798 wp = NULL;
11799 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11800 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011801 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011802 if (wp == NULL)
11803 return;
11804 }
11805
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011806 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011807 }
11808#endif
11809}
11810
11811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 * "getreg()" function
11813 */
11814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
11819 char_u *strregname;
11820 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011821 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011822 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 strregname = get_tv_string_chk(&argvars[0]);
11828 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011832 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11833 return_list = get_tv_number_chk(&argvars[2], &error);
11834 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011838
11839 if (error)
11840 return;
11841
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 regname = (strregname == NULL ? '"' : *strregname);
11843 if (regname == 0)
11844 regname = '"';
11845
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011846 if (return_list)
11847 {
11848 rettv->v_type = VAR_LIST;
11849 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11850 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11851 }
11852 else
11853 {
11854 rettv->v_type = VAR_STRING;
11855 rettv->vval.v_string = get_reg_contents(regname,
11856 arg2 ? GREG_EXPR_SRC : 0);
11857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858}
11859
11860/*
11861 * "getregtype()" function
11862 */
11863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 typval_T *argvars;
11866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867{
11868 char_u *strregname;
11869 int regname;
11870 char_u buf[NUMBUFLEN + 2];
11871 long reglen = 0;
11872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011873 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 {
11875 strregname = get_tv_string_chk(&argvars[0]);
11876 if (strregname == NULL) /* type error; errmsg already given */
11877 {
11878 rettv->v_type = VAR_STRING;
11879 rettv->vval.v_string = NULL;
11880 return;
11881 }
11882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 else
11884 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011885 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886
11887 regname = (strregname == NULL ? '"' : *strregname);
11888 if (regname == 0)
11889 regname = '"';
11890
11891 buf[0] = NUL;
11892 buf[1] = NUL;
11893 switch (get_reg_type(regname, &reglen))
11894 {
11895 case MLINE: buf[0] = 'V'; break;
11896 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 case MBLOCK:
11898 buf[0] = Ctrl_V;
11899 sprintf((char *)buf + 1, "%ld", reglen + 1);
11900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 rettv->v_type = VAR_STRING;
11903 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904}
11905
11906/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011907 * "gettabvar()" function
11908 */
11909 static void
11910f_gettabvar(argvars, rettv)
11911 typval_T *argvars;
11912 typval_T *rettv;
11913{
11914 tabpage_T *tp;
11915 dictitem_T *v;
11916 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011917 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011918
11919 rettv->v_type = VAR_STRING;
11920 rettv->vval.v_string = NULL;
11921
11922 varname = get_tv_string_chk(&argvars[1]);
11923 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11924 if (tp != NULL && varname != NULL)
11925 {
11926 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011927 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011928 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011929 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011930 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011931 done = TRUE;
11932 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011933 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011934
11935 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011936 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011937}
11938
11939/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011940 * "gettabwinvar()" function
11941 */
11942 static void
11943f_gettabwinvar(argvars, rettv)
11944 typval_T *argvars;
11945 typval_T *rettv;
11946{
11947 getwinvar(argvars, rettv, 1);
11948}
11949
11950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 * "getwinposx()" function
11952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011955 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959#ifdef FEAT_GUI
11960 if (gui.in_use)
11961 {
11962 int x, y;
11963
11964 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 }
11967#endif
11968}
11969
11970/*
11971 * "getwinposy()" function
11972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979#ifdef FEAT_GUI
11980 if (gui.in_use)
11981 {
11982 int x, y;
11983
11984 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 }
11987#endif
11988}
11989
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011990/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011991 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011992 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011993 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011994find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011995 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011996 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011997{
11998#ifdef FEAT_WINDOWS
11999 win_T *wp;
12000#endif
12001 int nr;
12002
12003 nr = get_tv_number_chk(vp, NULL);
12004
12005#ifdef FEAT_WINDOWS
12006 if (nr < 0)
12007 return NULL;
12008 if (nr == 0)
12009 return curwin;
12010
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012011 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12012 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012013 if (--nr <= 0)
12014 break;
12015 return wp;
12016#else
12017 if (nr == 0 || nr == 1)
12018 return curwin;
12019 return NULL;
12020#endif
12021}
12022
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023/*
12024 * "getwinvar()" function
12025 */
12026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012031 getwinvar(argvars, rettv, 0);
12032}
12033
12034/*
12035 * getwinvar() and gettabwinvar()
12036 */
12037 static void
12038getwinvar(argvars, rettv, off)
12039 typval_T *argvars;
12040 typval_T *rettv;
12041 int off; /* 1 for gettabwinvar() */
12042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 win_T *win, *oldcurwin;
12044 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012045 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012046 tabpage_T *tp = NULL;
12047 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012048 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012050#ifdef FEAT_WINDOWS
12051 if (off == 1)
12052 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12053 else
12054 tp = curtab;
12055#endif
12056 win = find_win_by_nr(&argvars[off], tp);
12057 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012060 rettv->v_type = VAR_STRING;
12061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062
12063 if (win != NULL && varname != NULL)
12064 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012065 /* Set curwin to be our win, temporarily. Also set the tabpage,
12066 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012067 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012068
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012070 {
12071 if (get_option_tv(&varname, rettv, 1) == OK)
12072 done = TRUE;
12073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 else
12075 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012076 /* Look up the variable. */
12077 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12078 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012080 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012082 done = TRUE;
12083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012085
12086 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012087 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 }
12089
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012090 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12091 /* use the default return value */
12092 copy_tv(&argvars[off + 2], rettv);
12093
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 --emsg_off;
12095}
12096
12097/*
12098 * "glob()" function
12099 */
12100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012105 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012107 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012109 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012110 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012112 if (argvars[1].v_type != VAR_UNKNOWN)
12113 {
12114 if (get_tv_number_chk(&argvars[1], &error))
12115 options |= WILD_KEEP_ALL;
12116 if (argvars[2].v_type != VAR_UNKNOWN
12117 && get_tv_number_chk(&argvars[2], &error))
12118 {
12119 rettv->v_type = VAR_LIST;
12120 rettv->vval.v_list = NULL;
12121 }
12122 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012123 if (!error)
12124 {
12125 ExpandInit(&xpc);
12126 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012127 if (p_wic)
12128 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012129 if (rettv->v_type == VAR_STRING)
12130 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012131 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012132 else if (rettv_list_alloc(rettv) != FAIL)
12133 {
12134 int i;
12135
12136 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12137 NULL, options, WILD_ALL_KEEP);
12138 for (i = 0; i < xpc.xp_numfiles; i++)
12139 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12140
12141 ExpandCleanup(&xpc);
12142 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012143 }
12144 else
12145 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146}
12147
12148/*
12149 * "globpath()" function
12150 */
12151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012153 typval_T *argvars;
12154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012156 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012159 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012160 garray_T ga;
12161 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012163 /* When the optional second argument is non-zero, don't remove matches
12164 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012166 if (argvars[2].v_type != VAR_UNKNOWN)
12167 {
12168 if (get_tv_number_chk(&argvars[2], &error))
12169 flags |= WILD_KEEP_ALL;
12170 if (argvars[3].v_type != VAR_UNKNOWN
12171 && get_tv_number_chk(&argvars[3], &error))
12172 {
12173 rettv->v_type = VAR_LIST;
12174 rettv->vval.v_list = NULL;
12175 }
12176 }
12177 if (file != NULL && !error)
12178 {
12179 ga_init2(&ga, (int)sizeof(char_u *), 10);
12180 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12181 if (rettv->v_type == VAR_STRING)
12182 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12183 else if (rettv_list_alloc(rettv) != FAIL)
12184 for (i = 0; i < ga.ga_len; ++i)
12185 list_append_string(rettv->vval.v_list,
12186 ((char_u **)(ga.ga_data))[i], -1);
12187 ga_clear_strings(&ga);
12188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191}
12192
12193/*
12194 * "has()" function
12195 */
12196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012198 typval_T *argvars;
12199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200{
12201 int i;
12202 char_u *name;
12203 int n = FALSE;
12204 static char *(has_list[]) =
12205 {
12206#ifdef AMIGA
12207 "amiga",
12208# ifdef FEAT_ARP
12209 "arp",
12210# endif
12211#endif
12212#ifdef __BEOS__
12213 "beos",
12214#endif
12215#ifdef MSDOS
12216# ifdef DJGPP
12217 "dos32",
12218# else
12219 "dos16",
12220# endif
12221#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012222#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 "mac",
12224#endif
12225#if defined(MACOS_X_UNIX)
12226 "macunix",
12227#endif
12228#ifdef OS2
12229 "os2",
12230#endif
12231#ifdef __QNX__
12232 "qnx",
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef UNIX
12235 "unix",
12236#endif
12237#ifdef VMS
12238 "vms",
12239#endif
12240#ifdef WIN16
12241 "win16",
12242#endif
12243#ifdef WIN32
12244 "win32",
12245#endif
12246#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12247 "win32unix",
12248#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012249#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 "win64",
12251#endif
12252#ifdef EBCDIC
12253 "ebcdic",
12254#endif
12255#ifndef CASE_INSENSITIVE_FILENAME
12256 "fname_case",
12257#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012258#ifdef HAVE_ACL
12259 "acl",
12260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261#ifdef FEAT_ARABIC
12262 "arabic",
12263#endif
12264#ifdef FEAT_AUTOCMD
12265 "autocmd",
12266#endif
12267#ifdef FEAT_BEVAL
12268 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012269# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12270 "balloon_multiline",
12271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#endif
12273#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12274 "builtin_terms",
12275# ifdef ALL_BUILTIN_TCAPS
12276 "all_builtin_terms",
12277# endif
12278#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012279#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12280 || defined(FEAT_GUI_W32) \
12281 || defined(FEAT_GUI_MOTIF))
12282 "browsefilter",
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef FEAT_BYTEOFF
12285 "byte_offset",
12286#endif
12287#ifdef FEAT_CINDENT
12288 "cindent",
12289#endif
12290#ifdef FEAT_CLIENTSERVER
12291 "clientserver",
12292#endif
12293#ifdef FEAT_CLIPBOARD
12294 "clipboard",
12295#endif
12296#ifdef FEAT_CMDL_COMPL
12297 "cmdline_compl",
12298#endif
12299#ifdef FEAT_CMDHIST
12300 "cmdline_hist",
12301#endif
12302#ifdef FEAT_COMMENTS
12303 "comments",
12304#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012305#ifdef FEAT_CONCEAL
12306 "conceal",
12307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308#ifdef FEAT_CRYPT
12309 "cryptv",
12310#endif
12311#ifdef FEAT_CSCOPE
12312 "cscope",
12313#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012314#ifdef FEAT_CURSORBIND
12315 "cursorbind",
12316#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012317#ifdef CURSOR_SHAPE
12318 "cursorshape",
12319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320#ifdef DEBUG
12321 "debug",
12322#endif
12323#ifdef FEAT_CON_DIALOG
12324 "dialog_con",
12325#endif
12326#ifdef FEAT_GUI_DIALOG
12327 "dialog_gui",
12328#endif
12329#ifdef FEAT_DIFF
12330 "diff",
12331#endif
12332#ifdef FEAT_DIGRAPHS
12333 "digraphs",
12334#endif
12335#ifdef FEAT_DND
12336 "dnd",
12337#endif
12338#ifdef FEAT_EMACS_TAGS
12339 "emacs_tags",
12340#endif
12341 "eval", /* always present, of course! */
12342#ifdef FEAT_EX_EXTRA
12343 "ex_extra",
12344#endif
12345#ifdef FEAT_SEARCH_EXTRA
12346 "extra_search",
12347#endif
12348#ifdef FEAT_FKMAP
12349 "farsi",
12350#endif
12351#ifdef FEAT_SEARCHPATH
12352 "file_in_path",
12353#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012354#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012355 "filterpipe",
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef FEAT_FIND_ID
12358 "find_in_path",
12359#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012360#ifdef FEAT_FLOAT
12361 "float",
12362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363#ifdef FEAT_FOLDING
12364 "folding",
12365#endif
12366#ifdef FEAT_FOOTER
12367 "footer",
12368#endif
12369#if !defined(USE_SYSTEM) && defined(UNIX)
12370 "fork",
12371#endif
12372#ifdef FEAT_GETTEXT
12373 "gettext",
12374#endif
12375#ifdef FEAT_GUI
12376 "gui",
12377#endif
12378#ifdef FEAT_GUI_ATHENA
12379# ifdef FEAT_GUI_NEXTAW
12380 "gui_neXtaw",
12381# else
12382 "gui_athena",
12383# endif
12384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385#ifdef FEAT_GUI_GTK
12386 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012389#ifdef FEAT_GUI_GNOME
12390 "gui_gnome",
12391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392#ifdef FEAT_GUI_MAC
12393 "gui_mac",
12394#endif
12395#ifdef FEAT_GUI_MOTIF
12396 "gui_motif",
12397#endif
12398#ifdef FEAT_GUI_PHOTON
12399 "gui_photon",
12400#endif
12401#ifdef FEAT_GUI_W16
12402 "gui_win16",
12403#endif
12404#ifdef FEAT_GUI_W32
12405 "gui_win32",
12406#endif
12407#ifdef FEAT_HANGULIN
12408 "hangul_input",
12409#endif
12410#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12411 "iconv",
12412#endif
12413#ifdef FEAT_INS_EXPAND
12414 "insert_expand",
12415#endif
12416#ifdef FEAT_JUMPLIST
12417 "jumplist",
12418#endif
12419#ifdef FEAT_KEYMAP
12420 "keymap",
12421#endif
12422#ifdef FEAT_LANGMAP
12423 "langmap",
12424#endif
12425#ifdef FEAT_LIBCALL
12426 "libcall",
12427#endif
12428#ifdef FEAT_LINEBREAK
12429 "linebreak",
12430#endif
12431#ifdef FEAT_LISP
12432 "lispindent",
12433#endif
12434#ifdef FEAT_LISTCMDS
12435 "listcmds",
12436#endif
12437#ifdef FEAT_LOCALMAP
12438 "localmap",
12439#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012440#ifdef FEAT_LUA
12441# ifndef DYNAMIC_LUA
12442 "lua",
12443# endif
12444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445#ifdef FEAT_MENU
12446 "menu",
12447#endif
12448#ifdef FEAT_SESSION
12449 "mksession",
12450#endif
12451#ifdef FEAT_MODIFY_FNAME
12452 "modify_fname",
12453#endif
12454#ifdef FEAT_MOUSE
12455 "mouse",
12456#endif
12457#ifdef FEAT_MOUSESHAPE
12458 "mouseshape",
12459#endif
12460#if defined(UNIX) || defined(VMS)
12461# ifdef FEAT_MOUSE_DEC
12462 "mouse_dec",
12463# endif
12464# ifdef FEAT_MOUSE_GPM
12465 "mouse_gpm",
12466# endif
12467# ifdef FEAT_MOUSE_JSB
12468 "mouse_jsbterm",
12469# endif
12470# ifdef FEAT_MOUSE_NET
12471 "mouse_netterm",
12472# endif
12473# ifdef FEAT_MOUSE_PTERM
12474 "mouse_pterm",
12475# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012476# ifdef FEAT_MOUSE_SGR
12477 "mouse_sgr",
12478# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012479# ifdef FEAT_SYSMOUSE
12480 "mouse_sysmouse",
12481# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012482# ifdef FEAT_MOUSE_URXVT
12483 "mouse_urxvt",
12484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485# ifdef FEAT_MOUSE_XTERM
12486 "mouse_xterm",
12487# endif
12488#endif
12489#ifdef FEAT_MBYTE
12490 "multi_byte",
12491#endif
12492#ifdef FEAT_MBYTE_IME
12493 "multi_byte_ime",
12494#endif
12495#ifdef FEAT_MULTI_LANG
12496 "multi_lang",
12497#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012498#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012499#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012500 "mzscheme",
12501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503#ifdef FEAT_OLE
12504 "ole",
12505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506#ifdef FEAT_PATH_EXTRA
12507 "path_extra",
12508#endif
12509#ifdef FEAT_PERL
12510#ifndef DYNAMIC_PERL
12511 "perl",
12512#endif
12513#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012514#ifdef FEAT_PERSISTENT_UNDO
12515 "persistent_undo",
12516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517#ifdef FEAT_PYTHON
12518#ifndef DYNAMIC_PYTHON
12519 "python",
12520#endif
12521#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012522#ifdef FEAT_PYTHON3
12523#ifndef DYNAMIC_PYTHON3
12524 "python3",
12525#endif
12526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527#ifdef FEAT_POSTSCRIPT
12528 "postscript",
12529#endif
12530#ifdef FEAT_PRINTER
12531 "printer",
12532#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012533#ifdef FEAT_PROFILE
12534 "profile",
12535#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012536#ifdef FEAT_RELTIME
12537 "reltime",
12538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539#ifdef FEAT_QUICKFIX
12540 "quickfix",
12541#endif
12542#ifdef FEAT_RIGHTLEFT
12543 "rightleft",
12544#endif
12545#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12546 "ruby",
12547#endif
12548#ifdef FEAT_SCROLLBIND
12549 "scrollbind",
12550#endif
12551#ifdef FEAT_CMDL_INFO
12552 "showcmd",
12553 "cmdline_info",
12554#endif
12555#ifdef FEAT_SIGNS
12556 "signs",
12557#endif
12558#ifdef FEAT_SMARTINDENT
12559 "smartindent",
12560#endif
12561#ifdef FEAT_SNIFF
12562 "sniff",
12563#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012564#ifdef STARTUPTIME
12565 "startuptime",
12566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567#ifdef FEAT_STL_OPT
12568 "statusline",
12569#endif
12570#ifdef FEAT_SUN_WORKSHOP
12571 "sun_workshop",
12572#endif
12573#ifdef FEAT_NETBEANS_INTG
12574 "netbeans_intg",
12575#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012576#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012577 "spell",
12578#endif
12579#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 "syntax",
12581#endif
12582#if defined(USE_SYSTEM) || !defined(UNIX)
12583 "system",
12584#endif
12585#ifdef FEAT_TAG_BINS
12586 "tag_binary",
12587#endif
12588#ifdef FEAT_TAG_OLDSTATIC
12589 "tag_old_static",
12590#endif
12591#ifdef FEAT_TAG_ANYWHITE
12592 "tag_any_white",
12593#endif
12594#ifdef FEAT_TCL
12595# ifndef DYNAMIC_TCL
12596 "tcl",
12597# endif
12598#endif
12599#ifdef TERMINFO
12600 "terminfo",
12601#endif
12602#ifdef FEAT_TERMRESPONSE
12603 "termresponse",
12604#endif
12605#ifdef FEAT_TEXTOBJ
12606 "textobjects",
12607#endif
12608#ifdef HAVE_TGETENT
12609 "tgetent",
12610#endif
12611#ifdef FEAT_TITLE
12612 "title",
12613#endif
12614#ifdef FEAT_TOOLBAR
12615 "toolbar",
12616#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012617#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12618 "unnamedplus",
12619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620#ifdef FEAT_USR_CMDS
12621 "user-commands", /* was accidentally included in 5.4 */
12622 "user_commands",
12623#endif
12624#ifdef FEAT_VIMINFO
12625 "viminfo",
12626#endif
12627#ifdef FEAT_VERTSPLIT
12628 "vertsplit",
12629#endif
12630#ifdef FEAT_VIRTUALEDIT
12631 "virtualedit",
12632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#ifdef FEAT_VISUALEXTRA
12635 "visualextra",
12636#endif
12637#ifdef FEAT_VREPLACE
12638 "vreplace",
12639#endif
12640#ifdef FEAT_WILDIGN
12641 "wildignore",
12642#endif
12643#ifdef FEAT_WILDMENU
12644 "wildmenu",
12645#endif
12646#ifdef FEAT_WINDOWS
12647 "windows",
12648#endif
12649#ifdef FEAT_WAK
12650 "winaltkeys",
12651#endif
12652#ifdef FEAT_WRITEBACKUP
12653 "writebackup",
12654#endif
12655#ifdef FEAT_XIM
12656 "xim",
12657#endif
12658#ifdef FEAT_XFONTSET
12659 "xfontset",
12660#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012661#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012662 "xpm",
12663 "xpm_w32", /* for backward compatibility */
12664#else
12665# if defined(HAVE_XPM)
12666 "xpm",
12667# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669#ifdef USE_XSMP
12670 "xsmp",
12671#endif
12672#ifdef USE_XSMP_INTERACT
12673 "xsmp_interact",
12674#endif
12675#ifdef FEAT_XCLIPBOARD
12676 "xterm_clipboard",
12677#endif
12678#ifdef FEAT_XTERM_SAVE
12679 "xterm_save",
12680#endif
12681#if defined(UNIX) && defined(FEAT_X11)
12682 "X11",
12683#endif
12684 NULL
12685 };
12686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 for (i = 0; has_list[i] != NULL; ++i)
12689 if (STRICMP(name, has_list[i]) == 0)
12690 {
12691 n = TRUE;
12692 break;
12693 }
12694
12695 if (n == FALSE)
12696 {
12697 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012698 {
12699 if (name[5] == '-'
12700 && STRLEN(name) > 11
12701 && vim_isdigit(name[6])
12702 && vim_isdigit(name[8])
12703 && vim_isdigit(name[10]))
12704 {
12705 int major = atoi((char *)name + 6);
12706 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012707
12708 /* Expect "patch-9.9.01234". */
12709 n = (major < VIM_VERSION_MAJOR
12710 || (major == VIM_VERSION_MAJOR
12711 && (minor < VIM_VERSION_MINOR
12712 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012713 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012714 }
12715 else
12716 n = has_patch(atoi((char *)name + 5));
12717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 else if (STRICMP(name, "vim_starting") == 0)
12719 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012720#ifdef FEAT_MBYTE
12721 else if (STRICMP(name, "multi_byte_encoding") == 0)
12722 n = has_mbyte;
12723#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012724#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12725 else if (STRICMP(name, "balloon_multiline") == 0)
12726 n = multiline_balloon_available();
12727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728#ifdef DYNAMIC_TCL
12729 else if (STRICMP(name, "tcl") == 0)
12730 n = tcl_enabled(FALSE);
12731#endif
12732#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12733 else if (STRICMP(name, "iconv") == 0)
12734 n = iconv_enabled(FALSE);
12735#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012736#ifdef DYNAMIC_LUA
12737 else if (STRICMP(name, "lua") == 0)
12738 n = lua_enabled(FALSE);
12739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012740#ifdef DYNAMIC_MZSCHEME
12741 else if (STRICMP(name, "mzscheme") == 0)
12742 n = mzscheme_enabled(FALSE);
12743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef DYNAMIC_RUBY
12745 else if (STRICMP(name, "ruby") == 0)
12746 n = ruby_enabled(FALSE);
12747#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012748#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749#ifdef DYNAMIC_PYTHON
12750 else if (STRICMP(name, "python") == 0)
12751 n = python_enabled(FALSE);
12752#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012753#endif
12754#ifdef FEAT_PYTHON3
12755#ifdef DYNAMIC_PYTHON3
12756 else if (STRICMP(name, "python3") == 0)
12757 n = python3_enabled(FALSE);
12758#endif
12759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760#ifdef DYNAMIC_PERL
12761 else if (STRICMP(name, "perl") == 0)
12762 n = perl_enabled(FALSE);
12763#endif
12764#ifdef FEAT_GUI
12765 else if (STRICMP(name, "gui_running") == 0)
12766 n = (gui.in_use || gui.starting);
12767# ifdef FEAT_GUI_W32
12768 else if (STRICMP(name, "gui_win32s") == 0)
12769 n = gui_is_win32s();
12770# endif
12771# ifdef FEAT_BROWSE
12772 else if (STRICMP(name, "browse") == 0)
12773 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12774# endif
12775#endif
12776#ifdef FEAT_SYN_HL
12777 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012778 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779#endif
12780#if defined(WIN3264)
12781 else if (STRICMP(name, "win95") == 0)
12782 n = mch_windows95();
12783#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012784#ifdef FEAT_NETBEANS_INTG
12785 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012786 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 }
12789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791}
12792
12793/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012794 * "has_key()" function
12795 */
12796 static void
12797f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012798 typval_T *argvars;
12799 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012800{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012801 if (argvars[0].v_type != VAR_DICT)
12802 {
12803 EMSG(_(e_dictreq));
12804 return;
12805 }
12806 if (argvars[0].vval.v_dict == NULL)
12807 return;
12808
12809 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012810 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012811}
12812
12813/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012814 * "haslocaldir()" function
12815 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012816 static void
12817f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012818 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012819 typval_T *rettv;
12820{
12821 rettv->vval.v_number = (curwin->w_localdir != NULL);
12822}
12823
12824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 * "hasmapto()" function
12826 */
12827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
12832 char_u *name;
12833 char_u *mode;
12834 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012835 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 mode = (char_u *)"nvo";
12840 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012843 if (argvars[2].v_type != VAR_UNKNOWN)
12844 abbr = get_tv_number(&argvars[2]);
12845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846
Bram Moolenaar2c932302006-03-18 21:42:09 +000012847 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
12854 * "histadd()" function
12855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012858 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860{
12861#ifdef FEAT_CMDHIST
12862 int histype;
12863 char_u *str;
12864 char_u buf[NUMBUFLEN];
12865#endif
12866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 if (check_restricted() || check_secure())
12869 return;
12870#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12872 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 if (histype >= 0)
12874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 if (*str != NUL)
12877 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012878 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 return;
12882 }
12883 }
12884#endif
12885}
12886
12887/*
12888 * "histdel()" function
12889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012892 typval_T *argvars UNUSED;
12893 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894{
12895#ifdef FEAT_CMDHIST
12896 int n;
12897 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12901 if (str == NULL)
12902 n = 0;
12903 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012905 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012906 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 else
12911 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012912 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 get_tv_string_buf(&argvars[1], buf));
12914 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915#endif
12916}
12917
12918/*
12919 * "histget()" function
12920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
12926#ifdef FEAT_CMDHIST
12927 int type;
12928 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12932 if (str == NULL)
12933 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012935 {
12936 type = get_histtype(str);
12937 if (argvars[1].v_type == VAR_UNKNOWN)
12938 idx = get_history_idx(type);
12939 else
12940 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12941 /* -1 on type error */
12942 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948}
12949
12950/*
12951 * "histnr()" function
12952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012955 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957{
12958 int i;
12959
12960#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 char_u *history = get_tv_string_chk(&argvars[0]);
12962
12963 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 if (i >= HIST_CMD && i < HIST_COUNT)
12965 i = get_history_idx(i);
12966 else
12967#endif
12968 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970}
12971
12972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 * "highlightID(name)" function
12974 */
12975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012977 typval_T *argvars;
12978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981}
12982
12983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012984 * "highlight_exists()" function
12985 */
12986 static void
12987f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012988 typval_T *argvars;
12989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012990{
12991 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12992}
12993
12994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 * "hostname()" function
12996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001{
13002 char_u hostname[256];
13003
13004 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013005 rettv->v_type = VAR_STRING;
13006 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007}
13008
13009/*
13010 * iconv() function
13011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016{
13017#ifdef FEAT_MBYTE
13018 char_u buf1[NUMBUFLEN];
13019 char_u buf2[NUMBUFLEN];
13020 char_u *from, *to, *str;
13021 vimconv_T vimconv;
13022#endif
13023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->v_type = VAR_STRING;
13025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
13027#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 str = get_tv_string(&argvars[0]);
13029 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13030 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 vimconv.vc_type = CONV_NONE;
13032 convert_setup(&vimconv, from, to);
13033
13034 /* If the encodings are equal, no conversion needed. */
13035 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039
13040 convert_setup(&vimconv, NULL, NULL);
13041 vim_free(from);
13042 vim_free(to);
13043#endif
13044}
13045
13046/*
13047 * "indent()" function
13048 */
13049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
13054 linenr_T lnum;
13055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061}
13062
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013063/*
13064 * "index()" function
13065 */
13066 static void
13067f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013068 typval_T *argvars;
13069 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013070{
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 list_T *l;
13072 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013073 long idx = 0;
13074 int ic = FALSE;
13075
13076 rettv->vval.v_number = -1;
13077 if (argvars[0].v_type != VAR_LIST)
13078 {
13079 EMSG(_(e_listreq));
13080 return;
13081 }
13082 l = argvars[0].vval.v_list;
13083 if (l != NULL)
13084 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013085 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 int error = FALSE;
13089
Bram Moolenaar758711c2005-02-02 23:11:38 +000013090 /* Start at specified item. Use the cached index that list_find()
13091 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013093 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013094 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095 ic = get_tv_number_chk(&argvars[3], &error);
13096 if (error)
13097 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013099
Bram Moolenaar758711c2005-02-02 23:11:38 +000013100 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013101 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013102 {
13103 rettv->vval.v_number = idx;
13104 break;
13105 }
13106 }
13107}
13108
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109static int inputsecret_flag = 0;
13110
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013111static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13112
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013114 * This function is used by f_input() and f_inputdialog() functions. The third
13115 * argument to f_input() specifies the type of completion to use at the
13116 * prompt. The third argument to f_inputdialog() specifies the value to return
13117 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 */
13119 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013120get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *argvars;
13122 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013123 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 char_u *p = NULL;
13127 int c;
13128 char_u buf[NUMBUFLEN];
13129 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013130 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013131 int xp_type = EXPAND_NOTHING;
13132 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013135 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
13137#ifdef NO_CONSOLE_INPUT
13138 /* While starting up, there is no place to enter text. */
13139 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141#endif
13142
13143 cmd_silent = FALSE; /* Want to see the prompt. */
13144 if (prompt != NULL)
13145 {
13146 /* Only the part of the message after the last NL is considered as
13147 * prompt for the command line */
13148 p = vim_strrchr(prompt, '\n');
13149 if (p == NULL)
13150 p = prompt;
13151 else
13152 {
13153 ++p;
13154 c = *p;
13155 *p = NUL;
13156 msg_start();
13157 msg_clr_eos();
13158 msg_puts_attr(prompt, echo_attr);
13159 msg_didout = FALSE;
13160 msg_starthere();
13161 *p = c;
13162 }
13163 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 if (argvars[1].v_type != VAR_UNKNOWN)
13166 {
13167 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13168 if (defstr != NULL)
13169 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013171 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013172 {
13173 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013174 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013175 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013176
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013177 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013178 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013179
Bram Moolenaar4463f292005-09-25 22:20:24 +000013180 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13181 if (xp_name == NULL)
13182 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013184 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013185
Bram Moolenaar4463f292005-09-25 22:20:24 +000013186 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13187 &xp_arg) == FAIL)
13188 return;
13189 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013190 }
13191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013193 {
13194# ifdef FEAT_EX_EXTRA
13195 int save_ex_normal_busy = ex_normal_busy;
13196 ex_normal_busy = 0;
13197# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013199 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13200 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013201# ifdef FEAT_EX_EXTRA
13202 ex_normal_busy = save_ex_normal_busy;
13203# endif
13204 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013205 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013206 && argvars[1].v_type != VAR_UNKNOWN
13207 && argvars[2].v_type != VAR_UNKNOWN)
13208 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13209 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013210
13211 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213 /* since the user typed this, no need to wait for return */
13214 need_wait_return = FALSE;
13215 msg_didout = FALSE;
13216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 cmd_silent = cmd_silent_save;
13218}
13219
13220/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013221 * "input()" function
13222 * Also handles inputsecret() when inputsecret is set.
13223 */
13224 static void
13225f_input(argvars, rettv)
13226 typval_T *argvars;
13227 typval_T *rettv;
13228{
13229 get_user_input(argvars, rettv, FALSE);
13230}
13231
13232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 * "inputdialog()" function
13234 */
13235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 typval_T *argvars;
13238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239{
13240#if defined(FEAT_GUI_TEXTDIALOG)
13241 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13242 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13243 {
13244 char_u *message;
13245 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013246 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013248 message = get_tv_string_chk(&argvars[0]);
13249 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013250 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013251 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 else
13253 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 if (message != NULL && defstr != NULL
13255 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013256 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
13259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 if (message != NULL && defstr != NULL
13261 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_string = vim_strsave(
13264 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 }
13270 else
13271#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013272 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273}
13274
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013275/*
13276 * "inputlist()" function
13277 */
13278 static void
13279f_inputlist(argvars, rettv)
13280 typval_T *argvars;
13281 typval_T *rettv;
13282{
13283 listitem_T *li;
13284 int selected;
13285 int mouse_used;
13286
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013287#ifdef NO_CONSOLE_INPUT
13288 /* While starting up, there is no place to enter text. */
13289 if (no_console_input())
13290 return;
13291#endif
13292 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13293 {
13294 EMSG2(_(e_listarg), "inputlist()");
13295 return;
13296 }
13297
13298 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013299 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013300 lines_left = Rows; /* avoid more prompt */
13301 msg_scroll = TRUE;
13302 msg_clr_eos();
13303
13304 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13305 {
13306 msg_puts(get_tv_string(&li->li_tv));
13307 msg_putchar('\n');
13308 }
13309
13310 /* Ask for choice. */
13311 selected = prompt_for_number(&mouse_used);
13312 if (mouse_used)
13313 selected -= lines_left;
13314
13315 rettv->vval.v_number = selected;
13316}
13317
13318
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13320
13321/*
13322 * "inputrestore()" function
13323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013326 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328{
13329 if (ga_userinput.ga_len > 0)
13330 {
13331 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13333 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013334 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 }
13336 else if (p_verbose > 1)
13337 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013338 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 }
13341}
13342
13343/*
13344 * "inputsave()" function
13345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013351 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 if (ga_grow(&ga_userinput, 1) == OK)
13353 {
13354 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13355 + ga_userinput.ga_len);
13356 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013357 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 }
13359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361}
13362
13363/*
13364 * "inputsecret()" function
13365 */
13366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *argvars;
13369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370{
13371 ++cmdline_star;
13372 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 --cmdline_star;
13375 --inputsecret_flag;
13376}
13377
13378/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379 * "insert()" function
13380 */
13381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *argvars;
13384 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013385{
13386 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 listitem_T *item;
13388 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013390
13391 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013393 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013394 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013395 {
13396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 before = get_tv_number_chk(&argvars[2], &error);
13398 if (error)
13399 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013400
Bram Moolenaar758711c2005-02-02 23:11:38 +000013401 if (before == l->lv_len)
13402 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013403 else
13404 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013405 item = list_find(l, before);
13406 if (item == NULL)
13407 {
13408 EMSGN(_(e_listidx), before);
13409 l = NULL;
13410 }
13411 }
13412 if (l != NULL)
13413 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013414 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013415 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013416 }
13417 }
13418}
13419
13420/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013421 * "invert(expr)" function
13422 */
13423 static void
13424f_invert(argvars, rettv)
13425 typval_T *argvars;
13426 typval_T *rettv;
13427{
13428 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13429}
13430
13431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 * "isdirectory()" function
13433 */
13434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440}
13441
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013442/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013443 * "islocked()" function
13444 */
13445 static void
13446f_islocked(argvars, rettv)
13447 typval_T *argvars;
13448 typval_T *rettv;
13449{
13450 lval_T lv;
13451 char_u *end;
13452 dictitem_T *di;
13453
13454 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013455 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13456 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013457 if (end != NULL && lv.ll_name != NULL)
13458 {
13459 if (*end != NUL)
13460 EMSG(_(e_trailing));
13461 else
13462 {
13463 if (lv.ll_tv == NULL)
13464 {
13465 if (check_changedtick(lv.ll_name))
13466 rettv->vval.v_number = 1; /* always locked */
13467 else
13468 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013469 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013470 if (di != NULL)
13471 {
13472 /* Consider a variable locked when:
13473 * 1. the variable itself is locked
13474 * 2. the value of the variable is locked.
13475 * 3. the List or Dict value is locked.
13476 */
13477 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13478 || tv_islocked(&di->di_tv));
13479 }
13480 }
13481 }
13482 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013483 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013484 else if (lv.ll_newkey != NULL)
13485 EMSG2(_(e_dictkey), lv.ll_newkey);
13486 else if (lv.ll_list != NULL)
13487 /* List item. */
13488 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13489 else
13490 /* Dictionary item. */
13491 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13492 }
13493 }
13494
13495 clear_lval(&lv);
13496}
13497
Bram Moolenaar33570922005-01-25 22:26:29 +000013498static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499
13500/*
13501 * Turn a dict into a list:
13502 * "what" == 0: list of keys
13503 * "what" == 1: list of values
13504 * "what" == 2: list of items
13505 */
13506 static void
13507dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013510 int what;
13511{
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 list_T *l2;
13513 dictitem_T *di;
13514 hashitem_T *hi;
13515 listitem_T *li;
13516 listitem_T *li2;
13517 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013518 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013519
Bram Moolenaar8c711452005-01-14 21:53:12 +000013520 if (argvars[0].v_type != VAR_DICT)
13521 {
13522 EMSG(_(e_dictreq));
13523 return;
13524 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013525 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013526 return;
13527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013528 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013529 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013530
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013531 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013534 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013536 --todo;
13537 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013538
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013539 li = listitem_alloc();
13540 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013541 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013542 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013544 if (what == 0)
13545 {
13546 /* keys() */
13547 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013548 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013549 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13550 }
13551 else if (what == 1)
13552 {
13553 /* values() */
13554 copy_tv(&di->di_tv, &li->li_tv);
13555 }
13556 else
13557 {
13558 /* items() */
13559 l2 = list_alloc();
13560 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013561 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013562 li->li_tv.vval.v_list = l2;
13563 if (l2 == NULL)
13564 break;
13565 ++l2->lv_refcount;
13566
13567 li2 = listitem_alloc();
13568 if (li2 == NULL)
13569 break;
13570 list_append(l2, li2);
13571 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013572 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013573 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13574
13575 li2 = listitem_alloc();
13576 if (li2 == NULL)
13577 break;
13578 list_append(l2, li2);
13579 copy_tv(&di->di_tv, &li2->li_tv);
13580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013581 }
13582 }
13583}
13584
13585/*
13586 * "items(dict)" function
13587 */
13588 static void
13589f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *argvars;
13591 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013592{
13593 dict_list(argvars, rettv, 2);
13594}
13595
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013597 * "join()" function
13598 */
13599 static void
13600f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013603{
13604 garray_T ga;
13605 char_u *sep;
13606
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013607 if (argvars[0].v_type != VAR_LIST)
13608 {
13609 EMSG(_(e_listreq));
13610 return;
13611 }
13612 if (argvars[0].vval.v_list == NULL)
13613 return;
13614 if (argvars[1].v_type == VAR_UNKNOWN)
13615 sep = (char_u *)" ";
13616 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013618
13619 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620
13621 if (sep != NULL)
13622 {
13623 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013624 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 ga_append(&ga, NUL);
13626 rettv->vval.v_string = (char_u *)ga.ga_data;
13627 }
13628 else
13629 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013630}
13631
13632/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013633 * "keys()" function
13634 */
13635 static void
13636f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *argvars;
13638 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013639{
13640 dict_list(argvars, rettv, 0);
13641}
13642
13643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 * "last_buffer_nr()" function.
13645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013648 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
13651 int n = 0;
13652 buf_T *buf;
13653
13654 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13655 if (n < buf->b_fnum)
13656 n = buf->b_fnum;
13657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013659}
13660
13661/*
13662 * "len()" function
13663 */
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013668{
13669 switch (argvars[0].v_type)
13670 {
13671 case VAR_STRING:
13672 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->vval.v_number = (varnumber_T)STRLEN(
13674 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013675 break;
13676 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013679 case VAR_DICT:
13680 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13681 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013682 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013683 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013684 break;
13685 }
13686}
13687
Bram Moolenaar33570922005-01-25 22:26:29 +000013688static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013689
13690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 typval_T *argvars;
13693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 int type;
13695{
13696#ifdef FEAT_LIBCALL
13697 char_u *string_in;
13698 char_u **string_result;
13699 int nr_result;
13700#endif
13701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013703 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013705
13706 if (check_restricted() || check_secure())
13707 return;
13708
13709#ifdef FEAT_LIBCALL
13710 /* The first two args must be strings, otherwise its meaningless */
13711 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13712 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013713 string_in = NULL;
13714 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013715 string_in = argvars[2].vval.v_string;
13716 if (type == VAR_NUMBER)
13717 string_result = NULL;
13718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013720 if (mch_libcall(argvars[0].vval.v_string,
13721 argvars[1].vval.v_string,
13722 string_in,
13723 argvars[2].vval.v_number,
13724 string_result,
13725 &nr_result) == OK
13726 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013727 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013728 }
13729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730}
13731
13732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 * "libcall()" function
13734 */
13735 static void
13736f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013737 typval_T *argvars;
13738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013739{
13740 libcall_common(argvars, rettv, VAR_STRING);
13741}
13742
13743/*
13744 * "libcallnr()" function
13745 */
13746 static void
13747f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T *argvars;
13749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750{
13751 libcall_common(argvars, rettv, VAR_NUMBER);
13752}
13753
13754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 * "line(string)" function
13756 */
13757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *argvars;
13760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
13762 linenr_T lnum = 0;
13763 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013764 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013766 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 if (fp != NULL)
13768 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013769 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770}
13771
13772/*
13773 * "line2byte(lnum)" function
13774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013777 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779{
13780#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013781 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782#else
13783 linenr_T lnum;
13784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013785 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013789 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13790 if (rettv->vval.v_number >= 0)
13791 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#endif
13793}
13794
13795/*
13796 * "lispindent(lnum)" function
13797 */
13798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013799f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802{
13803#ifdef FEAT_LISP
13804 pos_T pos;
13805 linenr_T lnum;
13806
13807 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13810 {
13811 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 curwin->w_cursor = pos;
13814 }
13815 else
13816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818}
13819
13820/*
13821 * "localtime()" function
13822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013828 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829}
13830
Bram Moolenaar33570922005-01-25 22:26:29 +000013831static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 typval_T *argvars;
13836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 int exact;
13838{
13839 char_u *keys;
13840 char_u *which;
13841 char_u buf[NUMBUFLEN];
13842 char_u *keys_buf = NULL;
13843 char_u *rhs;
13844 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013845 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013846 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013847 mapblock_T *mp;
13848 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
13850 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851 rettv->v_type = VAR_STRING;
13852 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 if (*keys == NUL)
13856 return;
13857
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013858 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013862 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013863 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013864 if (argvars[3].v_type != VAR_UNKNOWN)
13865 get_dict = get_tv_number(&argvars[3]);
13866 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 else
13869 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 if (which == NULL)
13871 return;
13872
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 mode = get_map_mode(&which, 0);
13874
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013875 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013876 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013878
13879 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013881 /* Return a string. */
13882 if (rhs != NULL)
13883 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaarbd743252010-10-20 21:23:33 +020013885 }
13886 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13887 {
13888 /* Return a dictionary. */
13889 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13890 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13891 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892
Bram Moolenaarbd743252010-10-20 21:23:33 +020013893 dict_add_nr_str(dict, "lhs", 0L, lhs);
13894 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13895 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13896 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13897 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13898 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13899 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013900 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013901 dict_add_nr_str(dict, "mode", 0L, mapmode);
13902
13903 vim_free(lhs);
13904 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 }
13906}
13907
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013908#ifdef FEAT_FLOAT
13909/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013910 * "log()" function
13911 */
13912 static void
13913f_log(argvars, rettv)
13914 typval_T *argvars;
13915 typval_T *rettv;
13916{
13917 float_T f;
13918
13919 rettv->v_type = VAR_FLOAT;
13920 if (get_float_arg(argvars, &f) == OK)
13921 rettv->vval.v_float = log(f);
13922 else
13923 rettv->vval.v_float = 0.0;
13924}
13925
13926/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013927 * "log10()" function
13928 */
13929 static void
13930f_log10(argvars, rettv)
13931 typval_T *argvars;
13932 typval_T *rettv;
13933{
13934 float_T f;
13935
13936 rettv->v_type = VAR_FLOAT;
13937 if (get_float_arg(argvars, &f) == OK)
13938 rettv->vval.v_float = log10(f);
13939 else
13940 rettv->vval.v_float = 0.0;
13941}
13942#endif
13943
Bram Moolenaar1dced572012-04-05 16:54:08 +020013944#ifdef FEAT_LUA
13945/*
13946 * "luaeval()" function
13947 */
13948 static void
13949f_luaeval(argvars, rettv)
13950 typval_T *argvars;
13951 typval_T *rettv;
13952{
13953 char_u *str;
13954 char_u buf[NUMBUFLEN];
13955
13956 str = get_tv_string_buf(&argvars[0], buf);
13957 do_luaeval(str, argvars + 1, rettv);
13958}
13959#endif
13960
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962 * "map()" function
13963 */
13964 static void
13965f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013966 typval_T *argvars;
13967 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968{
13969 filter_map(argvars, rettv, TRUE);
13970}
13971
13972/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013973 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 */
13975 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013977 typval_T *argvars;
13978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981}
13982
13983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013984 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 */
13986 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013987f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013988 typval_T *argvars;
13989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992}
13993
Bram Moolenaar33570922005-01-25 22:26:29 +000013994static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995
13996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 typval_T *argvars;
13999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 int type;
14001{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014003 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014004 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 char_u *pat;
14006 regmatch_T regmatch;
14007 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 char_u *save_cpo;
14010 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014011 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014012 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014013 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014014 list_T *l = NULL;
14015 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014017 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018
14019 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14020 save_cpo = p_cpo;
14021 p_cpo = (char_u *)"";
14022
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014023 rettv->vval.v_number = -1;
14024 if (type == 3)
14025 {
14026 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014027 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014028 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014029 }
14030 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->v_type = VAR_STRING;
14033 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014036 if (argvars[0].v_type == VAR_LIST)
14037 {
14038 if ((l = argvars[0].vval.v_list) == NULL)
14039 goto theend;
14040 li = l->lv_first;
14041 }
14042 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014043 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014045 len = (long)STRLEN(str);
14046 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14049 if (pat == NULL)
14050 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014051
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054 int error = FALSE;
14055
14056 start = get_tv_number_chk(&argvars[2], &error);
14057 if (error)
14058 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014059 if (l != NULL)
14060 {
14061 li = list_find(l, start);
14062 if (li == NULL)
14063 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014064 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014065 }
14066 else
14067 {
14068 if (start < 0)
14069 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014070 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014071 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014072 /* When "count" argument is there ignore matches before "start",
14073 * otherwise skip part of the string. Differs when pattern is "^"
14074 * or "\<". */
14075 if (argvars[3].v_type != VAR_UNKNOWN)
14076 startcol = start;
14077 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014078 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014079 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014080 len -= start;
14081 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014082 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014083
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014084 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 nth = get_tv_number_chk(&argvars[3], &error);
14086 if (error)
14087 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 }
14089
14090 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14091 if (regmatch.regprog != NULL)
14092 {
14093 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014094
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014095 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014096 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014097 if (l != NULL)
14098 {
14099 if (li == NULL)
14100 {
14101 match = FALSE;
14102 break;
14103 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014104 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014105 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014106 if (str == NULL)
14107 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014108 }
14109
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014110 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014111
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014112 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014113 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014114 if (l == NULL && !match)
14115 break;
14116
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014117 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014118 if (l != NULL)
14119 {
14120 li = li->li_next;
14121 ++idx;
14122 }
14123 else
14124 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014125#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014126 startcol = (colnr_T)(regmatch.startp[0]
14127 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014128#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014129 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014130#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014131 if (startcol > (colnr_T)len
14132 || str + startcol <= regmatch.startp[0])
14133 {
14134 match = FALSE;
14135 break;
14136 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014137 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014138 }
14139
14140 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014142 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014144 int i;
14145
14146 /* return list with matched string and submatches */
14147 for (i = 0; i < NSUBEXP; ++i)
14148 {
14149 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014150 {
14151 if (list_append_string(rettv->vval.v_list,
14152 (char_u *)"", 0) == FAIL)
14153 break;
14154 }
14155 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014156 regmatch.startp[i],
14157 (int)(regmatch.endp[i] - regmatch.startp[i]))
14158 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014159 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014160 }
14161 }
14162 else if (type == 2)
14163 {
14164 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014165 if (l != NULL)
14166 copy_tv(&li->li_tv, rettv);
14167 else
14168 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014170 }
14171 else if (l != NULL)
14172 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 else
14174 {
14175 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 (varnumber_T)(regmatch.startp[0] - str);
14178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014181 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 }
14183 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014184 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 }
14186
14187theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014188 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 p_cpo = save_cpo;
14190}
14191
14192/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193 * "match()" function
14194 */
14195 static void
14196f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014197 typval_T *argvars;
14198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199{
14200 find_some_match(argvars, rettv, 1);
14201}
14202
14203/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014204 * "matchadd()" function
14205 */
14206 static void
14207f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014208 typval_T *argvars UNUSED;
14209 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014210{
14211#ifdef FEAT_SEARCH_EXTRA
14212 char_u buf[NUMBUFLEN];
14213 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14214 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14215 int prio = 10; /* default priority */
14216 int id = -1;
14217 int error = FALSE;
14218
14219 rettv->vval.v_number = -1;
14220
14221 if (grp == NULL || pat == NULL)
14222 return;
14223 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014224 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014225 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014226 if (argvars[3].v_type != VAR_UNKNOWN)
14227 id = get_tv_number_chk(&argvars[3], &error);
14228 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014229 if (error == TRUE)
14230 return;
14231 if (id >= 1 && id <= 3)
14232 {
14233 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14234 return;
14235 }
14236
14237 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14238#endif
14239}
14240
14241/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014242 * "matcharg()" function
14243 */
14244 static void
14245f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014246 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014247 typval_T *rettv;
14248{
14249 if (rettv_list_alloc(rettv) == OK)
14250 {
14251#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014252 int id = get_tv_number(&argvars[0]);
14253 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014254
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014255 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014256 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014257 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14258 {
14259 list_append_string(rettv->vval.v_list,
14260 syn_id2name(m->hlg_id), -1);
14261 list_append_string(rettv->vval.v_list, m->pattern, -1);
14262 }
14263 else
14264 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014265 list_append_string(rettv->vval.v_list, NULL, -1);
14266 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014267 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014268 }
14269#endif
14270 }
14271}
14272
14273/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014274 * "matchdelete()" function
14275 */
14276 static void
14277f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014278 typval_T *argvars UNUSED;
14279 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014280{
14281#ifdef FEAT_SEARCH_EXTRA
14282 rettv->vval.v_number = match_delete(curwin,
14283 (int)get_tv_number(&argvars[0]), TRUE);
14284#endif
14285}
14286
14287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288 * "matchend()" function
14289 */
14290 static void
14291f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014292 typval_T *argvars;
14293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014294{
14295 find_some_match(argvars, rettv, 0);
14296}
14297
14298/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014299 * "matchlist()" function
14300 */
14301 static void
14302f_matchlist(argvars, rettv)
14303 typval_T *argvars;
14304 typval_T *rettv;
14305{
14306 find_some_match(argvars, rettv, 3);
14307}
14308
14309/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 * "matchstr()" function
14311 */
14312 static void
14313f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 typval_T *argvars;
14315 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316{
14317 find_some_match(argvars, rettv, 2);
14318}
14319
Bram Moolenaar33570922005-01-25 22:26:29 +000014320static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014321
14322 static void
14323max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014324 typval_T *argvars;
14325 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014326 int domax;
14327{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014328 long n = 0;
14329 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014331
14332 if (argvars[0].v_type == VAR_LIST)
14333 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014334 list_T *l;
14335 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014336
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014337 l = argvars[0].vval.v_list;
14338 if (l != NULL)
14339 {
14340 li = l->lv_first;
14341 if (li != NULL)
14342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014344 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014345 {
14346 li = li->li_next;
14347 if (li == NULL)
14348 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014350 if (domax ? i > n : i < n)
14351 n = i;
14352 }
14353 }
14354 }
14355 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014356 else if (argvars[0].v_type == VAR_DICT)
14357 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014358 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014359 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014361 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014362
14363 d = argvars[0].vval.v_dict;
14364 if (d != NULL)
14365 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014366 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014367 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014371 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014373 if (first)
14374 {
14375 n = i;
14376 first = FALSE;
14377 }
14378 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014379 n = i;
14380 }
14381 }
14382 }
14383 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014384 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014385 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014387}
14388
14389/*
14390 * "max()" function
14391 */
14392 static void
14393f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014394 typval_T *argvars;
14395 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014396{
14397 max_min(argvars, rettv, TRUE);
14398}
14399
14400/*
14401 * "min()" function
14402 */
14403 static void
14404f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014405 typval_T *argvars;
14406 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014407{
14408 max_min(argvars, rettv, FALSE);
14409}
14410
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014411static int mkdir_recurse __ARGS((char_u *dir, int prot));
14412
14413/*
14414 * Create the directory in which "dir" is located, and higher levels when
14415 * needed.
14416 */
14417 static int
14418mkdir_recurse(dir, prot)
14419 char_u *dir;
14420 int prot;
14421{
14422 char_u *p;
14423 char_u *updir;
14424 int r = FAIL;
14425
14426 /* Get end of directory name in "dir".
14427 * We're done when it's "/" or "c:/". */
14428 p = gettail_sep(dir);
14429 if (p <= get_past_head(dir))
14430 return OK;
14431
14432 /* If the directory exists we're done. Otherwise: create it.*/
14433 updir = vim_strnsave(dir, (int)(p - dir));
14434 if (updir == NULL)
14435 return FAIL;
14436 if (mch_isdir(updir))
14437 r = OK;
14438 else if (mkdir_recurse(updir, prot) == OK)
14439 r = vim_mkdir_emsg(updir, prot);
14440 vim_free(updir);
14441 return r;
14442}
14443
14444#ifdef vim_mkdir
14445/*
14446 * "mkdir()" function
14447 */
14448 static void
14449f_mkdir(argvars, rettv)
14450 typval_T *argvars;
14451 typval_T *rettv;
14452{
14453 char_u *dir;
14454 char_u buf[NUMBUFLEN];
14455 int prot = 0755;
14456
14457 rettv->vval.v_number = FAIL;
14458 if (check_restricted() || check_secure())
14459 return;
14460
14461 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014462 if (*dir == NUL)
14463 rettv->vval.v_number = FAIL;
14464 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014465 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014466 if (*gettail(dir) == NUL)
14467 /* remove trailing slashes */
14468 *gettail_sep(dir) = NUL;
14469
14470 if (argvars[1].v_type != VAR_UNKNOWN)
14471 {
14472 if (argvars[2].v_type != VAR_UNKNOWN)
14473 prot = get_tv_number_chk(&argvars[2], NULL);
14474 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14475 mkdir_recurse(dir, prot);
14476 }
14477 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014478 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014479}
14480#endif
14481
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 * "mode()" function
14484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 typval_T *argvars;
14488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014490 char_u buf[3];
14491
14492 buf[1] = NUL;
14493 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 if (VIsual_active)
14496 {
14497 if (VIsual_select)
14498 buf[0] = VIsual_mode + 's' - 'v';
14499 else
14500 buf[0] = VIsual_mode;
14501 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014502 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014503 || State == CONFIRM)
14504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014506 if (State == ASKMORE)
14507 buf[1] = 'm';
14508 else if (State == CONFIRM)
14509 buf[1] = '?';
14510 }
14511 else if (State == EXTERNCMD)
14512 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 else if (State & INSERT)
14514 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014515#ifdef FEAT_VREPLACE
14516 if (State & VREPLACE_FLAG)
14517 {
14518 buf[0] = 'R';
14519 buf[1] = 'v';
14520 }
14521 else
14522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 if (State & REPLACE_FLAG)
14524 buf[0] = 'R';
14525 else
14526 buf[0] = 'i';
14527 }
14528 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014531 if (exmode_active)
14532 buf[1] = 'v';
14533 }
14534 else if (exmode_active)
14535 {
14536 buf[0] = 'c';
14537 buf[1] = 'e';
14538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014542 if (finish_op)
14543 buf[1] = 'o';
14544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014546 /* Clear out the minor mode when the argument is not a non-zero number or
14547 * non-empty string. */
14548 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014549 buf[1] = NUL;
14550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014551 rettv->vval.v_string = vim_strsave(buf);
14552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553}
14554
Bram Moolenaar429fa852013-04-15 12:27:36 +020014555#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014556/*
14557 * "mzeval()" function
14558 */
14559 static void
14560f_mzeval(argvars, rettv)
14561 typval_T *argvars;
14562 typval_T *rettv;
14563{
14564 char_u *str;
14565 char_u buf[NUMBUFLEN];
14566
14567 str = get_tv_string_buf(&argvars[0], buf);
14568 do_mzeval(str, rettv);
14569}
Bram Moolenaar75676462013-01-30 14:55:42 +010014570
14571 void
14572mzscheme_call_vim(name, args, rettv)
14573 char_u *name;
14574 typval_T *args;
14575 typval_T *rettv;
14576{
14577 typval_T argvars[3];
14578
14579 argvars[0].v_type = VAR_STRING;
14580 argvars[0].vval.v_string = name;
14581 copy_tv(args, &argvars[1]);
14582 argvars[2].v_type = VAR_UNKNOWN;
14583 f_call(argvars, rettv);
14584 clear_tv(&argvars[1]);
14585}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014586#endif
14587
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589 * "nextnonblank()" function
14590 */
14591 static void
14592f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 typval_T *argvars;
14594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595{
14596 linenr_T lnum;
14597
14598 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601 {
14602 lnum = 0;
14603 break;
14604 }
14605 if (*skipwhite(ml_get(lnum)) != NUL)
14606 break;
14607 }
14608 rettv->vval.v_number = lnum;
14609}
14610
14611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 * "nr2char()" function
14613 */
14614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 typval_T *argvars;
14617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618{
14619 char_u buf[NUMBUFLEN];
14620
14621#ifdef FEAT_MBYTE
14622 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014623 {
14624 int utf8 = 0;
14625
14626 if (argvars[1].v_type != VAR_UNKNOWN)
14627 utf8 = get_tv_number_chk(&argvars[1], NULL);
14628 if (utf8)
14629 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14630 else
14631 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 else
14634#endif
14635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 buf[1] = NUL;
14638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv->v_type = VAR_STRING;
14640 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014641}
14642
14643/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014644 * "or(expr, expr)" function
14645 */
14646 static void
14647f_or(argvars, rettv)
14648 typval_T *argvars;
14649 typval_T *rettv;
14650{
14651 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14652 | get_tv_number_chk(&argvars[1], NULL);
14653}
14654
14655/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014656 * "pathshorten()" function
14657 */
14658 static void
14659f_pathshorten(argvars, rettv)
14660 typval_T *argvars;
14661 typval_T *rettv;
14662{
14663 char_u *p;
14664
14665 rettv->v_type = VAR_STRING;
14666 p = get_tv_string_chk(&argvars[0]);
14667 if (p == NULL)
14668 rettv->vval.v_string = NULL;
14669 else
14670 {
14671 p = vim_strsave(p);
14672 rettv->vval.v_string = p;
14673 if (p != NULL)
14674 shorten_dir(p);
14675 }
14676}
14677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014678#ifdef FEAT_FLOAT
14679/*
14680 * "pow()" function
14681 */
14682 static void
14683f_pow(argvars, rettv)
14684 typval_T *argvars;
14685 typval_T *rettv;
14686{
14687 float_T fx, fy;
14688
14689 rettv->v_type = VAR_FLOAT;
14690 if (get_float_arg(argvars, &fx) == OK
14691 && get_float_arg(&argvars[1], &fy) == OK)
14692 rettv->vval.v_float = pow(fx, fy);
14693 else
14694 rettv->vval.v_float = 0.0;
14695}
14696#endif
14697
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 * "prevnonblank()" function
14700 */
14701 static void
14702f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014703 typval_T *argvars;
14704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705{
14706 linenr_T lnum;
14707
14708 lnum = get_tv_lnum(argvars);
14709 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14710 lnum = 0;
14711 else
14712 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14713 --lnum;
14714 rettv->vval.v_number = lnum;
14715}
14716
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014717#ifdef HAVE_STDARG_H
14718/* This dummy va_list is here because:
14719 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14720 * - locally in the function results in a "used before set" warning
14721 * - using va_start() to initialize it gives "function with fixed args" error */
14722static va_list ap;
14723#endif
14724
Bram Moolenaar8c711452005-01-14 21:53:12 +000014725/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014726 * "printf()" function
14727 */
14728 static void
14729f_printf(argvars, rettv)
14730 typval_T *argvars;
14731 typval_T *rettv;
14732{
14733 rettv->v_type = VAR_STRING;
14734 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014735#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014736 {
14737 char_u buf[NUMBUFLEN];
14738 int len;
14739 char_u *s;
14740 int saved_did_emsg = did_emsg;
14741 char *fmt;
14742
14743 /* Get the required length, allocate the buffer and do it for real. */
14744 did_emsg = FALSE;
14745 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014746 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014747 if (!did_emsg)
14748 {
14749 s = alloc(len + 1);
14750 if (s != NULL)
14751 {
14752 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014753 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014754 }
14755 }
14756 did_emsg |= saved_did_emsg;
14757 }
14758#endif
14759}
14760
14761/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014762 * "pumvisible()" function
14763 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014764 static void
14765f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014766 typval_T *argvars UNUSED;
14767 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014768{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014769#ifdef FEAT_INS_EXPAND
14770 if (pum_visible())
14771 rettv->vval.v_number = 1;
14772#endif
14773}
14774
Bram Moolenaardb913952012-06-29 12:54:53 +020014775#ifdef FEAT_PYTHON3
14776/*
14777 * "py3eval()" function
14778 */
14779 static void
14780f_py3eval(argvars, rettv)
14781 typval_T *argvars;
14782 typval_T *rettv;
14783{
14784 char_u *str;
14785 char_u buf[NUMBUFLEN];
14786
14787 str = get_tv_string_buf(&argvars[0], buf);
14788 do_py3eval(str, rettv);
14789}
14790#endif
14791
14792#ifdef FEAT_PYTHON
14793/*
14794 * "pyeval()" function
14795 */
14796 static void
14797f_pyeval(argvars, rettv)
14798 typval_T *argvars;
14799 typval_T *rettv;
14800{
14801 char_u *str;
14802 char_u buf[NUMBUFLEN];
14803
14804 str = get_tv_string_buf(&argvars[0], buf);
14805 do_pyeval(str, rettv);
14806}
14807#endif
14808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014809/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014810 * "range()" function
14811 */
14812 static void
14813f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 typval_T *argvars;
14815 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014816{
14817 long start;
14818 long end;
14819 long stride = 1;
14820 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014824 if (argvars[1].v_type == VAR_UNKNOWN)
14825 {
14826 end = start - 1;
14827 start = 0;
14828 }
14829 else
14830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014834 }
14835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 if (error)
14837 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014838 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014839 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014840 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014841 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842 else
14843 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014844 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014845 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014846 if (list_append_number(rettv->vval.v_list,
14847 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014848 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014849 }
14850}
14851
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014852/*
14853 * "readfile()" function
14854 */
14855 static void
14856f_readfile(argvars, rettv)
14857 typval_T *argvars;
14858 typval_T *rettv;
14859{
14860 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014861 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 char_u *fname;
14863 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014864 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14865 int io_size = sizeof(buf);
14866 int readlen; /* size of last fread() */
14867 char_u *prev = NULL; /* previously read bytes, if any */
14868 long prevlen = 0; /* length of data in prev */
14869 long prevsize = 0; /* size of prev buffer */
14870 long maxline = MAXLNUM;
14871 long cnt = 0;
14872 char_u *p; /* position in buf */
14873 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014874
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014875 if (argvars[1].v_type != VAR_UNKNOWN)
14876 {
14877 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14878 binary = TRUE;
14879 if (argvars[2].v_type != VAR_UNKNOWN)
14880 maxline = get_tv_number(&argvars[2]);
14881 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014882
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014883 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014884 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014885
14886 /* Always open the file in binary mode, library functions have a mind of
14887 * their own about CR-LF conversion. */
14888 fname = get_tv_string(&argvars[0]);
14889 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14890 {
14891 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14892 return;
14893 }
14894
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014895 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014896 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014897 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014899 /* This for loop processes what was read, but is also entered at end
14900 * of file so that either:
14901 * - an incomplete line gets written
14902 * - a "binary" file gets an empty line at the end if it ends in a
14903 * newline. */
14904 for (p = buf, start = buf;
14905 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14906 ++p)
14907 {
14908 if (*p == '\n' || readlen <= 0)
14909 {
14910 listitem_T *li;
14911 char_u *s = NULL;
14912 long_u len = p - start;
14913
14914 /* Finished a line. Remove CRs before NL. */
14915 if (readlen > 0 && !binary)
14916 {
14917 while (len > 0 && start[len - 1] == '\r')
14918 --len;
14919 /* removal may cross back to the "prev" string */
14920 if (len == 0)
14921 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14922 --prevlen;
14923 }
14924 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014925 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014926 else
14927 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014928 /* Change "prev" buffer to be the right size. This way
14929 * the bytes are only copied once, and very long lines are
14930 * allocated only once. */
14931 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014932 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014933 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014934 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014935 prev = NULL; /* the list will own the string */
14936 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014937 }
14938 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014939 if (s == NULL)
14940 {
14941 do_outofmem_msg((long_u) prevlen + len + 1);
14942 failed = TRUE;
14943 break;
14944 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014946 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014947 {
14948 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014950 break;
14951 }
14952 li->li_tv.v_type = VAR_STRING;
14953 li->li_tv.v_lock = 0;
14954 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014955 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014956
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014957 start = p + 1; /* step over newline */
14958 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014959 break;
14960 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014961 else if (*p == NUL)
14962 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014963#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014964 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14965 * when finding the BF and check the previous two bytes. */
14966 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014967 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014968 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14969 * + 1, these may be in the "prev" string. */
14970 char_u back1 = p >= buf + 1 ? p[-1]
14971 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14972 char_u back2 = p >= buf + 2 ? p[-2]
14973 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14974 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014977 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014978 char_u *dest = p - 2;
14979
14980 /* Usually a BOM is at the beginning of a file, and so at
14981 * the beginning of a line; then we can just step over it.
14982 */
14983 if (start == dest)
14984 start = p + 1;
14985 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014986 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014987 /* have to shuffle buf to close gap */
14988 int adjust_prevlen = 0;
14989
14990 if (dest < buf)
14991 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014992 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014993 dest = buf;
14994 }
14995 if (readlen > p - buf + 1)
14996 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14997 readlen -= 3 - adjust_prevlen;
14998 prevlen -= adjust_prevlen;
14999 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015000 }
15001 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015002 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015003#endif
15004 } /* for */
15005
15006 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15007 break;
15008 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015010 /* There's part of a line in buf, store it in "prev". */
15011 if (p - start + prevlen >= prevsize)
15012 {
15013 /* need bigger "prev" buffer */
15014 char_u *newprev;
15015
15016 /* A common use case is ordinary text files and "prev" gets a
15017 * fragment of a line, so the first allocation is made
15018 * small, to avoid repeatedly 'allocing' large and
15019 * 'reallocing' small. */
15020 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015021 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015022 else
15023 {
15024 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015025 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015026 prevsize = grow50pc > growmin ? grow50pc : growmin;
15027 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015028 newprev = prev == NULL ? alloc(prevsize)
15029 : vim_realloc(prev, prevsize);
15030 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015031 {
15032 do_outofmem_msg((long_u)prevsize);
15033 failed = TRUE;
15034 break;
15035 }
15036 prev = newprev;
15037 }
15038 /* Add the line part to end of "prev". */
15039 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015040 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015041 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015042 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015043
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015044 /*
15045 * For a negative line count use only the lines at the end of the file,
15046 * free the rest.
15047 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015048 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015049 while (cnt > -maxline)
15050 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015051 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015052 --cnt;
15053 }
15054
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015055 if (failed)
15056 {
15057 list_free(rettv->vval.v_list, TRUE);
15058 /* readfile doc says an empty list is returned on error */
15059 rettv->vval.v_list = list_alloc();
15060 }
15061
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015062 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015063 fclose(fd);
15064}
15065
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015066#if defined(FEAT_RELTIME)
15067static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15068
15069/*
15070 * Convert a List to proftime_T.
15071 * Return FAIL when there is something wrong.
15072 */
15073 static int
15074list2proftime(arg, tm)
15075 typval_T *arg;
15076 proftime_T *tm;
15077{
15078 long n1, n2;
15079 int error = FALSE;
15080
15081 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15082 || arg->vval.v_list->lv_len != 2)
15083 return FAIL;
15084 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15085 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15086# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015087 tm->HighPart = n1;
15088 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015089# else
15090 tm->tv_sec = n1;
15091 tm->tv_usec = n2;
15092# endif
15093 return error ? FAIL : OK;
15094}
15095#endif /* FEAT_RELTIME */
15096
15097/*
15098 * "reltime()" function
15099 */
15100 static void
15101f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015102 typval_T *argvars UNUSED;
15103 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015104{
15105#ifdef FEAT_RELTIME
15106 proftime_T res;
15107 proftime_T start;
15108
15109 if (argvars[0].v_type == VAR_UNKNOWN)
15110 {
15111 /* No arguments: get current time. */
15112 profile_start(&res);
15113 }
15114 else if (argvars[1].v_type == VAR_UNKNOWN)
15115 {
15116 if (list2proftime(&argvars[0], &res) == FAIL)
15117 return;
15118 profile_end(&res);
15119 }
15120 else
15121 {
15122 /* Two arguments: compute the difference. */
15123 if (list2proftime(&argvars[0], &start) == FAIL
15124 || list2proftime(&argvars[1], &res) == FAIL)
15125 return;
15126 profile_sub(&res, &start);
15127 }
15128
15129 if (rettv_list_alloc(rettv) == OK)
15130 {
15131 long n1, n2;
15132
15133# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015134 n1 = res.HighPart;
15135 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015136# else
15137 n1 = res.tv_sec;
15138 n2 = res.tv_usec;
15139# endif
15140 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15141 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15142 }
15143#endif
15144}
15145
15146/*
15147 * "reltimestr()" function
15148 */
15149 static void
15150f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015151 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015152 typval_T *rettv;
15153{
15154#ifdef FEAT_RELTIME
15155 proftime_T tm;
15156#endif
15157
15158 rettv->v_type = VAR_STRING;
15159 rettv->vval.v_string = NULL;
15160#ifdef FEAT_RELTIME
15161 if (list2proftime(&argvars[0], &tm) == OK)
15162 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15163#endif
15164}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015165
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15167static void make_connection __ARGS((void));
15168static int check_connection __ARGS((void));
15169
15170 static void
15171make_connection()
15172{
15173 if (X_DISPLAY == NULL
15174# ifdef FEAT_GUI
15175 && !gui.in_use
15176# endif
15177 )
15178 {
15179 x_force_connect = TRUE;
15180 setup_term_clip();
15181 x_force_connect = FALSE;
15182 }
15183}
15184
15185 static int
15186check_connection()
15187{
15188 make_connection();
15189 if (X_DISPLAY == NULL)
15190 {
15191 EMSG(_("E240: No connection to Vim server"));
15192 return FAIL;
15193 }
15194 return OK;
15195}
15196#endif
15197
15198#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015199static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200
15201 static void
15202remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 typval_T *argvars;
15204 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015205 int expr;
15206{
15207 char_u *server_name;
15208 char_u *keys;
15209 char_u *r = NULL;
15210 char_u buf[NUMBUFLEN];
15211# ifdef WIN32
15212 HWND w;
15213# else
15214 Window w;
15215# endif
15216
15217 if (check_restricted() || check_secure())
15218 return;
15219
15220# ifdef FEAT_X11
15221 if (check_connection() == FAIL)
15222 return;
15223# endif
15224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 server_name = get_tv_string_chk(&argvars[0]);
15226 if (server_name == NULL)
15227 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228 keys = get_tv_string_buf(&argvars[1], buf);
15229# ifdef WIN32
15230 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15231# else
15232 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15233 < 0)
15234# endif
15235 {
15236 if (r != NULL)
15237 EMSG(r); /* sending worked but evaluation failed */
15238 else
15239 EMSG2(_("E241: Unable to send to %s"), server_name);
15240 return;
15241 }
15242
15243 rettv->vval.v_string = r;
15244
15245 if (argvars[2].v_type != VAR_UNKNOWN)
15246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015247 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015248 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015251 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 v.di_tv.v_type = VAR_STRING;
15253 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 idvar = get_tv_string_chk(&argvars[2]);
15255 if (idvar != NULL)
15256 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258 }
15259}
15260#endif
15261
15262/*
15263 * "remote_expr()" function
15264 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015265 static void
15266f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015268 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015269{
15270 rettv->v_type = VAR_STRING;
15271 rettv->vval.v_string = NULL;
15272#ifdef FEAT_CLIENTSERVER
15273 remote_common(argvars, rettv, TRUE);
15274#endif
15275}
15276
15277/*
15278 * "remote_foreground()" function
15279 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 static void
15281f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015282 typval_T *argvars UNUSED;
15283 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285#ifdef FEAT_CLIENTSERVER
15286# ifdef WIN32
15287 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015288 {
15289 char_u *server_name = get_tv_string_chk(&argvars[0]);
15290
15291 if (server_name != NULL)
15292 serverForeground(server_name);
15293 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294# else
15295 /* Send a foreground() expression to the server. */
15296 argvars[1].v_type = VAR_STRING;
15297 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15298 argvars[2].v_type = VAR_UNKNOWN;
15299 remote_common(argvars, rettv, TRUE);
15300 vim_free(argvars[1].vval.v_string);
15301# endif
15302#endif
15303}
15304
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305 static void
15306f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309{
15310#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015311 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015312 char_u *s = NULL;
15313# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015314 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317
15318 if (check_restricted() || check_secure())
15319 {
15320 rettv->vval.v_number = -1;
15321 return;
15322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 serverid = get_tv_string_chk(&argvars[0]);
15324 if (serverid == NULL)
15325 {
15326 rettv->vval.v_number = -1;
15327 return; /* type error; errmsg already given */
15328 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015330 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331 if (n == 0)
15332 rettv->vval.v_number = -1;
15333 else
15334 {
15335 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15336 rettv->vval.v_number = (s != NULL);
15337 }
15338# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 if (check_connection() == FAIL)
15340 return;
15341
15342 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015343 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344# endif
15345
15346 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 char_u *retvar;
15349
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 v.di_tv.v_type = VAR_STRING;
15351 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015352 retvar = get_tv_string_chk(&argvars[1]);
15353 if (retvar != NULL)
15354 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015355 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015356 }
15357#else
15358 rettv->vval.v_number = -1;
15359#endif
15360}
15361
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 static void
15363f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366{
15367 char_u *r = NULL;
15368
15369#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015370 char_u *serverid = get_tv_string_chk(&argvars[0]);
15371
15372 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373 {
15374# ifdef WIN32
15375 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015376 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015377
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015378 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379 if (n != 0)
15380 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15381 if (r == NULL)
15382# else
15383 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015384 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015385# endif
15386 EMSG(_("E277: Unable to read a server reply"));
15387 }
15388#endif
15389 rettv->v_type = VAR_STRING;
15390 rettv->vval.v_string = r;
15391}
15392
15393/*
15394 * "remote_send()" function
15395 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396 static void
15397f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400{
15401 rettv->v_type = VAR_STRING;
15402 rettv->vval.v_string = NULL;
15403#ifdef FEAT_CLIENTSERVER
15404 remote_common(argvars, rettv, FALSE);
15405#endif
15406}
15407
15408/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015409 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015410 */
15411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015413 typval_T *argvars;
15414 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015415{
Bram Moolenaar33570922005-01-25 22:26:29 +000015416 list_T *l;
15417 listitem_T *item, *item2;
15418 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015419 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015420 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015421 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015422 dict_T *d;
15423 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015424 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015425
Bram Moolenaar8c711452005-01-14 21:53:12 +000015426 if (argvars[0].v_type == VAR_DICT)
15427 {
15428 if (argvars[2].v_type != VAR_UNKNOWN)
15429 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015430 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015431 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015433 key = get_tv_string_chk(&argvars[1]);
15434 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015436 di = dict_find(d, key, -1);
15437 if (di == NULL)
15438 EMSG2(_(e_dictkey), key);
15439 else
15440 {
15441 *rettv = di->di_tv;
15442 init_tv(&di->di_tv);
15443 dictitem_remove(d, di);
15444 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015445 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015446 }
15447 }
15448 else if (argvars[0].v_type != VAR_LIST)
15449 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015450 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015451 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015453 int error = FALSE;
15454
15455 idx = get_tv_number_chk(&argvars[1], &error);
15456 if (error)
15457 ; /* type error: do nothing, errmsg already given */
15458 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 EMSGN(_(e_listidx), idx);
15460 else
15461 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015462 if (argvars[2].v_type == VAR_UNKNOWN)
15463 {
15464 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015465 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015466 *rettv = item->li_tv;
15467 vim_free(item);
15468 }
15469 else
15470 {
15471 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015472 end = get_tv_number_chk(&argvars[2], &error);
15473 if (error)
15474 ; /* type error: do nothing */
15475 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015476 EMSGN(_(e_listidx), end);
15477 else
15478 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015479 int cnt = 0;
15480
15481 for (li = item; li != NULL; li = li->li_next)
15482 {
15483 ++cnt;
15484 if (li == item2)
15485 break;
15486 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015487 if (li == NULL) /* didn't find "item2" after "item" */
15488 EMSG(_(e_invrange));
15489 else
15490 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015491 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015492 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015494 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015495 l->lv_first = item;
15496 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015497 item->li_prev = NULL;
15498 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015499 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015500 }
15501 }
15502 }
15503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015504 }
15505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506}
15507
15508/*
15509 * "rename({from}, {to})" function
15510 */
15511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015512f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015513 typval_T *argvars;
15514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
15516 char_u buf[NUMBUFLEN];
15517
15518 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15522 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523}
15524
15525/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015526 * "repeat()" function
15527 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015528 static void
15529f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *argvars;
15531 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015532{
15533 char_u *p;
15534 int n;
15535 int slen;
15536 int len;
15537 char_u *r;
15538 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015539
15540 n = get_tv_number(&argvars[1]);
15541 if (argvars[0].v_type == VAR_LIST)
15542 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015543 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015544 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015545 if (list_extend(rettv->vval.v_list,
15546 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015547 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015548 }
15549 else
15550 {
15551 p = get_tv_string(&argvars[0]);
15552 rettv->v_type = VAR_STRING;
15553 rettv->vval.v_string = NULL;
15554
15555 slen = (int)STRLEN(p);
15556 len = slen * n;
15557 if (len <= 0)
15558 return;
15559
15560 r = alloc(len + 1);
15561 if (r != NULL)
15562 {
15563 for (i = 0; i < n; i++)
15564 mch_memmove(r + i * slen, p, (size_t)slen);
15565 r[len] = NUL;
15566 }
15567
15568 rettv->vval.v_string = r;
15569 }
15570}
15571
15572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 * "resolve()" function
15574 */
15575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 typval_T *argvars;
15578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579{
15580 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015581#ifdef HAVE_READLINK
15582 char_u *buf = NULL;
15583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586#ifdef FEAT_SHORTCUT
15587 {
15588 char_u *v = NULL;
15589
15590 v = mch_resolve_shortcut(p);
15591 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015594 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 }
15596#else
15597# ifdef HAVE_READLINK
15598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 char_u *cpy;
15600 int len;
15601 char_u *remain = NULL;
15602 char_u *q;
15603 int is_relative_to_current = FALSE;
15604 int has_trailing_pathsep = FALSE;
15605 int limit = 100;
15606
15607 p = vim_strsave(p);
15608
15609 if (p[0] == '.' && (vim_ispathsep(p[1])
15610 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15611 is_relative_to_current = TRUE;
15612
15613 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015614 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015617 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619
15620 q = getnextcomp(p);
15621 if (*q != NUL)
15622 {
15623 /* Separate the first path component in "p", and keep the
15624 * remainder (beginning with the path separator). */
15625 remain = vim_strsave(q - 1);
15626 q[-1] = NUL;
15627 }
15628
Bram Moolenaard9462e32011-04-11 21:35:11 +020015629 buf = alloc(MAXPATHL + 1);
15630 if (buf == NULL)
15631 goto fail;
15632
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 for (;;)
15634 {
15635 for (;;)
15636 {
15637 len = readlink((char *)p, (char *)buf, MAXPATHL);
15638 if (len <= 0)
15639 break;
15640 buf[len] = NUL;
15641
15642 if (limit-- == 0)
15643 {
15644 vim_free(p);
15645 vim_free(remain);
15646 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 goto fail;
15649 }
15650
15651 /* Ensure that the result will have a trailing path separator
15652 * if the argument has one. */
15653 if (remain == NULL && has_trailing_pathsep)
15654 add_pathsep(buf);
15655
15656 /* Separate the first path component in the link value and
15657 * concatenate the remainders. */
15658 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15659 if (*q != NUL)
15660 {
15661 if (remain == NULL)
15662 remain = vim_strsave(q - 1);
15663 else
15664 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015665 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 if (cpy != NULL)
15667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 vim_free(remain);
15669 remain = cpy;
15670 }
15671 }
15672 q[-1] = NUL;
15673 }
15674
15675 q = gettail(p);
15676 if (q > p && *q == NUL)
15677 {
15678 /* Ignore trailing path separator. */
15679 q[-1] = NUL;
15680 q = gettail(p);
15681 }
15682 if (q > p && !mch_isFullName(buf))
15683 {
15684 /* symlink is relative to directory of argument */
15685 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15686 if (cpy != NULL)
15687 {
15688 STRCPY(cpy, p);
15689 STRCPY(gettail(cpy), buf);
15690 vim_free(p);
15691 p = cpy;
15692 }
15693 }
15694 else
15695 {
15696 vim_free(p);
15697 p = vim_strsave(buf);
15698 }
15699 }
15700
15701 if (remain == NULL)
15702 break;
15703
15704 /* Append the first path component of "remain" to "p". */
15705 q = getnextcomp(remain + 1);
15706 len = q - remain - (*q != NUL);
15707 cpy = vim_strnsave(p, STRLEN(p) + len);
15708 if (cpy != NULL)
15709 {
15710 STRNCAT(cpy, remain, len);
15711 vim_free(p);
15712 p = cpy;
15713 }
15714 /* Shorten "remain". */
15715 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015716 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 else
15718 {
15719 vim_free(remain);
15720 remain = NULL;
15721 }
15722 }
15723
15724 /* If the result is a relative path name, make it explicitly relative to
15725 * the current directory if and only if the argument had this form. */
15726 if (!vim_ispathsep(*p))
15727 {
15728 if (is_relative_to_current
15729 && *p != NUL
15730 && !(p[0] == '.'
15731 && (p[1] == NUL
15732 || vim_ispathsep(p[1])
15733 || (p[1] == '.'
15734 && (p[2] == NUL
15735 || vim_ispathsep(p[2]))))))
15736 {
15737 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015738 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 if (cpy != NULL)
15740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 vim_free(p);
15742 p = cpy;
15743 }
15744 }
15745 else if (!is_relative_to_current)
15746 {
15747 /* Strip leading "./". */
15748 q = p;
15749 while (q[0] == '.' && vim_ispathsep(q[1]))
15750 q += 2;
15751 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015752 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 }
15754 }
15755
15756 /* Ensure that the result will have no trailing path separator
15757 * if the argument had none. But keep "/" or "//". */
15758 if (!has_trailing_pathsep)
15759 {
15760 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015761 if (after_pathsep(p, q))
15762 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 }
15764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 }
15767# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769# endif
15770#endif
15771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773
15774#ifdef HAVE_READLINK
15775fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015776 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779}
15780
15781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 */
15784 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015786 typval_T *argvars;
15787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788{
Bram Moolenaar33570922005-01-25 22:26:29 +000015789 list_T *l;
15790 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791
Bram Moolenaar0d660222005-01-07 21:51:51 +000015792 if (argvars[0].v_type != VAR_LIST)
15793 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015794 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015795 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 {
15797 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015798 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015799 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800 while (li != NULL)
15801 {
15802 ni = li->li_prev;
15803 list_append(l, li);
15804 li = ni;
15805 }
15806 rettv->vval.v_list = l;
15807 rettv->v_type = VAR_LIST;
15808 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015809 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811}
15812
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015813#define SP_NOMOVE 0x01 /* don't move cursor */
15814#define SP_REPEAT 0x02 /* repeat to find outer pair */
15815#define SP_RETCOUNT 0x04 /* return matchcount */
15816#define SP_SETPCMARK 0x08 /* set previous context mark */
15817#define SP_START 0x10 /* accept match at start position */
15818#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15819#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015820
Bram Moolenaar33570922005-01-25 22:26:29 +000015821static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822
15823/*
15824 * Get flags for a search function.
15825 * Possibly sets "p_ws".
15826 * Returns BACKWARD, FORWARD or zero (for an error).
15827 */
15828 static int
15829get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831 int *flagsp;
15832{
15833 int dir = FORWARD;
15834 char_u *flags;
15835 char_u nbuf[NUMBUFLEN];
15836 int mask;
15837
15838 if (varp->v_type != VAR_UNKNOWN)
15839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015840 flags = get_tv_string_buf_chk(varp, nbuf);
15841 if (flags == NULL)
15842 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843 while (*flags != NUL)
15844 {
15845 switch (*flags)
15846 {
15847 case 'b': dir = BACKWARD; break;
15848 case 'w': p_ws = TRUE; break;
15849 case 'W': p_ws = FALSE; break;
15850 default: mask = 0;
15851 if (flagsp != NULL)
15852 switch (*flags)
15853 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015854 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015855 case 'e': mask = SP_END; break;
15856 case 'm': mask = SP_RETCOUNT; break;
15857 case 'n': mask = SP_NOMOVE; break;
15858 case 'p': mask = SP_SUBPAT; break;
15859 case 'r': mask = SP_REPEAT; break;
15860 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 }
15862 if (mask == 0)
15863 {
15864 EMSG2(_(e_invarg2), flags);
15865 dir = 0;
15866 }
15867 else
15868 *flagsp |= mask;
15869 }
15870 if (dir == 0)
15871 break;
15872 ++flags;
15873 }
15874 }
15875 return dir;
15876}
15877
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015879 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015881 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015882search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015884 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015885 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015887 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 char_u *pat;
15889 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015890 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 int save_p_ws = p_ws;
15892 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015893 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015894 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015895 proftime_T tm;
15896#ifdef FEAT_RELTIME
15897 long time_limit = 0;
15898#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015899 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015900 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015903 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015904 if (dir == 0)
15905 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015906 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015907 if (flags & SP_START)
15908 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015909 if (flags & SP_END)
15910 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015911
Bram Moolenaar76929292008-01-06 19:07:36 +000015912 /* Optional arguments: line number to stop searching and timeout. */
15913 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015914 {
15915 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15916 if (lnum_stop < 0)
15917 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015918#ifdef FEAT_RELTIME
15919 if (argvars[3].v_type != VAR_UNKNOWN)
15920 {
15921 time_limit = get_tv_number_chk(&argvars[3], NULL);
15922 if (time_limit < 0)
15923 goto theend;
15924 }
15925#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 }
15927
Bram Moolenaar76929292008-01-06 19:07:36 +000015928#ifdef FEAT_RELTIME
15929 /* Set the time limit, if there is one. */
15930 profile_setlimit(time_limit, &tm);
15931#endif
15932
Bram Moolenaar231334e2005-07-25 20:46:57 +000015933 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015934 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015935 * Check to make sure only those flags are set.
15936 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15937 * flags cannot be set. Check for that condition also.
15938 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015939 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015940 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015943 goto theend;
15944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015946 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015947 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015948 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015949 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015951 if (flags & SP_SUBPAT)
15952 retval = subpatnum;
15953 else
15954 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015955 if (flags & SP_SETPCMARK)
15956 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015958 if (match_pos != NULL)
15959 {
15960 /* Store the match cursor position */
15961 match_pos->lnum = pos.lnum;
15962 match_pos->col = pos.col + 1;
15963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 /* "/$" will put the cursor after the end of the line, may need to
15965 * correct that here */
15966 check_cursor();
15967 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015968
15969 /* If 'n' flag is used: restore cursor position. */
15970 if (flags & SP_NOMOVE)
15971 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015972 else
15973 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015974theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015976
15977 return retval;
15978}
15979
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015980#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015981
15982/*
15983 * round() is not in C90, use ceil() or floor() instead.
15984 */
15985 float_T
15986vim_round(f)
15987 float_T f;
15988{
15989 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15990}
15991
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015992/*
15993 * "round({float})" function
15994 */
15995 static void
15996f_round(argvars, rettv)
15997 typval_T *argvars;
15998 typval_T *rettv;
15999{
16000 float_T f;
16001
16002 rettv->v_type = VAR_FLOAT;
16003 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016004 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016005 else
16006 rettv->vval.v_float = 0.0;
16007}
16008#endif
16009
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016010/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016011 * "screenattr()" function
16012 */
16013 static void
16014f_screenattr(argvars, rettv)
16015 typval_T *argvars UNUSED;
16016 typval_T *rettv;
16017{
16018 int row;
16019 int col;
16020 int c;
16021
16022 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16023 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16024 if (row < 0 || row >= screen_Rows
16025 || col < 0 || col >= screen_Columns)
16026 c = -1;
16027 else
16028 c = ScreenAttrs[LineOffset[row] + col];
16029 rettv->vval.v_number = c;
16030}
16031
16032/*
16033 * "screenchar()" function
16034 */
16035 static void
16036f_screenchar(argvars, rettv)
16037 typval_T *argvars UNUSED;
16038 typval_T *rettv;
16039{
16040 int row;
16041 int col;
16042 int off;
16043 int c;
16044
16045 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16046 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16047 if (row < 0 || row >= screen_Rows
16048 || col < 0 || col >= screen_Columns)
16049 c = -1;
16050 else
16051 {
16052 off = LineOffset[row] + col;
16053#ifdef FEAT_MBYTE
16054 if (enc_utf8 && ScreenLinesUC[off] != 0)
16055 c = ScreenLinesUC[off];
16056 else
16057#endif
16058 c = ScreenLines[off];
16059 }
16060 rettv->vval.v_number = c;
16061}
16062
16063/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016064 * "screencol()" function
16065 *
16066 * First column is 1 to be consistent with virtcol().
16067 */
16068 static void
16069f_screencol(argvars, rettv)
16070 typval_T *argvars UNUSED;
16071 typval_T *rettv;
16072{
16073 rettv->vval.v_number = screen_screencol() + 1;
16074}
16075
16076/*
16077 * "screenrow()" function
16078 */
16079 static void
16080f_screenrow(argvars, rettv)
16081 typval_T *argvars UNUSED;
16082 typval_T *rettv;
16083{
16084 rettv->vval.v_number = screen_screenrow() + 1;
16085}
16086
16087/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016088 * "search()" function
16089 */
16090 static void
16091f_search(argvars, rettv)
16092 typval_T *argvars;
16093 typval_T *rettv;
16094{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016095 int flags = 0;
16096
16097 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098}
16099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016101 * "searchdecl()" function
16102 */
16103 static void
16104f_searchdecl(argvars, rettv)
16105 typval_T *argvars;
16106 typval_T *rettv;
16107{
16108 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016109 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016110 int error = FALSE;
16111 char_u *name;
16112
16113 rettv->vval.v_number = 1; /* default: FAIL */
16114
16115 name = get_tv_string_chk(&argvars[0]);
16116 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016117 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016118 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016119 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16120 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16121 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016122 if (!error && name != NULL)
16123 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016124 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016125}
16126
16127/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016128 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016130 static int
16131searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
16135 char_u *spat, *mpat, *epat;
16136 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 int dir;
16139 int flags = 0;
16140 char_u nbuf1[NUMBUFLEN];
16141 char_u nbuf2[NUMBUFLEN];
16142 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016143 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016144 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016145 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016148 spat = get_tv_string_chk(&argvars[0]);
16149 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16150 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16151 if (spat == NULL || mpat == NULL || epat == NULL)
16152 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 /* Handle the optional fourth argument: flags */
16155 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016156 if (dir == 0)
16157 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016158
16159 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016160 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16161 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016162 if ((flags & (SP_END | SP_SUBPAT)) != 0
16163 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016164 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016165 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016166 goto theend;
16167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016169 /* Using 'r' implies 'W', otherwise it doesn't work. */
16170 if (flags & SP_REPEAT)
16171 p_ws = FALSE;
16172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016173 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016174 if (argvars[3].v_type == VAR_UNKNOWN
16175 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 skip = (char_u *)"";
16177 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016180 if (argvars[5].v_type != VAR_UNKNOWN)
16181 {
16182 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16183 if (lnum_stop < 0)
16184 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016185#ifdef FEAT_RELTIME
16186 if (argvars[6].v_type != VAR_UNKNOWN)
16187 {
16188 time_limit = get_tv_number_chk(&argvars[6], NULL);
16189 if (time_limit < 0)
16190 goto theend;
16191 }
16192#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016193 }
16194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016195 if (skip == NULL)
16196 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016198 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016199 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016200
16201theend:
16202 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016203
16204 return retval;
16205}
16206
16207/*
16208 * "searchpair()" function
16209 */
16210 static void
16211f_searchpair(argvars, rettv)
16212 typval_T *argvars;
16213 typval_T *rettv;
16214{
16215 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16216}
16217
16218/*
16219 * "searchpairpos()" function
16220 */
16221 static void
16222f_searchpairpos(argvars, rettv)
16223 typval_T *argvars;
16224 typval_T *rettv;
16225{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016226 pos_T match_pos;
16227 int lnum = 0;
16228 int col = 0;
16229
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016230 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016231 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016232
16233 if (searchpair_cmn(argvars, &match_pos) > 0)
16234 {
16235 lnum = match_pos.lnum;
16236 col = match_pos.col;
16237 }
16238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016239 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16240 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016241}
16242
16243/*
16244 * Search for a start/middle/end thing.
16245 * Used by searchpair(), see its documentation for the details.
16246 * Returns 0 or -1 for no match,
16247 */
16248 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016249do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16250 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016251 char_u *spat; /* start pattern */
16252 char_u *mpat; /* middle pattern */
16253 char_u *epat; /* end pattern */
16254 int dir; /* BACKWARD or FORWARD */
16255 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016256 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016257 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016258 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016259 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016260{
16261 char_u *save_cpo;
16262 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16263 long retval = 0;
16264 pos_T pos;
16265 pos_T firstpos;
16266 pos_T foundpos;
16267 pos_T save_cursor;
16268 pos_T save_pos;
16269 int n;
16270 int r;
16271 int nest = 1;
16272 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016273 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016274 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016275
16276 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16277 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016278 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016279
Bram Moolenaar76929292008-01-06 19:07:36 +000016280#ifdef FEAT_RELTIME
16281 /* Set the time limit, if there is one. */
16282 profile_setlimit(time_limit, &tm);
16283#endif
16284
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016285 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16286 * start/middle/end (pat3, for the top pair). */
16287 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16288 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16289 if (pat2 == NULL || pat3 == NULL)
16290 goto theend;
16291 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16292 if (*mpat == NUL)
16293 STRCPY(pat3, pat2);
16294 else
16295 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16296 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016297 if (flags & SP_START)
16298 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016299
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 save_cursor = curwin->w_cursor;
16301 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016302 clearpos(&firstpos);
16303 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 pat = pat3;
16305 for (;;)
16306 {
16307 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016308 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16310 /* didn't find it or found the first match again: FAIL */
16311 break;
16312
16313 if (firstpos.lnum == 0)
16314 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016315 if (equalpos(pos, foundpos))
16316 {
16317 /* Found the same position again. Can happen with a pattern that
16318 * has "\zs" at the end and searching backwards. Advance one
16319 * character and try again. */
16320 if (dir == BACKWARD)
16321 decl(&pos);
16322 else
16323 incl(&pos);
16324 }
16325 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016327 /* clear the start flag to avoid getting stuck here */
16328 options &= ~SEARCH_START;
16329
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 /* If the skip pattern matches, ignore this match. */
16331 if (*skip != NUL)
16332 {
16333 save_pos = curwin->w_cursor;
16334 curwin->w_cursor = pos;
16335 r = eval_to_bool(skip, &err, NULL, FALSE);
16336 curwin->w_cursor = save_pos;
16337 if (err)
16338 {
16339 /* Evaluating {skip} caused an error, break here. */
16340 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016341 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 break;
16343 }
16344 if (r)
16345 continue;
16346 }
16347
16348 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16349 {
16350 /* Found end when searching backwards or start when searching
16351 * forward: nested pair. */
16352 ++nest;
16353 pat = pat2; /* nested, don't search for middle */
16354 }
16355 else
16356 {
16357 /* Found end when searching forward or start when searching
16358 * backward: end of (nested) pair; or found middle in outer pair. */
16359 if (--nest == 1)
16360 pat = pat3; /* outer level, search for middle */
16361 }
16362
16363 if (nest == 0)
16364 {
16365 /* Found the match: return matchcount or line number. */
16366 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016367 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016369 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016370 if (flags & SP_SETPCMARK)
16371 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 curwin->w_cursor = pos;
16373 if (!(flags & SP_REPEAT))
16374 break;
16375 nest = 1; /* search for next unmatched */
16376 }
16377 }
16378
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016379 if (match_pos != NULL)
16380 {
16381 /* Store the match cursor position */
16382 match_pos->lnum = curwin->w_cursor.lnum;
16383 match_pos->col = curwin->w_cursor.col + 1;
16384 }
16385
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016387 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 curwin->w_cursor = save_cursor;
16389
16390theend:
16391 vim_free(pat2);
16392 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016393 if (p_cpo == empty_option)
16394 p_cpo = save_cpo;
16395 else
16396 /* Darn, evaluating the {skip} expression changed the value. */
16397 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016398
16399 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400}
16401
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016402/*
16403 * "searchpos()" function
16404 */
16405 static void
16406f_searchpos(argvars, rettv)
16407 typval_T *argvars;
16408 typval_T *rettv;
16409{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410 pos_T match_pos;
16411 int lnum = 0;
16412 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016413 int n;
16414 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016415
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016416 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016417 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016418
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016419 n = search_cmn(argvars, &match_pos, &flags);
16420 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016421 {
16422 lnum = match_pos.lnum;
16423 col = match_pos.col;
16424 }
16425
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016426 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16427 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016428 if (flags & SP_SUBPAT)
16429 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016430}
16431
16432
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 static void
16434f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438#ifdef FEAT_CLIENTSERVER
16439 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016440 char_u *server = get_tv_string_chk(&argvars[0]);
16441 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 if (server == NULL || reply == NULL)
16445 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446 if (check_restricted() || check_secure())
16447 return;
16448# ifdef FEAT_X11
16449 if (check_connection() == FAIL)
16450 return;
16451# endif
16452
16453 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 EMSG(_("E258: Unable to send to client"));
16456 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 rettv->vval.v_number = 0;
16459#else
16460 rettv->vval.v_number = -1;
16461#endif
16462}
16463
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 static void
16465f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016466 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016467 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468{
16469 char_u *r = NULL;
16470
16471#ifdef FEAT_CLIENTSERVER
16472# ifdef WIN32
16473 r = serverGetVimNames();
16474# else
16475 make_connection();
16476 if (X_DISPLAY != NULL)
16477 r = serverGetVimNames(X_DISPLAY);
16478# endif
16479#endif
16480 rettv->v_type = VAR_STRING;
16481 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482}
16483
16484/*
16485 * "setbufvar()" function
16486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016488f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016490 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491{
16492 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 char_u nbuf[NUMBUFLEN];
16497
16498 if (check_restricted() || check_secure())
16499 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016500 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16501 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016502 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 varp = &argvars[2];
16504
16505 if (buf != NULL && varname != NULL && varp != NULL)
16506 {
16507 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509
16510 if (*varname == '&')
16511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016512 long numval;
16513 char_u *strval;
16514 int error = FALSE;
16515
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 numval = get_tv_number_chk(varp, &error);
16518 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 if (!error && strval != NULL)
16520 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 }
16522 else
16523 {
16524 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16525 if (bufvarname != NULL)
16526 {
16527 STRCPY(bufvarname, "b:");
16528 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016529 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 vim_free(bufvarname);
16531 }
16532 }
16533
16534 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537}
16538
16539/*
16540 * "setcmdpos()" function
16541 */
16542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016543f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016544 typval_T *argvars;
16545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 int pos = (int)get_tv_number(&argvars[0]) - 1;
16548
16549 if (pos >= 0)
16550 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551}
16552
16553/*
16554 * "setline()" function
16555 */
16556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016558 typval_T *argvars;
16559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560{
16561 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016562 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016563 list_T *l = NULL;
16564 listitem_T *li = NULL;
16565 long added = 0;
16566 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016568 lnum = get_tv_lnum(&argvars[0]);
16569 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016571 l = argvars[1].vval.v_list;
16572 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016574 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016576
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016577 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016578 for (;;)
16579 {
16580 if (l != NULL)
16581 {
16582 /* list argument, get next string */
16583 if (li == NULL)
16584 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016586 li = li->li_next;
16587 }
16588
16589 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016590 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016591 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016592
16593 /* When coming here from Insert mode, sync undo, so that this can be
16594 * undone separately from what was previously inserted. */
16595 if (u_sync_once == 2)
16596 {
16597 u_sync_once = 1; /* notify that u_sync() was called */
16598 u_sync(TRUE);
16599 }
16600
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016601 if (lnum <= curbuf->b_ml.ml_line_count)
16602 {
16603 /* existing line, replace it */
16604 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16605 {
16606 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016607 if (lnum == curwin->w_cursor.lnum)
16608 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016609 rettv->vval.v_number = 0; /* OK */
16610 }
16611 }
16612 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16613 {
16614 /* lnum is one past the last line, append the line */
16615 ++added;
16616 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16617 rettv->vval.v_number = 0; /* OK */
16618 }
16619
16620 if (l == NULL) /* only one string argument */
16621 break;
16622 ++lnum;
16623 }
16624
16625 if (added > 0)
16626 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627}
16628
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016629static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16630
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016632 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016633 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016634 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016635set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016636 win_T *wp UNUSED;
16637 typval_T *list_arg UNUSED;
16638 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016639 typval_T *rettv;
16640{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016641#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016642 char_u *act;
16643 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016644#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016645
Bram Moolenaar2641f772005-03-25 21:58:17 +000016646 rettv->vval.v_number = -1;
16647
16648#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016649 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016650 EMSG(_(e_listreq));
16651 else
16652 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016653 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016654
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016655 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016656 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016657 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 if (act == NULL)
16659 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016660 if (*act == 'a' || *act == 'r')
16661 action = *act;
16662 }
16663
Bram Moolenaar81484f42012-12-05 15:16:47 +010016664 if (l != NULL && set_errorlist(wp, l, action,
16665 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016666 rettv->vval.v_number = 0;
16667 }
16668#endif
16669}
16670
16671/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016672 * "setloclist()" function
16673 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016674 static void
16675f_setloclist(argvars, rettv)
16676 typval_T *argvars;
16677 typval_T *rettv;
16678{
16679 win_T *win;
16680
16681 rettv->vval.v_number = -1;
16682
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016683 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016684 if (win != NULL)
16685 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16686}
16687
16688/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016689 * "setmatches()" function
16690 */
16691 static void
16692f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016693 typval_T *argvars UNUSED;
16694 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016695{
16696#ifdef FEAT_SEARCH_EXTRA
16697 list_T *l;
16698 listitem_T *li;
16699 dict_T *d;
16700
16701 rettv->vval.v_number = -1;
16702 if (argvars[0].v_type != VAR_LIST)
16703 {
16704 EMSG(_(e_listreq));
16705 return;
16706 }
16707 if ((l = argvars[0].vval.v_list) != NULL)
16708 {
16709
16710 /* To some extent make sure that we are dealing with a list from
16711 * "getmatches()". */
16712 li = l->lv_first;
16713 while (li != NULL)
16714 {
16715 if (li->li_tv.v_type != VAR_DICT
16716 || (d = li->li_tv.vval.v_dict) == NULL)
16717 {
16718 EMSG(_(e_invarg));
16719 return;
16720 }
16721 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16722 && dict_find(d, (char_u *)"pattern", -1) != NULL
16723 && dict_find(d, (char_u *)"priority", -1) != NULL
16724 && dict_find(d, (char_u *)"id", -1) != NULL))
16725 {
16726 EMSG(_(e_invarg));
16727 return;
16728 }
16729 li = li->li_next;
16730 }
16731
16732 clear_matches(curwin);
16733 li = l->lv_first;
16734 while (li != NULL)
16735 {
16736 d = li->li_tv.vval.v_dict;
16737 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16738 get_dict_string(d, (char_u *)"pattern", FALSE),
16739 (int)get_dict_number(d, (char_u *)"priority"),
16740 (int)get_dict_number(d, (char_u *)"id"));
16741 li = li->li_next;
16742 }
16743 rettv->vval.v_number = 0;
16744 }
16745#endif
16746}
16747
16748/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016749 * "setpos()" function
16750 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016751 static void
16752f_setpos(argvars, rettv)
16753 typval_T *argvars;
16754 typval_T *rettv;
16755{
16756 pos_T pos;
16757 int fnum;
16758 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016759 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016760
Bram Moolenaar08250432008-02-13 11:42:46 +000016761 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016762 name = get_tv_string_chk(argvars);
16763 if (name != NULL)
16764 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016765 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016766 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016767 if (--pos.col < 0)
16768 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016769 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016770 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016771 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016772 if (fnum == curbuf->b_fnum)
16773 {
16774 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016775 if (curswant >= 0)
16776 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016777 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016778 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016779 }
16780 else
16781 EMSG(_(e_invarg));
16782 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016783 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16784 {
16785 /* set mark */
16786 if (setmark_pos(name[1], &pos, fnum) == OK)
16787 rettv->vval.v_number = 0;
16788 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016789 else
16790 EMSG(_(e_invarg));
16791 }
16792 }
16793}
16794
16795/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016796 * "setqflist()" function
16797 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016798 static void
16799f_setqflist(argvars, rettv)
16800 typval_T *argvars;
16801 typval_T *rettv;
16802{
16803 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16804}
16805
16806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 * "setreg()" function
16808 */
16809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 typval_T *argvars;
16812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813{
16814 int regname;
16815 char_u *strregname;
16816 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016817 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 int append;
16819 char_u yank_type;
16820 long block_len;
16821
16822 block_len = -1;
16823 yank_type = MAUTO;
16824 append = FALSE;
16825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016826 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016829 if (strregname == NULL)
16830 return; /* type error; errmsg already given */
16831 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 if (regname == 0 || regname == '@')
16833 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837 stropt = get_tv_string_chk(&argvars[2]);
16838 if (stropt == NULL)
16839 return; /* type error */
16840 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 switch (*stropt)
16842 {
16843 case 'a': case 'A': /* append */
16844 append = TRUE;
16845 break;
16846 case 'v': case 'c': /* character-wise selection */
16847 yank_type = MCHAR;
16848 break;
16849 case 'V': case 'l': /* line-wise selection */
16850 yank_type = MLINE;
16851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 case 'b': case Ctrl_V: /* block-wise selection */
16853 yank_type = MBLOCK;
16854 if (VIM_ISDIGIT(stropt[1]))
16855 {
16856 ++stropt;
16857 block_len = getdigits(&stropt) - 1;
16858 --stropt;
16859 }
16860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 }
16862 }
16863
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016864 if (argvars[1].v_type == VAR_LIST)
16865 {
16866 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016867 char_u **allocval;
16868 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016869 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016870 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016871 int len = argvars[1].vval.v_list->lv_len;
16872 listitem_T *li;
16873
Bram Moolenaar7d647822014-04-05 21:28:56 +020016874 /* First half: use for pointers to result lines; second half: use for
16875 * pointers to allocated copies. */
16876 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016877 if (lstval == NULL)
16878 return;
16879 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016880 allocval = lstval + len + 2;
16881 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016882
16883 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16884 li = li->li_next)
16885 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016886 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016887 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016888 goto free_lstval;
16889 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016890 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016891 /* Need to make a copy, next get_tv_string_buf_chk() will
16892 * overwrite the string. */
16893 strval = vim_strsave(buf);
16894 if (strval == NULL)
16895 goto free_lstval;
16896 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016897 }
16898 *curval++ = strval;
16899 }
16900 *curval++ = NULL;
16901
16902 write_reg_contents_lst(regname, lstval, -1,
16903 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016904free_lstval:
16905 while (curallocval > allocval)
16906 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016907 vim_free(lstval);
16908 }
16909 else
16910 {
16911 strval = get_tv_string_chk(&argvars[1]);
16912 if (strval == NULL)
16913 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016914 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016917 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918}
16919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016920/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016921 * "settabvar()" function
16922 */
16923 static void
16924f_settabvar(argvars, rettv)
16925 typval_T *argvars;
16926 typval_T *rettv;
16927{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016928#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016929 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016930 tabpage_T *tp;
16931#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016932 char_u *varname, *tabvarname;
16933 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016934
16935 rettv->vval.v_number = 0;
16936
16937 if (check_restricted() || check_secure())
16938 return;
16939
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016940#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016941 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016942#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016943 varname = get_tv_string_chk(&argvars[1]);
16944 varp = &argvars[2];
16945
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016946 if (varname != NULL && varp != NULL
16947#ifdef FEAT_WINDOWS
16948 && tp != NULL
16949#endif
16950 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016951 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016952#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016953 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016954 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016955#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016956
16957 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16958 if (tabvarname != NULL)
16959 {
16960 STRCPY(tabvarname, "t:");
16961 STRCPY(tabvarname + 2, varname);
16962 set_var(tabvarname, varp, TRUE);
16963 vim_free(tabvarname);
16964 }
16965
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016966#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016967 /* Restore current tabpage */
16968 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016969 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016970#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016971 }
16972}
16973
16974/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016975 * "settabwinvar()" function
16976 */
16977 static void
16978f_settabwinvar(argvars, rettv)
16979 typval_T *argvars;
16980 typval_T *rettv;
16981{
16982 setwinvar(argvars, rettv, 1);
16983}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
16985/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016986 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016990 typval_T *argvars;
16991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016993 setwinvar(argvars, rettv, 0);
16994}
16995
16996/*
16997 * "setwinvar()" and "settabwinvar()" functions
16998 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016999
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017000 static void
17001setwinvar(argvars, rettv, off)
17002 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017003 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017004 int off;
17005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 win_T *win;
17007#ifdef FEAT_WINDOWS
17008 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017009 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010#endif
17011 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017014 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
17016 if (check_restricted() || check_secure())
17017 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017018
17019#ifdef FEAT_WINDOWS
17020 if (off == 1)
17021 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17022 else
17023 tp = curtab;
17024#endif
17025 win = find_win_by_nr(&argvars[off], tp);
17026 varname = get_tv_string_chk(&argvars[off + 1]);
17027 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
17029 if (win != NULL && varname != NULL && varp != NULL)
17030 {
17031#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017032 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017033 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034#endif
17035
17036 if (*varname == '&')
17037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 long numval;
17039 char_u *strval;
17040 int error = FALSE;
17041
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 numval = get_tv_number_chk(varp, &error);
17044 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 if (!error && strval != NULL)
17046 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 }
17048 else
17049 {
17050 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17051 if (winvarname != NULL)
17052 {
17053 STRCPY(winvarname, "w:");
17054 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017055 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 vim_free(winvarname);
17057 }
17058 }
17059
17060#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017061 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062#endif
17063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017066#ifdef FEAT_CRYPT
17067/*
17068 * "sha256({string})" function
17069 */
17070 static void
17071f_sha256(argvars, rettv)
17072 typval_T *argvars;
17073 typval_T *rettv;
17074{
17075 char_u *p;
17076
17077 p = get_tv_string(&argvars[0]);
17078 rettv->vval.v_string = vim_strsave(
17079 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17080 rettv->v_type = VAR_STRING;
17081}
17082#endif /* FEAT_CRYPT */
17083
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017085 * "shellescape({string})" function
17086 */
17087 static void
17088f_shellescape(argvars, rettv)
17089 typval_T *argvars;
17090 typval_T *rettv;
17091{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017092 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017093 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017094 rettv->v_type = VAR_STRING;
17095}
17096
17097/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017098 * shiftwidth() function
17099 */
17100 static void
17101f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017102 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017103 typval_T *rettv;
17104{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017105 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017106}
17107
17108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 */
17111 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 typval_T *argvars;
17114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 p = get_tv_string(&argvars[0]);
17119 rettv->vval.v_string = vim_strsave(p);
17120 simplify_filename(rettv->vval.v_string); /* simplify in place */
17121 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122}
17123
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017124#ifdef FEAT_FLOAT
17125/*
17126 * "sin()" function
17127 */
17128 static void
17129f_sin(argvars, rettv)
17130 typval_T *argvars;
17131 typval_T *rettv;
17132{
17133 float_T f;
17134
17135 rettv->v_type = VAR_FLOAT;
17136 if (get_float_arg(argvars, &f) == OK)
17137 rettv->vval.v_float = sin(f);
17138 else
17139 rettv->vval.v_float = 0.0;
17140}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017141
17142/*
17143 * "sinh()" function
17144 */
17145 static void
17146f_sinh(argvars, rettv)
17147 typval_T *argvars;
17148 typval_T *rettv;
17149{
17150 float_T f;
17151
17152 rettv->v_type = VAR_FLOAT;
17153 if (get_float_arg(argvars, &f) == OK)
17154 rettv->vval.v_float = sinh(f);
17155 else
17156 rettv->vval.v_float = 0.0;
17157}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017158#endif
17159
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160static int
17161#ifdef __BORLANDC__
17162 _RTLENTRYF
17163#endif
17164 item_compare __ARGS((const void *s1, const void *s2));
17165static int
17166#ifdef __BORLANDC__
17167 _RTLENTRYF
17168#endif
17169 item_compare2 __ARGS((const void *s1, const void *s2));
17170
17171static int item_compare_ic;
17172static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017173static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017174static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017175static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176#define ITEM_COMPARE_FAIL 999
17177
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017179 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 static int
17182#ifdef __BORLANDC__
17183_RTLENTRYF
17184#endif
17185item_compare(s1, s2)
17186 const void *s1;
17187 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 char_u *p1, *p2;
17190 char_u *tofree1, *tofree2;
17191 int res;
17192 char_u numbuf1[NUMBUFLEN];
17193 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017195 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17196 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017197 if (p1 == NULL)
17198 p1 = (char_u *)"";
17199 if (p2 == NULL)
17200 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 if (item_compare_ic)
17202 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204 res = STRCMP(p1, p2);
17205 vim_free(tofree1);
17206 vim_free(tofree2);
17207 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208}
17209
17210 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211#ifdef __BORLANDC__
17212_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017214item_compare2(s1, s2)
17215 const void *s1;
17216 const void *s2;
17217{
17218 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017220 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017223 /* shortcut after failure in previous call; compare all items equal */
17224 if (item_compare_func_err)
17225 return 0;
17226
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017227 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17228 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17230 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231
17232 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017233 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017234 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17235 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017236 clear_tv(&argv[0]);
17237 clear_tv(&argv[1]);
17238
17239 if (res == FAIL)
17240 res = ITEM_COMPARE_FAIL;
17241 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017242 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17243 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017244 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 clear_tv(&rettv);
17246 return res;
17247}
17248
17249/*
17250 * "sort({list})" function
17251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017253do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017254 typval_T *argvars;
17255 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017256 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257{
Bram Moolenaar33570922005-01-25 22:26:29 +000017258 list_T *l;
17259 listitem_T *li;
17260 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261 long len;
17262 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263
Bram Moolenaar0d660222005-01-07 21:51:51 +000017264 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017265 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 else
17267 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017269 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017270 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271 return;
17272 rettv->vval.v_list = l;
17273 rettv->v_type = VAR_LIST;
17274 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276 len = list_len(l);
17277 if (len <= 1)
17278 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 item_compare_ic = FALSE;
17281 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017282 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 if (argvars[1].v_type != VAR_UNKNOWN)
17284 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017285 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017287 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017288 else
17289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 int error = FALSE;
17291
17292 i = get_tv_number_chk(&argvars[1], &error);
17293 if (error)
17294 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 if (i == 1)
17296 item_compare_ic = TRUE;
17297 else
17298 item_compare_func = get_tv_string(&argvars[1]);
17299 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017300
17301 if (argvars[2].v_type != VAR_UNKNOWN)
17302 {
17303 /* optional third argument: {dict} */
17304 if (argvars[2].v_type != VAR_DICT)
17305 {
17306 EMSG(_(e_dictreq));
17307 return;
17308 }
17309 item_compare_selfdict = argvars[2].vval.v_dict;
17310 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017314 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315 if (ptrs == NULL)
17316 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317
Bram Moolenaar327aa022014-03-25 18:24:23 +010017318 i = 0;
17319 if (sort)
17320 {
17321 /* sort(): ptrs will be the list to sort */
17322 for (li = l->lv_first; li != NULL; li = li->li_next)
17323 ptrs[i++] = li;
17324
17325 item_compare_func_err = FALSE;
17326 /* test the compare function */
17327 if (item_compare_func != NULL
17328 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017330 EMSG(_("E702: Sort compare function failed"));
17331 else
17332 {
17333 /* Sort the array with item pointers. */
17334 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17335 item_compare_func == NULL ? item_compare : item_compare2);
17336
17337 if (!item_compare_func_err)
17338 {
17339 /* Clear the List and append the items in sorted order. */
17340 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17341 l->lv_len = 0;
17342 for (i = 0; i < len; ++i)
17343 list_append(l, ptrs[i]);
17344 }
17345 }
17346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017349 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17350
17351 /* f_uniq(): ptrs will be a stack of items to remove */
17352 item_compare_func_err = FALSE;
17353 item_compare_func_ptr = item_compare_func
17354 ? item_compare2 : item_compare;
17355
17356 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17357 li = li->li_next)
17358 {
17359 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17360 == 0)
17361 ptrs[i++] = li;
17362 if (item_compare_func_err)
17363 {
17364 EMSG(_("E882: Uniq compare function failed"));
17365 break;
17366 }
17367 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 if (!item_compare_func_err)
17370 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017371 while (--i >= 0)
17372 {
17373 li = ptrs[i]->li_next;
17374 ptrs[i]->li_next = li->li_next;
17375 if (li->li_next != NULL)
17376 li->li_next->li_prev = ptrs[i];
17377 else
17378 l->lv_last = ptrs[i];
17379 list_fix_watch(l, li);
17380 listitem_free(li);
17381 l->lv_len--;
17382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384 }
17385
17386 vim_free(ptrs);
17387 }
17388}
17389
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017390/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017391 * "sort({list})" function
17392 */
17393 static void
17394f_sort(argvars, rettv)
17395 typval_T *argvars;
17396 typval_T *rettv;
17397{
17398 do_sort_uniq(argvars, rettv, TRUE);
17399}
17400
17401/*
17402 * "uniq({list})" function
17403 */
17404 static void
17405f_uniq(argvars, rettv)
17406 typval_T *argvars;
17407 typval_T *rettv;
17408{
17409 do_sort_uniq(argvars, rettv, FALSE);
17410}
17411
17412/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017413 * "soundfold({word})" function
17414 */
17415 static void
17416f_soundfold(argvars, rettv)
17417 typval_T *argvars;
17418 typval_T *rettv;
17419{
17420 char_u *s;
17421
17422 rettv->v_type = VAR_STRING;
17423 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017424#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017425 rettv->vval.v_string = eval_soundfold(s);
17426#else
17427 rettv->vval.v_string = vim_strsave(s);
17428#endif
17429}
17430
17431/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017432 * "spellbadword()" function
17433 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017434 static void
17435f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017436 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017437 typval_T *rettv;
17438{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017439 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017440 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017441 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017442
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017443 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017444 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017445
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017446#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017447 if (argvars[0].v_type == VAR_UNKNOWN)
17448 {
17449 /* Find the start and length of the badly spelled word. */
17450 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17451 if (len != 0)
17452 word = ml_get_cursor();
17453 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017454 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017455 {
17456 char_u *str = get_tv_string_chk(&argvars[0]);
17457 int capcol = -1;
17458
17459 if (str != NULL)
17460 {
17461 /* Check the argument for spelling. */
17462 while (*str != NUL)
17463 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017464 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017465 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017466 {
17467 word = str;
17468 break;
17469 }
17470 str += len;
17471 }
17472 }
17473 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017475
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017476 list_append_string(rettv->vval.v_list, word, len);
17477 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017478 attr == HLF_SPB ? "bad" :
17479 attr == HLF_SPR ? "rare" :
17480 attr == HLF_SPL ? "local" :
17481 attr == HLF_SPC ? "caps" :
17482 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017483}
17484
17485/*
17486 * "spellsuggest()" function
17487 */
17488 static void
17489f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017490 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017491 typval_T *rettv;
17492{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017493#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017494 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017495 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017496 int maxcount;
17497 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017498 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017499 listitem_T *li;
17500 int need_capital = FALSE;
17501#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017502
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017503 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017504 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017505
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017506#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017507 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017508 {
17509 str = get_tv_string(&argvars[0]);
17510 if (argvars[1].v_type != VAR_UNKNOWN)
17511 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017512 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017513 if (maxcount <= 0)
17514 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017515 if (argvars[2].v_type != VAR_UNKNOWN)
17516 {
17517 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17518 if (typeerr)
17519 return;
17520 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017521 }
17522 else
17523 maxcount = 25;
17524
Bram Moolenaar4770d092006-01-12 23:22:24 +000017525 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017526
17527 for (i = 0; i < ga.ga_len; ++i)
17528 {
17529 str = ((char_u **)ga.ga_data)[i];
17530
17531 li = listitem_alloc();
17532 if (li == NULL)
17533 vim_free(str);
17534 else
17535 {
17536 li->li_tv.v_type = VAR_STRING;
17537 li->li_tv.v_lock = 0;
17538 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017539 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017540 }
17541 }
17542 ga_clear(&ga);
17543 }
17544#endif
17545}
17546
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017548f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 typval_T *argvars;
17550 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551{
17552 char_u *str;
17553 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017554 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 regmatch_T regmatch;
17556 char_u patbuf[NUMBUFLEN];
17557 char_u *save_cpo;
17558 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017560 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017562
17563 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17564 save_cpo = p_cpo;
17565 p_cpo = (char_u *)"";
17566
17567 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017568 if (argvars[1].v_type != VAR_UNKNOWN)
17569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017570 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17571 if (pat == NULL)
17572 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017574 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017575 }
17576 if (pat == NULL || *pat == NUL)
17577 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017579 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017581 if (typeerr)
17582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17585 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017587 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017588 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017590 if (*str == NUL)
17591 match = FALSE; /* empty item at the end */
17592 else
17593 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 if (match)
17595 end = regmatch.startp[0];
17596 else
17597 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017598 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17599 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017601 if (list_append_string(rettv->vval.v_list, str,
17602 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604 }
17605 if (!match)
17606 break;
17607 /* Advance to just after the match. */
17608 if (regmatch.endp[0] > str)
17609 col = 0;
17610 else
17611 {
17612 /* Don't get stuck at the same match. */
17613#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017614 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615#else
17616 col = 1;
17617#endif
17618 }
17619 str = regmatch.endp[0];
17620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621
Bram Moolenaar473de612013-06-08 18:19:48 +020017622 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626}
17627
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017628#ifdef FEAT_FLOAT
17629/*
17630 * "sqrt()" function
17631 */
17632 static void
17633f_sqrt(argvars, rettv)
17634 typval_T *argvars;
17635 typval_T *rettv;
17636{
17637 float_T f;
17638
17639 rettv->v_type = VAR_FLOAT;
17640 if (get_float_arg(argvars, &f) == OK)
17641 rettv->vval.v_float = sqrt(f);
17642 else
17643 rettv->vval.v_float = 0.0;
17644}
17645
17646/*
17647 * "str2float()" function
17648 */
17649 static void
17650f_str2float(argvars, rettv)
17651 typval_T *argvars;
17652 typval_T *rettv;
17653{
17654 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17655
17656 if (*p == '+')
17657 p = skipwhite(p + 1);
17658 (void)string2float(p, &rettv->vval.v_float);
17659 rettv->v_type = VAR_FLOAT;
17660}
17661#endif
17662
Bram Moolenaar2c932302006-03-18 21:42:09 +000017663/*
17664 * "str2nr()" function
17665 */
17666 static void
17667f_str2nr(argvars, rettv)
17668 typval_T *argvars;
17669 typval_T *rettv;
17670{
17671 int base = 10;
17672 char_u *p;
17673 long n;
17674
17675 if (argvars[1].v_type != VAR_UNKNOWN)
17676 {
17677 base = get_tv_number(&argvars[1]);
17678 if (base != 8 && base != 10 && base != 16)
17679 {
17680 EMSG(_(e_invarg));
17681 return;
17682 }
17683 }
17684
17685 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017686 if (*p == '+')
17687 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017688 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17689 rettv->vval.v_number = n;
17690}
17691
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692#ifdef HAVE_STRFTIME
17693/*
17694 * "strftime({format}[, {time}])" function
17695 */
17696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 typval_T *argvars;
17699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700{
17701 char_u result_buf[256];
17702 struct tm *curtime;
17703 time_t seconds;
17704 char_u *p;
17705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017708 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017709 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 seconds = time(NULL);
17711 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017712 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 curtime = localtime(&seconds);
17714 /* MSVC returns NULL for an invalid value of seconds. */
17715 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 else
17718 {
17719# ifdef FEAT_MBYTE
17720 vimconv_T conv;
17721 char_u *enc;
17722
17723 conv.vc_type = CONV_NONE;
17724 enc = enc_locale();
17725 convert_setup(&conv, p_enc, enc);
17726 if (conv.vc_type != CONV_NONE)
17727 p = string_convert(&conv, p, NULL);
17728# endif
17729 if (p != NULL)
17730 (void)strftime((char *)result_buf, sizeof(result_buf),
17731 (char *)p, curtime);
17732 else
17733 result_buf[0] = NUL;
17734
17735# ifdef FEAT_MBYTE
17736 if (conv.vc_type != CONV_NONE)
17737 vim_free(p);
17738 convert_setup(&conv, enc, p_enc);
17739 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 else
17742# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
17745# ifdef FEAT_MBYTE
17746 /* Release conversion descriptors */
17747 convert_setup(&conv, NULL, NULL);
17748 vim_free(enc);
17749# endif
17750 }
17751}
17752#endif
17753
17754/*
17755 * "stridx()" function
17756 */
17757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
17762 char_u buf[NUMBUFLEN];
17763 char_u *needle;
17764 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017769 needle = get_tv_string_chk(&argvars[1]);
17770 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017771 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017772 if (needle == NULL || haystack == NULL)
17773 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 if (argvars[2].v_type != VAR_UNKNOWN)
17776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017777 int error = FALSE;
17778
17779 start_idx = get_tv_number_chk(&argvars[2], &error);
17780 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017782 if (start_idx >= 0)
17783 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 }
17785
17786 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17787 if (pos != NULL)
17788 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789}
17790
17791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017792 * "string()" function
17793 */
17794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017796 typval_T *argvars;
17797 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017798{
17799 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017800 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017803 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017804 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017805 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807}
17808
17809/*
17810 * "strlen()" function
17811 */
17812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 typval_T *argvars;
17815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017817 rettv->vval.v_number = (varnumber_T)(STRLEN(
17818 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819}
17820
17821/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017822 * "strchars()" function
17823 */
17824 static void
17825f_strchars(argvars, rettv)
17826 typval_T *argvars;
17827 typval_T *rettv;
17828{
17829 char_u *s = get_tv_string(&argvars[0]);
17830#ifdef FEAT_MBYTE
17831 varnumber_T len = 0;
17832
17833 while (*s != NUL)
17834 {
17835 mb_cptr2char_adv(&s);
17836 ++len;
17837 }
17838 rettv->vval.v_number = len;
17839#else
17840 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17841#endif
17842}
17843
17844/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017845 * "strdisplaywidth()" function
17846 */
17847 static void
17848f_strdisplaywidth(argvars, rettv)
17849 typval_T *argvars;
17850 typval_T *rettv;
17851{
17852 char_u *s = get_tv_string(&argvars[0]);
17853 int col = 0;
17854
17855 if (argvars[1].v_type != VAR_UNKNOWN)
17856 col = get_tv_number(&argvars[1]);
17857
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017858 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017859}
17860
17861/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017862 * "strwidth()" function
17863 */
17864 static void
17865f_strwidth(argvars, rettv)
17866 typval_T *argvars;
17867 typval_T *rettv;
17868{
17869 char_u *s = get_tv_string(&argvars[0]);
17870
17871 rettv->vval.v_number = (varnumber_T)(
17872#ifdef FEAT_MBYTE
17873 mb_string2cells(s, -1)
17874#else
17875 STRLEN(s)
17876#endif
17877 );
17878}
17879
17880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 * "strpart()" function
17882 */
17883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017884f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017885 typval_T *argvars;
17886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887{
17888 char_u *p;
17889 int n;
17890 int len;
17891 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017892 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 slen = (int)STRLEN(p);
17896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017897 n = get_tv_number_chk(&argvars[1], &error);
17898 if (error)
17899 len = 0;
17900 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017901 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 else
17903 len = slen - n; /* default len: all bytes that are available. */
17904
17905 /*
17906 * Only return the overlap between the specified part and the actual
17907 * string.
17908 */
17909 if (n < 0)
17910 {
17911 len += n;
17912 n = 0;
17913 }
17914 else if (n > slen)
17915 n = slen;
17916 if (len < 0)
17917 len = 0;
17918 else if (n + len > slen)
17919 len = slen - n;
17920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017921 rettv->v_type = VAR_STRING;
17922 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923}
17924
17925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 * "strridx()" function
17927 */
17928 static void
17929f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017930 typval_T *argvars;
17931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932{
17933 char_u buf[NUMBUFLEN];
17934 char_u *needle;
17935 char_u *haystack;
17936 char_u *rest;
17937 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017938 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017940 needle = get_tv_string_chk(&argvars[1]);
17941 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017942
17943 rettv->vval.v_number = -1;
17944 if (needle == NULL || haystack == NULL)
17945 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017946
17947 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017948 if (argvars[2].v_type != VAR_UNKNOWN)
17949 {
17950 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017951 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017952 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017953 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017954 }
17955 else
17956 end_idx = haystack_len;
17957
Bram Moolenaar0d660222005-01-07 21:51:51 +000017958 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017959 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017960 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017961 lastmatch = haystack + end_idx;
17962 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017964 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017965 for (rest = haystack; *rest != '\0'; ++rest)
17966 {
17967 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017968 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017969 break;
17970 lastmatch = rest;
17971 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973
17974 if (lastmatch == NULL)
17975 rettv->vval.v_number = -1;
17976 else
17977 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17978}
17979
17980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 * "strtrans()" function
17982 */
17983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 typval_T *argvars;
17986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017988 rettv->v_type = VAR_STRING;
17989 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990}
17991
17992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017993 * "submatch()" function
17994 */
17995 static void
17996f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017997 typval_T *argvars;
17998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999{
Bram Moolenaar41571762014-04-02 19:00:58 +020018000 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018001 int no;
18002 int retList = 0;
18003
18004 no = (int)get_tv_number_chk(&argvars[0], &error);
18005 if (error)
18006 return;
18007 error = FALSE;
18008 if (argvars[1].v_type != VAR_UNKNOWN)
18009 retList = get_tv_number_chk(&argvars[1], &error);
18010 if (error)
18011 return;
18012
18013 if (retList == 0)
18014 {
18015 rettv->v_type = VAR_STRING;
18016 rettv->vval.v_string = reg_submatch(no);
18017 }
18018 else
18019 {
18020 rettv->v_type = VAR_LIST;
18021 rettv->vval.v_list = reg_submatch_list(no);
18022 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023}
18024
18025/*
18026 * "substitute()" function
18027 */
18028 static void
18029f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 typval_T *argvars;
18031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018032{
18033 char_u patbuf[NUMBUFLEN];
18034 char_u subbuf[NUMBUFLEN];
18035 char_u flagsbuf[NUMBUFLEN];
18036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018037 char_u *str = get_tv_string_chk(&argvars[0]);
18038 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18039 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18040 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18041
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018043 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18044 rettv->vval.v_string = NULL;
18045 else
18046 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047}
18048
18049/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018050 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056{
18057 int id = 0;
18058#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018059 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 long col;
18061 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018062 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018064 lnum = get_tv_lnum(argvars); /* -1 on type error */
18065 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18066 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018068 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018069 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018070 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071#endif
18072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018073 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074}
18075
18076/*
18077 * "synIDattr(id, what [, mode])" function
18078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018080f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018081 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083{
18084 char_u *p = NULL;
18085#ifdef FEAT_SYN_HL
18086 int id;
18087 char_u *what;
18088 char_u *mode;
18089 char_u modebuf[NUMBUFLEN];
18090 int modec;
18091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 id = get_tv_number(&argvars[0]);
18093 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018096 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018098 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 modec = 0; /* replace invalid with current */
18100 }
18101 else
18102 {
18103#ifdef FEAT_GUI
18104 if (gui.in_use)
18105 modec = 'g';
18106 else
18107#endif
18108 if (t_colors > 1)
18109 modec = 'c';
18110 else
18111 modec = 't';
18112 }
18113
18114
18115 switch (TOLOWER_ASC(what[0]))
18116 {
18117 case 'b':
18118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18119 p = highlight_color(id, what, modec);
18120 else /* bold */
18121 p = highlight_has_attr(id, HL_BOLD, modec);
18122 break;
18123
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018124 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 p = highlight_color(id, what, modec);
18126 break;
18127
18128 case 'i':
18129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18130 p = highlight_has_attr(id, HL_INVERSE, modec);
18131 else /* italic */
18132 p = highlight_has_attr(id, HL_ITALIC, modec);
18133 break;
18134
18135 case 'n': /* name */
18136 p = get_highlight_name(NULL, id - 1);
18137 break;
18138
18139 case 'r': /* reverse */
18140 p = highlight_has_attr(id, HL_INVERSE, modec);
18141 break;
18142
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018143 case 's':
18144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18145 p = highlight_color(id, what, modec);
18146 else /* standout */
18147 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 break;
18149
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018150 case 'u':
18151 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18152 /* underline */
18153 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18154 else
18155 /* undercurl */
18156 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 break;
18158 }
18159
18160 if (p != NULL)
18161 p = vim_strsave(p);
18162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163 rettv->v_type = VAR_STRING;
18164 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165}
18166
18167/*
18168 * "synIDtrans(id)" function
18169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018172 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174{
18175 int id;
18176
18177#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179
18180 if (id > 0)
18181 id = syn_get_final_id(id);
18182 else
18183#endif
18184 id = 0;
18185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018186 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187}
18188
18189/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018190 * "synconcealed(lnum, col)" function
18191 */
18192 static void
18193f_synconcealed(argvars, rettv)
18194 typval_T *argvars UNUSED;
18195 typval_T *rettv;
18196{
18197#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18198 long lnum;
18199 long col;
18200 int syntax_flags = 0;
18201 int cchar;
18202 int matchid = 0;
18203 char_u str[NUMBUFLEN];
18204#endif
18205
18206 rettv->v_type = VAR_LIST;
18207 rettv->vval.v_list = NULL;
18208
18209#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18210 lnum = get_tv_lnum(argvars); /* -1 on type error */
18211 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18212
18213 vim_memset(str, NUL, sizeof(str));
18214
18215 if (rettv_list_alloc(rettv) != FAIL)
18216 {
18217 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18218 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18219 && curwin->w_p_cole > 0)
18220 {
18221 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18222 syntax_flags = get_syntax_info(&matchid);
18223
18224 /* get the conceal character */
18225 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18226 {
18227 cchar = syn_get_sub_char();
18228 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18229 cchar = lcs_conceal;
18230 if (cchar != NUL)
18231 {
18232# ifdef FEAT_MBYTE
18233 if (has_mbyte)
18234 (*mb_char2bytes)(cchar, str);
18235 else
18236# endif
18237 str[0] = cchar;
18238 }
18239 }
18240 }
18241
18242 list_append_number(rettv->vval.v_list,
18243 (syntax_flags & HL_CONCEAL) != 0);
18244 /* -1 to auto-determine strlen */
18245 list_append_string(rettv->vval.v_list, str, -1);
18246 list_append_number(rettv->vval.v_list, matchid);
18247 }
18248#endif
18249}
18250
18251/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018252 * "synstack(lnum, col)" function
18253 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018254 static void
18255f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018256 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018257 typval_T *rettv;
18258{
18259#ifdef FEAT_SYN_HL
18260 long lnum;
18261 long col;
18262 int i;
18263 int id;
18264#endif
18265
18266 rettv->v_type = VAR_LIST;
18267 rettv->vval.v_list = NULL;
18268
18269#ifdef FEAT_SYN_HL
18270 lnum = get_tv_lnum(argvars); /* -1 on type error */
18271 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18272
18273 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018274 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018275 && rettv_list_alloc(rettv) != FAIL)
18276 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018277 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018278 for (i = 0; ; ++i)
18279 {
18280 id = syn_get_stack_item(i);
18281 if (id < 0)
18282 break;
18283 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18284 break;
18285 }
18286 }
18287#endif
18288}
18289
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018291get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018292 typval_T *argvars;
18293 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018294 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018296 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018298 char_u *infile = NULL;
18299 char_u buf[NUMBUFLEN];
18300 int err = FALSE;
18301 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018302 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018304 rettv->v_type = VAR_STRING;
18305 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018306 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018307 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018308
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018309 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018310 {
18311 /*
18312 * Write the string to a temp file, to be used for input of the shell
18313 * command.
18314 */
18315 if ((infile = vim_tempname('i')) == NULL)
18316 {
18317 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018318 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018319 }
18320
18321 fd = mch_fopen((char *)infile, WRITEBIN);
18322 if (fd == NULL)
18323 {
18324 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018325 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018326 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018327 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018328 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018329 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18330 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018331 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018332 else
18333 {
18334 p = get_tv_string_buf_chk(&argvars[1], buf);
18335 if (p == NULL)
18336 {
18337 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018338 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018339 }
18340 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18341 err = TRUE;
18342 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018343 if (fclose(fd) != 0)
18344 err = TRUE;
18345 if (err)
18346 {
18347 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018348 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018349 }
18350 }
18351
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018352 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018354 int len;
18355 listitem_T *li;
18356 char_u *s = NULL;
18357 char_u *start;
18358 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018359 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018361 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18362 SHELL_SILENT | SHELL_COOKED, &len);
18363 if (res == NULL)
18364 goto errret;
18365
18366 list = list_alloc();
18367 if (list == NULL)
18368 goto errret;
18369
18370 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018372 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018373 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018374 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018375 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018376
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018377 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018378 if (s == NULL)
18379 goto errret;
18380
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018381 for (p = s; start < end; ++p, ++start)
18382 *p = *start == NUL ? NL : *start;
18383 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018384
18385 li = listitem_alloc();
18386 if (li == NULL)
18387 {
18388 vim_free(s);
18389 goto errret;
18390 }
18391 li->li_tv.v_type = VAR_STRING;
18392 li->li_tv.vval.v_string = s;
18393 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018395
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018396 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018397 rettv->v_type = VAR_LIST;
18398 rettv->vval.v_list = list;
18399 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018401 else
18402 {
18403 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18404 SHELL_SILENT | SHELL_COOKED, NULL);
18405#ifdef USE_CR
18406 /* translate <CR> into <NL> */
18407 if (res != NULL)
18408 {
18409 char_u *s;
18410
18411 for (s = res; *s; ++s)
18412 {
18413 if (*s == CAR)
18414 *s = NL;
18415 }
18416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417#else
18418# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018419 /* translate <CR><NL> into <NL> */
18420 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018422 char_u *s, *d;
18423
18424 d = res;
18425 for (s = res; *s; ++s)
18426 {
18427 if (s[0] == CAR && s[1] == NL)
18428 ++s;
18429 *d++ = *s;
18430 }
18431 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433# endif
18434#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018435 rettv->vval.v_string = res;
18436 res = NULL;
18437 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018438
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018439errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018440 if (infile != NULL)
18441 {
18442 mch_remove(infile);
18443 vim_free(infile);
18444 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018445 if (res != NULL)
18446 vim_free(res);
18447 if (list != NULL)
18448 list_free(list, TRUE);
18449}
18450
18451/*
18452 * "system()" function
18453 */
18454 static void
18455f_system(argvars, rettv)
18456 typval_T *argvars;
18457 typval_T *rettv;
18458{
18459 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18460}
18461
18462/*
18463 * "systemlist()" function
18464 */
18465 static void
18466f_systemlist(argvars, rettv)
18467 typval_T *argvars;
18468 typval_T *rettv;
18469{
18470 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471}
18472
18473/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018474 * "tabpagebuflist()" function
18475 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018476 static void
18477f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018478 typval_T *argvars UNUSED;
18479 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018480{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018481#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018482 tabpage_T *tp;
18483 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018484
18485 if (argvars[0].v_type == VAR_UNKNOWN)
18486 wp = firstwin;
18487 else
18488 {
18489 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18490 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018491 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018492 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018493 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018494 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018495 for (; wp != NULL; wp = wp->w_next)
18496 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018497 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018498 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018499 }
18500#endif
18501}
18502
18503
18504/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018505 * "tabpagenr()" function
18506 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018507 static void
18508f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018509 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018510 typval_T *rettv;
18511{
18512 int nr = 1;
18513#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018514 char_u *arg;
18515
18516 if (argvars[0].v_type != VAR_UNKNOWN)
18517 {
18518 arg = get_tv_string_chk(&argvars[0]);
18519 nr = 0;
18520 if (arg != NULL)
18521 {
18522 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018523 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018524 else
18525 EMSG2(_(e_invexpr2), arg);
18526 }
18527 }
18528 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018529 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018530#endif
18531 rettv->vval.v_number = nr;
18532}
18533
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018534
18535#ifdef FEAT_WINDOWS
18536static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18537
18538/*
18539 * Common code for tabpagewinnr() and winnr().
18540 */
18541 static int
18542get_winnr(tp, argvar)
18543 tabpage_T *tp;
18544 typval_T *argvar;
18545{
18546 win_T *twin;
18547 int nr = 1;
18548 win_T *wp;
18549 char_u *arg;
18550
18551 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18552 if (argvar->v_type != VAR_UNKNOWN)
18553 {
18554 arg = get_tv_string_chk(argvar);
18555 if (arg == NULL)
18556 nr = 0; /* type error; errmsg already given */
18557 else if (STRCMP(arg, "$") == 0)
18558 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18559 else if (STRCMP(arg, "#") == 0)
18560 {
18561 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18562 if (twin == NULL)
18563 nr = 0;
18564 }
18565 else
18566 {
18567 EMSG2(_(e_invexpr2), arg);
18568 nr = 0;
18569 }
18570 }
18571
18572 if (nr > 0)
18573 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18574 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018575 {
18576 if (wp == NULL)
18577 {
18578 /* didn't find it in this tabpage */
18579 nr = 0;
18580 break;
18581 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018582 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018583 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018584 return nr;
18585}
18586#endif
18587
18588/*
18589 * "tabpagewinnr()" function
18590 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018591 static void
18592f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018593 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018594 typval_T *rettv;
18595{
18596 int nr = 1;
18597#ifdef FEAT_WINDOWS
18598 tabpage_T *tp;
18599
18600 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18601 if (tp == NULL)
18602 nr = 0;
18603 else
18604 nr = get_winnr(tp, &argvars[1]);
18605#endif
18606 rettv->vval.v_number = nr;
18607}
18608
18609
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018610/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018611 * "tagfiles()" function
18612 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018613 static void
18614f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018615 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018616 typval_T *rettv;
18617{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018618 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018619 tagname_T tn;
18620 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018622 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018623 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018624 fname = alloc(MAXPATHL);
18625 if (fname == NULL)
18626 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018627
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018628 for (first = TRUE; ; first = FALSE)
18629 if (get_tagfname(&tn, first, fname) == FAIL
18630 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018631 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018632 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018633 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018634}
18635
18636/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018637 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018638 */
18639 static void
18640f_taglist(argvars, rettv)
18641 typval_T *argvars;
18642 typval_T *rettv;
18643{
18644 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018645
18646 tag_pattern = get_tv_string(&argvars[0]);
18647
18648 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 if (*tag_pattern == NUL)
18650 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018651
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018652 if (rettv_list_alloc(rettv) == OK)
18653 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018654}
18655
18656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 * "tempname()" function
18658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018661 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663{
18664 static int x = 'A';
18665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666 rettv->v_type = VAR_STRING;
18667 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668
18669 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18670 * names. Skip 'I' and 'O', they are used for shell redirection. */
18671 do
18672 {
18673 if (x == 'Z')
18674 x = '0';
18675 else if (x == '9')
18676 x = 'A';
18677 else
18678 {
18679#ifdef EBCDIC
18680 if (x == 'I')
18681 x = 'J';
18682 else if (x == 'R')
18683 x = 'S';
18684 else
18685#endif
18686 ++x;
18687 }
18688 } while (x == 'I' || x == 'O');
18689}
18690
18691/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018692 * "test(list)" function: Just checking the walls...
18693 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018694 static void
18695f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018696 typval_T *argvars UNUSED;
18697 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018698{
18699 /* Used for unit testing. Change the code below to your liking. */
18700#if 0
18701 listitem_T *li;
18702 list_T *l;
18703 char_u *bad, *good;
18704
18705 if (argvars[0].v_type != VAR_LIST)
18706 return;
18707 l = argvars[0].vval.v_list;
18708 if (l == NULL)
18709 return;
18710 li = l->lv_first;
18711 if (li == NULL)
18712 return;
18713 bad = get_tv_string(&li->li_tv);
18714 li = li->li_next;
18715 if (li == NULL)
18716 return;
18717 good = get_tv_string(&li->li_tv);
18718 rettv->vval.v_number = test_edit_score(bad, good);
18719#endif
18720}
18721
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018722#ifdef FEAT_FLOAT
18723/*
18724 * "tan()" function
18725 */
18726 static void
18727f_tan(argvars, rettv)
18728 typval_T *argvars;
18729 typval_T *rettv;
18730{
18731 float_T f;
18732
18733 rettv->v_type = VAR_FLOAT;
18734 if (get_float_arg(argvars, &f) == OK)
18735 rettv->vval.v_float = tan(f);
18736 else
18737 rettv->vval.v_float = 0.0;
18738}
18739
18740/*
18741 * "tanh()" function
18742 */
18743 static void
18744f_tanh(argvars, rettv)
18745 typval_T *argvars;
18746 typval_T *rettv;
18747{
18748 float_T f;
18749
18750 rettv->v_type = VAR_FLOAT;
18751 if (get_float_arg(argvars, &f) == OK)
18752 rettv->vval.v_float = tanh(f);
18753 else
18754 rettv->vval.v_float = 0.0;
18755}
18756#endif
18757
Bram Moolenaard52d9742005-08-21 22:20:28 +000018758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 * "tolower(string)" function
18760 */
18761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018763 typval_T *argvars;
18764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765{
18766 char_u *p;
18767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 p = vim_strsave(get_tv_string(&argvars[0]));
18769 rettv->v_type = VAR_STRING;
18770 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771
18772 if (p != NULL)
18773 while (*p != NUL)
18774 {
18775#ifdef FEAT_MBYTE
18776 int l;
18777
18778 if (enc_utf8)
18779 {
18780 int c, lc;
18781
18782 c = utf_ptr2char(p);
18783 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018784 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 /* TODO: reallocate string when byte count changes. */
18786 if (utf_char2len(lc) == l)
18787 utf_char2bytes(lc, p);
18788 p += l;
18789 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018790 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 p += l; /* skip multi-byte character */
18792 else
18793#endif
18794 {
18795 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18796 ++p;
18797 }
18798 }
18799}
18800
18801/*
18802 * "toupper(string)" function
18803 */
18804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 typval_T *argvars;
18807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018810 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811}
18812
18813/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018814 * "tr(string, fromstr, tostr)" function
18815 */
18816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018817f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018818 typval_T *argvars;
18819 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018820{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018821 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018822 char_u *fromstr;
18823 char_u *tostr;
18824 char_u *p;
18825#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018826 int inlen;
18827 int fromlen;
18828 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018829 int idx;
18830 char_u *cpstr;
18831 int cplen;
18832 int first = TRUE;
18833#endif
18834 char_u buf[NUMBUFLEN];
18835 char_u buf2[NUMBUFLEN];
18836 garray_T ga;
18837
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018838 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018839 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18840 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018841
18842 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 rettv->v_type = VAR_STRING;
18844 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018845 if (fromstr == NULL || tostr == NULL)
18846 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018847 ga_init2(&ga, (int)sizeof(char), 80);
18848
18849#ifdef FEAT_MBYTE
18850 if (!has_mbyte)
18851#endif
18852 /* not multi-byte: fromstr and tostr must be the same length */
18853 if (STRLEN(fromstr) != STRLEN(tostr))
18854 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018855#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018856error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018857#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018858 EMSG2(_(e_invarg2), fromstr);
18859 ga_clear(&ga);
18860 return;
18861 }
18862
18863 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018864 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018865 {
18866#ifdef FEAT_MBYTE
18867 if (has_mbyte)
18868 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018869 inlen = (*mb_ptr2len)(in_str);
18870 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018871 cplen = inlen;
18872 idx = 0;
18873 for (p = fromstr; *p != NUL; p += fromlen)
18874 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018875 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018876 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018877 {
18878 for (p = tostr; *p != NUL; p += tolen)
18879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018880 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018881 if (idx-- == 0)
18882 {
18883 cplen = tolen;
18884 cpstr = p;
18885 break;
18886 }
18887 }
18888 if (*p == NUL) /* tostr is shorter than fromstr */
18889 goto error;
18890 break;
18891 }
18892 ++idx;
18893 }
18894
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018895 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018896 {
18897 /* Check that fromstr and tostr have the same number of
18898 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018899 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018900 first = FALSE;
18901 for (p = tostr; *p != NUL; p += tolen)
18902 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018903 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018904 --idx;
18905 }
18906 if (idx != 0)
18907 goto error;
18908 }
18909
18910 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018911 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018912 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018913
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018914 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018915 }
18916 else
18917#endif
18918 {
18919 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018920 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018921 if (p != NULL)
18922 ga_append(&ga, tostr[p - fromstr]);
18923 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018924 ga_append(&ga, *in_str);
18925 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018926 }
18927 }
18928
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018929 /* add a terminating NUL */
18930 ga_grow(&ga, 1);
18931 ga_append(&ga, NUL);
18932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018934}
18935
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018936#ifdef FEAT_FLOAT
18937/*
18938 * "trunc({float})" function
18939 */
18940 static void
18941f_trunc(argvars, rettv)
18942 typval_T *argvars;
18943 typval_T *rettv;
18944{
18945 float_T f;
18946
18947 rettv->v_type = VAR_FLOAT;
18948 if (get_float_arg(argvars, &f) == OK)
18949 /* trunc() is not in C90, use floor() or ceil() instead. */
18950 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18951 else
18952 rettv->vval.v_float = 0.0;
18953}
18954#endif
18955
Bram Moolenaar8299df92004-07-10 09:47:34 +000018956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 * "type(expr)" function
18958 */
18959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018960f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018961 typval_T *argvars;
18962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018964 int n;
18965
18966 switch (argvars[0].v_type)
18967 {
18968 case VAR_NUMBER: n = 0; break;
18969 case VAR_STRING: n = 1; break;
18970 case VAR_FUNC: n = 2; break;
18971 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018972 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018973#ifdef FEAT_FLOAT
18974 case VAR_FLOAT: n = 5; break;
18975#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018976 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18977 }
18978 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979}
18980
18981/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018982 * "undofile(name)" function
18983 */
18984 static void
18985f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018986 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018987 typval_T *rettv;
18988{
18989 rettv->v_type = VAR_STRING;
18990#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018991 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018992 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018993
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018994 if (*fname == NUL)
18995 {
18996 /* If there is no file name there will be no undo file. */
18997 rettv->vval.v_string = NULL;
18998 }
18999 else
19000 {
19001 char_u *ffname = FullName_save(fname, FALSE);
19002
19003 if (ffname != NULL)
19004 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19005 vim_free(ffname);
19006 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019007 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019008#else
19009 rettv->vval.v_string = NULL;
19010#endif
19011}
19012
19013/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019014 * "undotree()" function
19015 */
19016 static void
19017f_undotree(argvars, rettv)
19018 typval_T *argvars UNUSED;
19019 typval_T *rettv;
19020{
19021 if (rettv_dict_alloc(rettv) == OK)
19022 {
19023 dict_T *dict = rettv->vval.v_dict;
19024 list_T *list;
19025
Bram Moolenaar730cde92010-06-27 05:18:54 +020019026 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019027 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019028 dict_add_nr_str(dict, "save_last",
19029 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019030 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19031 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019032 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019033
19034 list = list_alloc();
19035 if (list != NULL)
19036 {
19037 u_eval_tree(curbuf->b_u_oldhead, list);
19038 dict_add_list(dict, "entries", list);
19039 }
19040 }
19041}
19042
19043/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019044 * "values(dict)" function
19045 */
19046 static void
19047f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019048 typval_T *argvars;
19049 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019050{
19051 dict_list(argvars, rettv, 1);
19052}
19053
19054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 * "virtcol(string)" function
19056 */
19057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019058f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 typval_T *argvars;
19060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061{
19062 colnr_T vcol = 0;
19063 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019064 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019066 fp = var2fpos(&argvars[0], FALSE, &fnum);
19067 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19068 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 {
19070 getvvcol(curwin, fp, NULL, NULL, &vcol);
19071 ++vcol;
19072 }
19073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019074 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075}
19076
19077/*
19078 * "visualmode()" function
19079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019082 typval_T *argvars UNUSED;
19083 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 char_u str[2];
19086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 str[0] = curbuf->b_visual_mode_eval;
19089 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019090 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091
19092 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019093 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095}
19096
19097/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019098 * "wildmenumode()" function
19099 */
19100 static void
19101f_wildmenumode(argvars, rettv)
19102 typval_T *argvars UNUSED;
19103 typval_T *rettv UNUSED;
19104{
19105#ifdef FEAT_WILDMENU
19106 if (wild_menu_showing)
19107 rettv->vval.v_number = 1;
19108#endif
19109}
19110
19111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 * "winbufnr(nr)" function
19113 */
19114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 typval_T *argvars;
19117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118{
19119 win_T *wp;
19120
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019121 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019125 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126}
19127
19128/*
19129 * "wincol()" function
19130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019133 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135{
19136 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140/*
19141 * "winheight(nr)" function
19142 */
19143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 typval_T *argvars;
19146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147{
19148 win_T *wp;
19149
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019150 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019154 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155}
19156
19157/*
19158 * "winline()" function
19159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
19165 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167}
19168
19169/*
19170 * "winnr()" function
19171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176{
19177 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019178
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019180 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019182 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183}
19184
19185/*
19186 * "winrestcmd()" function
19187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192{
19193#ifdef FEAT_WINDOWS
19194 win_T *wp;
19195 int winnr = 1;
19196 garray_T ga;
19197 char_u buf[50];
19198
19199 ga_init2(&ga, (int)sizeof(char), 70);
19200 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19201 {
19202 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19203 ga_concat(&ga, buf);
19204# ifdef FEAT_VERTSPLIT
19205 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19206 ga_concat(&ga, buf);
19207# endif
19208 ++winnr;
19209 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019210 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019212 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217}
19218
19219/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019220 * "winrestview()" function
19221 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019222 static void
19223f_winrestview(argvars, rettv)
19224 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019225 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019226{
19227 dict_T *dict;
19228
19229 if (argvars[0].v_type != VAR_DICT
19230 || (dict = argvars[0].vval.v_dict) == NULL)
19231 EMSG(_(e_invarg));
19232 else
19233 {
19234 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19235 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19236#ifdef FEAT_VIRTUALEDIT
19237 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19238#endif
19239 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019240 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019241
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019242 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019243#ifdef FEAT_DIFF
19244 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19245#endif
19246 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19247 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19248
19249 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019250 win_new_height(curwin, curwin->w_height);
19251# ifdef FEAT_VERTSPLIT
19252 win_new_width(curwin, W_WIDTH(curwin));
19253# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019254 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019255
19256 if (curwin->w_topline == 0)
19257 curwin->w_topline = 1;
19258 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19259 curwin->w_topline = curbuf->b_ml.ml_line_count;
19260#ifdef FEAT_DIFF
19261 check_topfill(curwin, TRUE);
19262#endif
19263 }
19264}
19265
19266/*
19267 * "winsaveview()" function
19268 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019269 static void
19270f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019271 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019272 typval_T *rettv;
19273{
19274 dict_T *dict;
19275
Bram Moolenaara800b422010-06-27 01:15:55 +020019276 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019277 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019278 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019279
19280 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19281 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19282#ifdef FEAT_VIRTUALEDIT
19283 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19284#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019285 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019286 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19287
19288 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19289#ifdef FEAT_DIFF
19290 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19291#endif
19292 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19293 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19294}
19295
19296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 * "winwidth(nr)" function
19298 */
19299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019300f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019301 typval_T *argvars;
19302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303{
19304 win_T *wp;
19305
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019306 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 else
19310#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019311 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019313 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314#endif
19315}
19316
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019318 * Write list of strings to file
19319 */
19320 static int
19321write_list(fd, list, binary)
19322 FILE *fd;
19323 list_T *list;
19324 int binary;
19325{
19326 listitem_T *li;
19327 int c;
19328 int ret = OK;
19329 char_u *s;
19330
19331 for (li = list->lv_first; li != NULL; li = li->li_next)
19332 {
19333 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19334 {
19335 if (*s == '\n')
19336 c = putc(NUL, fd);
19337 else
19338 c = putc(*s, fd);
19339 if (c == EOF)
19340 {
19341 ret = FAIL;
19342 break;
19343 }
19344 }
19345 if (!binary || li->li_next != NULL)
19346 if (putc('\n', fd) == EOF)
19347 {
19348 ret = FAIL;
19349 break;
19350 }
19351 if (ret == FAIL)
19352 {
19353 EMSG(_(e_write));
19354 break;
19355 }
19356 }
19357 return ret;
19358}
19359
19360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019361 * "writefile()" function
19362 */
19363 static void
19364f_writefile(argvars, rettv)
19365 typval_T *argvars;
19366 typval_T *rettv;
19367{
19368 int binary = FALSE;
19369 char_u *fname;
19370 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019371 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019372
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019373 if (check_restricted() || check_secure())
19374 return;
19375
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019376 if (argvars[0].v_type != VAR_LIST)
19377 {
19378 EMSG2(_(e_listarg), "writefile()");
19379 return;
19380 }
19381 if (argvars[0].vval.v_list == NULL)
19382 return;
19383
19384 if (argvars[2].v_type != VAR_UNKNOWN
19385 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19386 binary = TRUE;
19387
19388 /* Always open the file in binary mode, library functions have a mind of
19389 * their own about CR-LF conversion. */
19390 fname = get_tv_string(&argvars[1]);
19391 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19392 {
19393 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19394 ret = -1;
19395 }
19396 else
19397 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019398 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19399 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019400 fclose(fd);
19401 }
19402
19403 rettv->vval.v_number = ret;
19404}
19405
19406/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019407 * "xor(expr, expr)" function
19408 */
19409 static void
19410f_xor(argvars, rettv)
19411 typval_T *argvars;
19412 typval_T *rettv;
19413{
19414 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19415 ^ get_tv_number_chk(&argvars[1], NULL);
19416}
19417
19418
19419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019421 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 */
19423 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019424var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019426 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019427 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019429 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019431 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432
Bram Moolenaara5525202006-03-02 22:52:09 +000019433 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019434 if (varp->v_type == VAR_LIST)
19435 {
19436 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019437 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019438 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019439 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019440
19441 l = varp->vval.v_list;
19442 if (l == NULL)
19443 return NULL;
19444
19445 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019446 pos.lnum = list_find_nr(l, 0L, &error);
19447 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019448 return NULL; /* invalid line number */
19449
19450 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019451 pos.col = list_find_nr(l, 1L, &error);
19452 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019453 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019454 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019455
19456 /* We accept "$" for the column number: last column. */
19457 li = list_find(l, 1L);
19458 if (li != NULL && li->li_tv.v_type == VAR_STRING
19459 && li->li_tv.vval.v_string != NULL
19460 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19461 pos.col = len + 1;
19462
Bram Moolenaara5525202006-03-02 22:52:09 +000019463 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019464 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019465 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019466 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019467
Bram Moolenaara5525202006-03-02 22:52:09 +000019468#ifdef FEAT_VIRTUALEDIT
19469 /* Get the virtual offset. Defaults to zero. */
19470 pos.coladd = list_find_nr(l, 2L, &error);
19471 if (error)
19472 pos.coladd = 0;
19473#endif
19474
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019475 return &pos;
19476 }
19477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019478 name = get_tv_string_chk(varp);
19479 if (name == NULL)
19480 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019481 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019483 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19484 {
19485 if (VIsual_active)
19486 return &VIsual;
19487 return &curwin->w_cursor;
19488 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019489 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019491 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19493 return NULL;
19494 return pp;
19495 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019496
19497#ifdef FEAT_VIRTUALEDIT
19498 pos.coladd = 0;
19499#endif
19500
Bram Moolenaar477933c2007-07-17 14:32:23 +000019501 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019502 {
19503 pos.col = 0;
19504 if (name[1] == '0') /* "w0": first visible line */
19505 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019506 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019507 pos.lnum = curwin->w_topline;
19508 return &pos;
19509 }
19510 else if (name[1] == '$') /* "w$": last visible line */
19511 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019512 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019513 pos.lnum = curwin->w_botline - 1;
19514 return &pos;
19515 }
19516 }
19517 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019519 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 {
19521 pos.lnum = curbuf->b_ml.ml_line_count;
19522 pos.col = 0;
19523 }
19524 else
19525 {
19526 pos.lnum = curwin->w_cursor.lnum;
19527 pos.col = (colnr_T)STRLEN(ml_get_curline());
19528 }
19529 return &pos;
19530 }
19531 return NULL;
19532}
19533
19534/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019535 * Convert list in "arg" into a position and optional file number.
19536 * When "fnump" is NULL there is no file number, only 3 items.
19537 * Note that the column is passed on as-is, the caller may want to decrement
19538 * it to use 1 for the first column.
19539 * Return FAIL when conversion is not possible, doesn't check the position for
19540 * validity.
19541 */
19542 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019543list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019544 typval_T *arg;
19545 pos_T *posp;
19546 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019547 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019548{
19549 list_T *l = arg->vval.v_list;
19550 long i = 0;
19551 long n;
19552
Bram Moolenaar493c1782014-05-28 14:34:46 +020019553 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19554 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019555 if (arg->v_type != VAR_LIST
19556 || l == NULL
19557 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019558 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019559 return FAIL;
19560
19561 if (fnump != NULL)
19562 {
19563 n = list_find_nr(l, i++, NULL); /* fnum */
19564 if (n < 0)
19565 return FAIL;
19566 if (n == 0)
19567 n = curbuf->b_fnum; /* current buffer */
19568 *fnump = n;
19569 }
19570
19571 n = list_find_nr(l, i++, NULL); /* lnum */
19572 if (n < 0)
19573 return FAIL;
19574 posp->lnum = n;
19575
19576 n = list_find_nr(l, i++, NULL); /* col */
19577 if (n < 0)
19578 return FAIL;
19579 posp->col = n;
19580
19581#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019582 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019583 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019584 posp->coladd = 0;
19585 else
19586 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019587#endif
19588
Bram Moolenaar493c1782014-05-28 14:34:46 +020019589 if (curswantp != NULL)
19590 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19591
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019592 return OK;
19593}
19594
19595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 * Get the length of an environment variable name.
19597 * Advance "arg" to the first character after the name.
19598 * Return 0 for error.
19599 */
19600 static int
19601get_env_len(arg)
19602 char_u **arg;
19603{
19604 char_u *p;
19605 int len;
19606
19607 for (p = *arg; vim_isIDc(*p); ++p)
19608 ;
19609 if (p == *arg) /* no name found */
19610 return 0;
19611
19612 len = (int)(p - *arg);
19613 *arg = p;
19614 return len;
19615}
19616
19617/*
19618 * Get the length of the name of a function or internal variable.
19619 * "arg" is advanced to the first non-white character after the name.
19620 * Return 0 if something is wrong.
19621 */
19622 static int
19623get_id_len(arg)
19624 char_u **arg;
19625{
19626 char_u *p;
19627 int len;
19628
19629 /* Find the end of the name. */
19630 for (p = *arg; eval_isnamec(*p); ++p)
19631 ;
19632 if (p == *arg) /* no name found */
19633 return 0;
19634
19635 len = (int)(p - *arg);
19636 *arg = skipwhite(p);
19637
19638 return len;
19639}
19640
19641/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019642 * Get the length of the name of a variable or function.
19643 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019645 * Return -1 if curly braces expansion failed.
19646 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 * If the name contains 'magic' {}'s, expand them and return the
19648 * expanded name in an allocated string via 'alias' - caller must free.
19649 */
19650 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019651get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 char_u **arg;
19653 char_u **alias;
19654 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019655 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656{
19657 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 char_u *p;
19659 char_u *expr_start;
19660 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661
19662 *alias = NULL; /* default to no alias */
19663
19664 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19665 && (*arg)[2] == (int)KE_SNR)
19666 {
19667 /* hard coded <SNR>, already translated */
19668 *arg += 3;
19669 return get_id_len(arg) + 3;
19670 }
19671 len = eval_fname_script(*arg);
19672 if (len > 0)
19673 {
19674 /* literal "<SID>", "s:" or "<SNR>" */
19675 *arg += len;
19676 }
19677
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019679 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019681 p = find_name_end(*arg, &expr_start, &expr_end,
19682 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 if (expr_start != NULL)
19684 {
19685 char_u *temp_string;
19686
19687 if (!evaluate)
19688 {
19689 len += (int)(p - *arg);
19690 *arg = skipwhite(p);
19691 return len;
19692 }
19693
19694 /*
19695 * Include any <SID> etc in the expanded string:
19696 * Thus the -len here.
19697 */
19698 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19699 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019700 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 *alias = temp_string;
19702 *arg = skipwhite(p);
19703 return (int)STRLEN(temp_string);
19704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705
19706 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019707 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 EMSG2(_(e_invexpr2), *arg);
19709
19710 return len;
19711}
19712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713/*
19714 * Find the end of a variable or function name, taking care of magic braces.
19715 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19716 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019717 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 * Return a pointer to just after the name. Equal to "arg" if there is no
19719 * valid name.
19720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019722find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 char_u *arg;
19724 char_u **expr_start;
19725 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019726 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 int mb_nest = 0;
19729 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 char_u *p;
19731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 *expr_start = NULL;
19735 *expr_end = NULL;
19736 }
19737
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019738 /* Quick check for valid starting character. */
19739 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19740 return arg;
19741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 for (p = arg; *p != NUL
19743 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019745 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019747 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019749 if (*p == '\'')
19750 {
19751 /* skip over 'string' to avoid counting [ and ] inside it. */
19752 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19753 ;
19754 if (*p == NUL)
19755 break;
19756 }
19757 else if (*p == '"')
19758 {
19759 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19760 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19761 if (*p == '\\' && p[1] != NUL)
19762 ++p;
19763 if (*p == NUL)
19764 break;
19765 }
19766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 if (*p == '[')
19770 ++br_nest;
19771 else if (*p == ']')
19772 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 if (*p == '{')
19778 {
19779 mb_nest++;
19780 if (expr_start != NULL && *expr_start == NULL)
19781 *expr_start = p;
19782 }
19783 else if (*p == '}')
19784 {
19785 mb_nest--;
19786 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19787 *expr_end = p;
19788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 }
19791
19792 return p;
19793}
19794
19795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019796 * Expands out the 'magic' {}'s in a variable/function name.
19797 * Note that this can call itself recursively, to deal with
19798 * constructs like foo{bar}{baz}{bam}
19799 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19800 * "in_start" ^
19801 * "expr_start" ^
19802 * "expr_end" ^
19803 * "in_end" ^
19804 *
19805 * Returns a new allocated string, which the caller must free.
19806 * Returns NULL for failure.
19807 */
19808 static char_u *
19809make_expanded_name(in_start, expr_start, expr_end, in_end)
19810 char_u *in_start;
19811 char_u *expr_start;
19812 char_u *expr_end;
19813 char_u *in_end;
19814{
19815 char_u c1;
19816 char_u *retval = NULL;
19817 char_u *temp_result;
19818 char_u *nextcmd = NULL;
19819
19820 if (expr_end == NULL || in_end == NULL)
19821 return NULL;
19822 *expr_start = NUL;
19823 *expr_end = NUL;
19824 c1 = *in_end;
19825 *in_end = NUL;
19826
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019827 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019828 if (temp_result != NULL && nextcmd == NULL)
19829 {
19830 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19831 + (in_end - expr_end) + 1));
19832 if (retval != NULL)
19833 {
19834 STRCPY(retval, in_start);
19835 STRCAT(retval, temp_result);
19836 STRCAT(retval, expr_end + 1);
19837 }
19838 }
19839 vim_free(temp_result);
19840
19841 *in_end = c1; /* put char back for error messages */
19842 *expr_start = '{';
19843 *expr_end = '}';
19844
19845 if (retval != NULL)
19846 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019847 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019848 if (expr_start != NULL)
19849 {
19850 /* Further expansion! */
19851 temp_result = make_expanded_name(retval, expr_start,
19852 expr_end, temp_result);
19853 vim_free(retval);
19854 retval = temp_result;
19855 }
19856 }
19857
19858 return retval;
19859}
19860
19861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019863 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 */
19865 static int
19866eval_isnamec(c)
19867 int c;
19868{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019869 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19870}
19871
19872/*
19873 * Return TRUE if character "c" can be used as the first character in a
19874 * variable or function name (excluding '{' and '}').
19875 */
19876 static int
19877eval_isnamec1(c)
19878 int c;
19879{
19880 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881}
19882
19883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 * Set number v: variable to "val".
19885 */
19886 void
19887set_vim_var_nr(idx, val)
19888 int idx;
19889 long val;
19890{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019891 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892}
19893
19894/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019895 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 */
19897 long
19898get_vim_var_nr(idx)
19899 int idx;
19900{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019901 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902}
19903
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019904/*
19905 * Get string v: variable value. Uses a static buffer, can only be used once.
19906 */
19907 char_u *
19908get_vim_var_str(idx)
19909 int idx;
19910{
19911 return get_tv_string(&vimvars[idx].vv_tv);
19912}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019913
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019915 * Get List v: variable value. Caller must take care of reference count when
19916 * needed.
19917 */
19918 list_T *
19919get_vim_var_list(idx)
19920 int idx;
19921{
19922 return vimvars[idx].vv_list;
19923}
19924
19925/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019926 * Set v:char to character "c".
19927 */
19928 void
19929set_vim_var_char(c)
19930 int c;
19931{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019932 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019933
19934#ifdef FEAT_MBYTE
19935 if (has_mbyte)
19936 buf[(*mb_char2bytes)(c, buf)] = NUL;
19937 else
19938#endif
19939 {
19940 buf[0] = c;
19941 buf[1] = NUL;
19942 }
19943 set_vim_var_string(VV_CHAR, buf, -1);
19944}
19945
19946/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019947 * Set v:count to "count" and v:count1 to "count1".
19948 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 */
19950 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019951set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 long count;
19953 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019954 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019956 if (set_prevcount)
19957 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019958 vimvars[VV_COUNT].vv_nr = count;
19959 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960}
19961
19962/*
19963 * Set string v: variable to a copy of "val".
19964 */
19965 void
19966set_vim_var_string(idx, val, len)
19967 int idx;
19968 char_u *val;
19969 int len; /* length of "val" to use or -1 (whole string) */
19970{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019971 /* Need to do this (at least) once, since we can't initialize a union.
19972 * Will always be invoked when "v:progname" is set. */
19973 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19974
Bram Moolenaare9a41262005-01-15 22:18:47 +000019975 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019977 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019979 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019981 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982}
19983
19984/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019985 * Set List v: variable to "val".
19986 */
19987 void
19988set_vim_var_list(idx, val)
19989 int idx;
19990 list_T *val;
19991{
19992 list_unref(vimvars[idx].vv_list);
19993 vimvars[idx].vv_list = val;
19994 if (val != NULL)
19995 ++val->lv_refcount;
19996}
19997
19998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 * Set v:register if needed.
20000 */
20001 void
20002set_reg_var(c)
20003 int c;
20004{
20005 char_u regname;
20006
20007 if (c == 0 || c == ' ')
20008 regname = '"';
20009 else
20010 regname = c;
20011 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020012 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 set_vim_var_string(VV_REG, &regname, 1);
20014}
20015
20016/*
20017 * Get or set v:exception. If "oldval" == NULL, return the current value.
20018 * Otherwise, restore the value to "oldval" and return NULL.
20019 * Must always be called in pairs to save and restore v:exception! Does not
20020 * take care of memory allocations.
20021 */
20022 char_u *
20023v_exception(oldval)
20024 char_u *oldval;
20025{
20026 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020027 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028
Bram Moolenaare9a41262005-01-15 22:18:47 +000020029 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 return NULL;
20031}
20032
20033/*
20034 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20035 * Otherwise, restore the value to "oldval" and return NULL.
20036 * Must always be called in pairs to save and restore v:throwpoint! Does not
20037 * take care of memory allocations.
20038 */
20039 char_u *
20040v_throwpoint(oldval)
20041 char_u *oldval;
20042{
20043 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020044 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
Bram Moolenaare9a41262005-01-15 22:18:47 +000020046 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 return NULL;
20048}
20049
20050#if defined(FEAT_AUTOCMD) || defined(PROTO)
20051/*
20052 * Set v:cmdarg.
20053 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20054 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20055 * Must always be called in pairs!
20056 */
20057 char_u *
20058set_cmdarg(eap, oldarg)
20059 exarg_T *eap;
20060 char_u *oldarg;
20061{
20062 char_u *oldval;
20063 char_u *newval;
20064 unsigned len;
20065
Bram Moolenaare9a41262005-01-15 22:18:47 +000020066 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020067 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020069 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020070 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020071 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 }
20073
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020074 if (eap->force_bin == FORCE_BIN)
20075 len = 6;
20076 else if (eap->force_bin == FORCE_NOBIN)
20077 len = 8;
20078 else
20079 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020080
20081 if (eap->read_edit)
20082 len += 7;
20083
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020084 if (eap->force_ff != 0)
20085 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20086# ifdef FEAT_MBYTE
20087 if (eap->force_enc != 0)
20088 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020089 if (eap->bad_char != 0)
20090 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020091# endif
20092
20093 newval = alloc(len + 1);
20094 if (newval == NULL)
20095 return NULL;
20096
20097 if (eap->force_bin == FORCE_BIN)
20098 sprintf((char *)newval, " ++bin");
20099 else if (eap->force_bin == FORCE_NOBIN)
20100 sprintf((char *)newval, " ++nobin");
20101 else
20102 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020103
20104 if (eap->read_edit)
20105 STRCAT(newval, " ++edit");
20106
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020107 if (eap->force_ff != 0)
20108 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20109 eap->cmd + eap->force_ff);
20110# ifdef FEAT_MBYTE
20111 if (eap->force_enc != 0)
20112 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20113 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020114 if (eap->bad_char == BAD_KEEP)
20115 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20116 else if (eap->bad_char == BAD_DROP)
20117 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20118 else if (eap->bad_char != 0)
20119 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020120# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020121 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020122 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123}
20124#endif
20125
20126/*
20127 * Get the value of internal variable "name".
20128 * Return OK or FAIL.
20129 */
20130 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020131get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 char_u *name;
20133 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020135 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020136 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137{
20138 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020139 typval_T *tv = NULL;
20140 typval_T atv;
20141 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143
20144 /* truncate the name, so that we can use strcmp() */
20145 cc = name[len];
20146 name[len] = NUL;
20147
20148 /*
20149 * Check for "b:changedtick".
20150 */
20151 if (STRCMP(name, "b:changedtick") == 0)
20152 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020153 atv.v_type = VAR_NUMBER;
20154 atv.vval.v_number = curbuf->b_changedtick;
20155 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 }
20157
20158 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 * Check for user-defined variables.
20160 */
20161 else
20162 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020163 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020165 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 }
20167
Bram Moolenaare9a41262005-01-15 22:18:47 +000020168 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020170 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020171 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 ret = FAIL;
20173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020175 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176
20177 name[len] = cc;
20178
20179 return ret;
20180}
20181
20182/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020183 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20184 * Also handle function call with Funcref variable: func(expr)
20185 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20186 */
20187 static int
20188handle_subscript(arg, rettv, evaluate, verbose)
20189 char_u **arg;
20190 typval_T *rettv;
20191 int evaluate; /* do more than finding the end */
20192 int verbose; /* give error messages */
20193{
20194 int ret = OK;
20195 dict_T *selfdict = NULL;
20196 char_u *s;
20197 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020198 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020199
20200 while (ret == OK
20201 && (**arg == '['
20202 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020203 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020204 && !vim_iswhite(*(*arg - 1)))
20205 {
20206 if (**arg == '(')
20207 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020208 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020209 if (evaluate)
20210 {
20211 functv = *rettv;
20212 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020213
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020214 /* Invoke the function. Recursive! */
20215 s = functv.vval.v_string;
20216 }
20217 else
20218 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020219 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020220 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20221 &len, evaluate, selfdict);
20222
20223 /* Clear the funcref afterwards, so that deleting it while
20224 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020225 if (evaluate)
20226 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020227
20228 /* Stop the expression evaluation when immediately aborting on
20229 * error, or when an interrupt occurred or an exception was thrown
20230 * but not caught. */
20231 if (aborting())
20232 {
20233 if (ret == OK)
20234 clear_tv(rettv);
20235 ret = FAIL;
20236 }
20237 dict_unref(selfdict);
20238 selfdict = NULL;
20239 }
20240 else /* **arg == '[' || **arg == '.' */
20241 {
20242 dict_unref(selfdict);
20243 if (rettv->v_type == VAR_DICT)
20244 {
20245 selfdict = rettv->vval.v_dict;
20246 if (selfdict != NULL)
20247 ++selfdict->dv_refcount;
20248 }
20249 else
20250 selfdict = NULL;
20251 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20252 {
20253 clear_tv(rettv);
20254 ret = FAIL;
20255 }
20256 }
20257 }
20258 dict_unref(selfdict);
20259 return ret;
20260}
20261
20262/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020263 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020264 * value).
20265 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020267alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020268{
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020270}
20271
20272/*
20273 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 * The string "s" must have been allocated, it is consumed.
20275 * Return NULL for out of memory, the variable otherwise.
20276 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 char_u *s;
20280{
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020283 rettv = alloc_tv();
20284 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 rettv->v_type = VAR_STRING;
20287 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 }
20289 else
20290 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020291 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
20294/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020295 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020297 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300{
20301 if (varp != NULL)
20302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020303 switch (varp->v_type)
20304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020305 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020306 func_unref(varp->vval.v_string);
20307 /*FALLTHROUGH*/
20308 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020309 vim_free(varp->vval.v_string);
20310 break;
20311 case VAR_LIST:
20312 list_unref(varp->vval.v_list);
20313 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020314 case VAR_DICT:
20315 dict_unref(varp->vval.v_dict);
20316 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020317 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020318#ifdef FEAT_FLOAT
20319 case VAR_FLOAT:
20320#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020321 case VAR_UNKNOWN:
20322 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020323 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020324 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020325 break;
20326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 vim_free(varp);
20328 }
20329}
20330
20331/*
20332 * Free the memory for a variable value and set the value to NULL or 0.
20333 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020334 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020335clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020336 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337{
20338 if (varp != NULL)
20339 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020340 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020342 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020343 func_unref(varp->vval.v_string);
20344 /*FALLTHROUGH*/
20345 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020346 vim_free(varp->vval.v_string);
20347 varp->vval.v_string = NULL;
20348 break;
20349 case VAR_LIST:
20350 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020351 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020352 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020353 case VAR_DICT:
20354 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020355 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020356 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020357 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020358 varp->vval.v_number = 0;
20359 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020360#ifdef FEAT_FLOAT
20361 case VAR_FLOAT:
20362 varp->vval.v_float = 0.0;
20363 break;
20364#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020365 case VAR_UNKNOWN:
20366 break;
20367 default:
20368 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020370 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 }
20372}
20373
20374/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 * Set the value of a variable to NULL without freeing items.
20376 */
20377 static void
20378init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380{
20381 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383}
20384
20385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 * Get the number value of a variable.
20387 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020388 * For incompatible types, return 0.
20389 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20390 * caller of incompatible types: it sets *denote to TRUE if "denote"
20391 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 */
20393 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020394get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020397 int error = FALSE;
20398
20399 return get_tv_number_chk(varp, &error); /* return 0L on error */
20400}
20401
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020402 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020403get_tv_number_chk(varp, denote)
20404 typval_T *varp;
20405 int *denote;
20406{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020407 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020409 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020411 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020412 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020413#ifdef FEAT_FLOAT
20414 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020415 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020416 break;
20417#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020418 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020419 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020420 break;
20421 case VAR_STRING:
20422 if (varp->vval.v_string != NULL)
20423 vim_str2nr(varp->vval.v_string, NULL, NULL,
20424 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020425 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020426 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020427 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020428 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020429 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020430 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020431 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020432 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020433 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020436 if (denote == NULL) /* useful for values that must be unsigned */
20437 n = -1;
20438 else
20439 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020440 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441}
20442
20443/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020444 * Get the lnum from the first argument.
20445 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020446 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 */
20448 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020449get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451{
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 linenr_T lnum;
20454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020455 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 if (lnum == 0) /* no valid number, try using line() */
20457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020458 rettv.v_type = VAR_NUMBER;
20459 f_line(argvars, &rettv);
20460 lnum = rettv.vval.v_number;
20461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 }
20463 return lnum;
20464}
20465
20466/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020467 * Get the lnum from the first argument.
20468 * Also accepts "$", then "buf" is used.
20469 * Returns 0 on error.
20470 */
20471 static linenr_T
20472get_tv_lnum_buf(argvars, buf)
20473 typval_T *argvars;
20474 buf_T *buf;
20475{
20476 if (argvars[0].v_type == VAR_STRING
20477 && argvars[0].vval.v_string != NULL
20478 && argvars[0].vval.v_string[0] == '$'
20479 && buf != NULL)
20480 return buf->b_ml.ml_line_count;
20481 return get_tv_number_chk(&argvars[0], NULL);
20482}
20483
20484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 * Get the string value of a variable.
20486 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020487 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20488 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 * If the String variable has never been set, return an empty string.
20490 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020491 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20492 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 */
20494 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497{
20498 static char_u mybuf[NUMBUFLEN];
20499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020500 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501}
20502
20503 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020504get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020506 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020508 char_u *res = get_tv_string_buf_chk(varp, buf);
20509
20510 return res != NULL ? res : (char_u *)"";
20511}
20512
Bram Moolenaar7d647822014-04-05 21:28:56 +020020513/*
20514 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20515 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020516 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020517get_tv_string_chk(varp)
20518 typval_T *varp;
20519{
20520 static char_u mybuf[NUMBUFLEN];
20521
20522 return get_tv_string_buf_chk(varp, mybuf);
20523}
20524
20525 static char_u *
20526get_tv_string_buf_chk(varp, buf)
20527 typval_T *varp;
20528 char_u *buf;
20529{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020530 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020532 case VAR_NUMBER:
20533 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20534 return buf;
20535 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020536 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020537 break;
20538 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020539 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020540 break;
20541 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020542 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020543 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020544#ifdef FEAT_FLOAT
20545 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020546 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020547 break;
20548#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020549 case VAR_STRING:
20550 if (varp->vval.v_string != NULL)
20551 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020552 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558}
20559
20560/*
20561 * Find variable "name" in the list of variables.
20562 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020563 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020564 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020565 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020567 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020568find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020570 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020571 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020574 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575
Bram Moolenaara7043832005-01-21 11:56:39 +000020576 ht = find_var_ht(name, &varname);
20577 if (htp != NULL)
20578 *htp = ht;
20579 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020581 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582}
20583
20584/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020585 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020586 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020589find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020591 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020592 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020593 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020594{
Bram Moolenaar33570922005-01-25 22:26:29 +000020595 hashitem_T *hi;
20596
20597 if (*varname == NUL)
20598 {
20599 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020600 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020601 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020602 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020603 case 'g': return &globvars_var;
20604 case 'v': return &vimvars_var;
20605 case 'b': return &curbuf->b_bufvar;
20606 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020607#ifdef FEAT_WINDOWS
20608 case 't': return &curtab->tp_winvar;
20609#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020610 case 'l': return current_funccal == NULL
20611 ? NULL : &current_funccal->l_vars_var;
20612 case 'a': return current_funccal == NULL
20613 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 }
20615 return NULL;
20616 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020617
20618 hi = hash_find(ht, varname);
20619 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020620 {
20621 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020622 * worked find the variable again. Don't auto-load a script if it was
20623 * loaded already, otherwise it would be loaded every time when
20624 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020625 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020626 {
20627 /* Note: script_autoload() may make "hi" invalid. It must either
20628 * be obtained again or not used. */
20629 if (!script_autoload(varname, FALSE) || aborting())
20630 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020631 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020632 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020633 if (HASHITEM_EMPTY(hi))
20634 return NULL;
20635 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020636 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020637}
20638
20639/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020640 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020641 * Set "varname" to the start of name without ':'.
20642 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020643 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020644find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 char_u *name;
20646 char_u **varname;
20647{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020648 hashitem_T *hi;
20649
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 if (name[1] != ':')
20651 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020652 /* The name must not start with a colon or #. */
20653 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 return NULL;
20655 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020656
20657 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020658 hi = hash_find(&compat_hashtab, name);
20659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020660 return &compat_hashtab;
20661
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 return &globvarht; /* global variable */
20664 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 }
20666 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020667 if (*name == 'g') /* global variable */
20668 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020669 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20670 */
20671 if (vim_strchr(name + 2, ':') != NULL
20672 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020673 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020675 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020677 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020678#ifdef FEAT_WINDOWS
20679 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020680 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020681#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 if (*name == 'v') /* v: variable */
20683 return &vimvarht;
20684 if (*name == 'a' && current_funccal != NULL) /* function argument */
20685 return &current_funccal->l_avars.dv_hashtab;
20686 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20687 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 if (*name == 's' /* script variable */
20689 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20690 return &SCRIPT_VARS(current_SID);
20691 return NULL;
20692}
20693
20694/*
20695 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020696 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 * Returns NULL when it doesn't exist.
20698 */
20699 char_u *
20700get_var_value(name)
20701 char_u *name;
20702{
Bram Moolenaar33570922005-01-25 22:26:29 +000020703 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020705 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 if (v == NULL)
20707 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020708 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709}
20710
20711/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020712 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 * sourcing this script and when executing functions defined in the script.
20714 */
20715 void
20716new_script_vars(id)
20717 scid_T id;
20718{
Bram Moolenaara7043832005-01-21 11:56:39 +000020719 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020720 hashtab_T *ht;
20721 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020722
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20724 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020725 /* Re-allocating ga_data means that an ht_array pointing to
20726 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020727 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020728 for (i = 1; i <= ga_scripts.ga_len; ++i)
20729 {
20730 ht = &SCRIPT_VARS(i);
20731 if (ht->ht_mask == HT_INIT_SIZE - 1)
20732 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020733 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020734 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020735 }
20736
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 while (ga_scripts.ga_len < id)
20738 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020739 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020740 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020741 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 }
20744 }
20745}
20746
20747/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020748 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20749 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 */
20751 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020752init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 dict_T *dict;
20754 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020755 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756{
Bram Moolenaar33570922005-01-25 22:26:29 +000020757 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020758 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020759 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020760 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020761 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020762 dict_var->di_tv.vval.v_dict = dict;
20763 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020764 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020765 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20766 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767}
20768
20769/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020770 * Unreference a dictionary initialized by init_var_dict().
20771 */
20772 void
20773unref_var_dict(dict)
20774 dict_T *dict;
20775{
20776 /* Now the dict needs to be freed if no one else is using it, go back to
20777 * normal reference counting. */
20778 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20779 dict_unref(dict);
20780}
20781
20782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020784 * Frees all allocated variables and the value they contain.
20785 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 */
20787 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020788vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020789 hashtab_T *ht;
20790{
20791 vars_clear_ext(ht, TRUE);
20792}
20793
20794/*
20795 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20796 */
20797 static void
20798vars_clear_ext(ht, free_val)
20799 hashtab_T *ht;
20800 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801{
Bram Moolenaara7043832005-01-21 11:56:39 +000020802 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020803 hashitem_T *hi;
20804 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805
Bram Moolenaar33570922005-01-25 22:26:29 +000020806 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020807 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020808 for (hi = ht->ht_array; todo > 0; ++hi)
20809 {
20810 if (!HASHITEM_EMPTY(hi))
20811 {
20812 --todo;
20813
Bram Moolenaar33570922005-01-25 22:26:29 +000020814 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020815 * ht_array might change then. hash_clear() takes care of it
20816 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020817 v = HI2DI(hi);
20818 if (free_val)
20819 clear_tv(&v->di_tv);
20820 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20821 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020822 }
20823 }
20824 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020825 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826}
20827
Bram Moolenaara7043832005-01-21 11:56:39 +000020828/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020829 * Delete a variable from hashtab "ht" at item "hi".
20830 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020833delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020834 hashtab_T *ht;
20835 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836{
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020838
20839 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020840 clear_tv(&di->di_tv);
20841 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842}
20843
20844/*
20845 * List the value of one internal variable.
20846 */
20847 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020848list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020849 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020851 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020853 char_u *tofree;
20854 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020855 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020856
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020857 current_copyID += COPYID_INC;
20858 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020859 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020860 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020861 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862}
20863
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020865list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 char_u *prefix;
20867 char_u *name;
20868 int type;
20869 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020870 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871{
Bram Moolenaar31859182007-08-14 20:41:13 +000020872 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20873 msg_start();
20874 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 if (name != NULL) /* "a:" vars don't have a name stored */
20876 msg_puts(name);
20877 msg_putchar(' ');
20878 msg_advance(22);
20879 if (type == VAR_NUMBER)
20880 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020881 else if (type == VAR_FUNC)
20882 msg_putchar('*');
20883 else if (type == VAR_LIST)
20884 {
20885 msg_putchar('[');
20886 if (*string == '[')
20887 ++string;
20888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020889 else if (type == VAR_DICT)
20890 {
20891 msg_putchar('{');
20892 if (*string == '{')
20893 ++string;
20894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 else
20896 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020897
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020899
20900 if (type == VAR_FUNC)
20901 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020902 if (*first)
20903 {
20904 msg_clr_eos();
20905 *first = FALSE;
20906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907}
20908
20909/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 * If the variable already exists, the value is updated.
20912 * Otherwise the variable is created.
20913 */
20914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020917 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020918 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919{
Bram Moolenaar33570922005-01-25 22:26:29 +000020920 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020922 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020924 ht = find_var_ht(name, &varname);
20925 if (ht == NULL || *varname == NUL)
20926 {
20927 EMSG2(_(e_illvar), name);
20928 return;
20929 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020930 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020931
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020932 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20933 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020934
Bram Moolenaar33570922005-01-25 22:26:29 +000020935 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020937 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020938 if (var_check_ro(v->di_flags, name)
20939 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 return;
20941 if (v->di_tv.v_type != tv->v_type
20942 && !((v->di_tv.v_type == VAR_STRING
20943 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020945 || tv->v_type == VAR_NUMBER))
20946#ifdef FEAT_FLOAT
20947 && !((v->di_tv.v_type == VAR_NUMBER
20948 || v->di_tv.v_type == VAR_FLOAT)
20949 && (tv->v_type == VAR_NUMBER
20950 || tv->v_type == VAR_FLOAT))
20951#endif
20952 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020954 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955 return;
20956 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020957
20958 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020959 * Handle setting internal v: variables separately: we don't change
20960 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020961 */
20962 if (ht == &vimvarht)
20963 {
20964 if (v->di_tv.v_type == VAR_STRING)
20965 {
20966 vim_free(v->di_tv.vval.v_string);
20967 if (copy || tv->v_type != VAR_STRING)
20968 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20969 else
20970 {
20971 /* Take over the string to avoid an extra alloc/free. */
20972 v->di_tv.vval.v_string = tv->vval.v_string;
20973 tv->vval.v_string = NULL;
20974 }
20975 }
20976 else if (v->di_tv.v_type != VAR_NUMBER)
20977 EMSG2(_(e_intern2), "set_var()");
20978 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020979 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020981 if (STRCMP(varname, "searchforward") == 0)
20982 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020983#ifdef FEAT_SEARCH_EXTRA
20984 else if (STRCMP(varname, "hlsearch") == 0)
20985 {
20986 no_hlsearch = !v->di_tv.vval.v_number;
20987 redraw_all_later(SOME_VALID);
20988 }
20989#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020990 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020991 return;
20992 }
20993
20994 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 }
20996 else /* add a new variable */
20997 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020998 /* Can't add "v:" variable. */
20999 if (ht == &vimvarht)
21000 {
21001 EMSG2(_(e_illvar), name);
21002 return;
21003 }
21004
Bram Moolenaar92124a32005-06-17 22:03:40 +000021005 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021006 if (!valid_varname(varname))
21007 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021008
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021009 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21010 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021011 if (v == NULL)
21012 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021016 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 return;
21018 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021019 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021021
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021022 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021023 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021024 else
21025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021026 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021027 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021032/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021033 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021034 * Also give an error message.
21035 */
21036 static int
21037var_check_ro(flags, name)
21038 int flags;
21039 char_u *name;
21040{
21041 if (flags & DI_FLAGS_RO)
21042 {
21043 EMSG2(_(e_readonlyvar), name);
21044 return TRUE;
21045 }
21046 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21047 {
21048 EMSG2(_(e_readonlysbx), name);
21049 return TRUE;
21050 }
21051 return FALSE;
21052}
21053
21054/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021055 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21056 * Also give an error message.
21057 */
21058 static int
21059var_check_fixed(flags, name)
21060 int flags;
21061 char_u *name;
21062{
21063 if (flags & DI_FLAGS_FIX)
21064 {
21065 EMSG2(_("E795: Cannot delete variable %s"), name);
21066 return TRUE;
21067 }
21068 return FALSE;
21069}
21070
21071/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021072 * Check if a funcref is assigned to a valid variable name.
21073 * Return TRUE and give an error if not.
21074 */
21075 static int
21076var_check_func_name(name, new_var)
21077 char_u *name; /* points to start of variable name */
21078 int new_var; /* TRUE when creating the variable */
21079{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021080 /* Allow for w: b: s: and t:. */
21081 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021082 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21083 ? name[2] : name[0]))
21084 {
21085 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21086 name);
21087 return TRUE;
21088 }
21089 /* Don't allow hiding a function. When "v" is not NULL we might be
21090 * assigning another function to the same var, the type is checked
21091 * below. */
21092 if (new_var && function_exists(name))
21093 {
21094 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21095 name);
21096 return TRUE;
21097 }
21098 return FALSE;
21099}
21100
21101/*
21102 * Check if a variable name is valid.
21103 * Return FALSE and give an error if not.
21104 */
21105 static int
21106valid_varname(varname)
21107 char_u *varname;
21108{
21109 char_u *p;
21110
21111 for (p = varname; *p != NUL; ++p)
21112 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21113 && *p != AUTOLOAD_CHAR)
21114 {
21115 EMSG2(_(e_illvar), varname);
21116 return FALSE;
21117 }
21118 return TRUE;
21119}
21120
21121/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021122 * Return TRUE if typeval "tv" is set to be locked (immutable).
21123 * Also give an error message, using "name".
21124 */
21125 static int
21126tv_check_lock(lock, name)
21127 int lock;
21128 char_u *name;
21129{
21130 if (lock & VAR_LOCKED)
21131 {
21132 EMSG2(_("E741: Value is locked: %s"),
21133 name == NULL ? (char_u *)_("Unknown") : name);
21134 return TRUE;
21135 }
21136 if (lock & VAR_FIXED)
21137 {
21138 EMSG2(_("E742: Cannot change value of %s"),
21139 name == NULL ? (char_u *)_("Unknown") : name);
21140 return TRUE;
21141 }
21142 return FALSE;
21143}
21144
21145/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021146 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021147 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021148 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021149 * It is OK for "from" and "to" to point to the same item. This is used to
21150 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021152 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021153copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021154 typval_T *from;
21155 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021157 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021158 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021159 switch (from->v_type)
21160 {
21161 case VAR_NUMBER:
21162 to->vval.v_number = from->vval.v_number;
21163 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021164#ifdef FEAT_FLOAT
21165 case VAR_FLOAT:
21166 to->vval.v_float = from->vval.v_float;
21167 break;
21168#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021169 case VAR_STRING:
21170 case VAR_FUNC:
21171 if (from->vval.v_string == NULL)
21172 to->vval.v_string = NULL;
21173 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021175 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021176 if (from->v_type == VAR_FUNC)
21177 func_ref(to->vval.v_string);
21178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021179 break;
21180 case VAR_LIST:
21181 if (from->vval.v_list == NULL)
21182 to->vval.v_list = NULL;
21183 else
21184 {
21185 to->vval.v_list = from->vval.v_list;
21186 ++to->vval.v_list->lv_refcount;
21187 }
21188 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021189 case VAR_DICT:
21190 if (from->vval.v_dict == NULL)
21191 to->vval.v_dict = NULL;
21192 else
21193 {
21194 to->vval.v_dict = from->vval.v_dict;
21195 ++to->vval.v_dict->dv_refcount;
21196 }
21197 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021198 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021200 break;
21201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202}
21203
21204/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021205 * Make a copy of an item.
21206 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021207 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21208 * reference to an already copied list/dict can be used.
21209 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021211 static int
21212item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021213 typval_T *from;
21214 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021215 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021216 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217{
21218 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021219 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021220
Bram Moolenaar33570922005-01-25 22:26:29 +000021221 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021222 {
21223 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021224 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021225 }
21226 ++recurse;
21227
21228 switch (from->v_type)
21229 {
21230 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021231#ifdef FEAT_FLOAT
21232 case VAR_FLOAT:
21233#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021234 case VAR_STRING:
21235 case VAR_FUNC:
21236 copy_tv(from, to);
21237 break;
21238 case VAR_LIST:
21239 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021240 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021241 if (from->vval.v_list == NULL)
21242 to->vval.v_list = NULL;
21243 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21244 {
21245 /* use the copy made earlier */
21246 to->vval.v_list = from->vval.v_list->lv_copylist;
21247 ++to->vval.v_list->lv_refcount;
21248 }
21249 else
21250 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21251 if (to->vval.v_list == NULL)
21252 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021253 break;
21254 case VAR_DICT:
21255 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021256 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021257 if (from->vval.v_dict == NULL)
21258 to->vval.v_dict = NULL;
21259 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21260 {
21261 /* use the copy made earlier */
21262 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21263 ++to->vval.v_dict->dv_refcount;
21264 }
21265 else
21266 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21267 if (to->vval.v_dict == NULL)
21268 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021269 break;
21270 default:
21271 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021272 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021273 }
21274 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021275 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021276}
21277
21278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 * ":echo expr1 ..." print each argument separated with a space, add a
21280 * newline at the end.
21281 * ":echon expr1 ..." print each argument plain.
21282 */
21283 void
21284ex_echo(eap)
21285 exarg_T *eap;
21286{
21287 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021288 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 char_u *p;
21291 int needclr = TRUE;
21292 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021293 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294
21295 if (eap->skip)
21296 ++emsg_skip;
21297 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21298 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021299 /* If eval1() causes an error message the text from the command may
21300 * still need to be cleared. E.g., "echo 22,44". */
21301 need_clr_eos = needclr;
21302
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 {
21306 /*
21307 * Report the invalid expression unless the expression evaluation
21308 * has been cancelled due to an aborting error, an interrupt, or an
21309 * exception.
21310 */
21311 if (!aborting())
21312 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021313 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 break;
21315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021316 need_clr_eos = FALSE;
21317
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 if (!eap->skip)
21319 {
21320 if (atstart)
21321 {
21322 atstart = FALSE;
21323 /* Call msg_start() after eval1(), evaluating the expression
21324 * may cause a message to appear. */
21325 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021326 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021327 /* Mark the saved text as finishing the line, so that what
21328 * follows is displayed on a new line when scrolling back
21329 * at the more prompt. */
21330 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 }
21334 else if (eap->cmdidx == CMD_echo)
21335 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021336 current_copyID += COPYID_INC;
21337 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021338 if (p != NULL)
21339 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021341 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021343 if (*p != TAB && needclr)
21344 {
21345 /* remove any text still there from the command */
21346 msg_clr_eos();
21347 needclr = FALSE;
21348 }
21349 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 }
21351 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021352 {
21353#ifdef FEAT_MBYTE
21354 if (has_mbyte)
21355 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021356 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021357
21358 (void)msg_outtrans_len_attr(p, i, echo_attr);
21359 p += i - 1;
21360 }
21361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021363 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021366 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021368 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 arg = skipwhite(arg);
21370 }
21371 eap->nextcmd = check_nextcmd(arg);
21372
21373 if (eap->skip)
21374 --emsg_skip;
21375 else
21376 {
21377 /* remove text that may still be there from the command */
21378 if (needclr)
21379 msg_clr_eos();
21380 if (eap->cmdidx == CMD_echo)
21381 msg_end();
21382 }
21383}
21384
21385/*
21386 * ":echohl {name}".
21387 */
21388 void
21389ex_echohl(eap)
21390 exarg_T *eap;
21391{
21392 int id;
21393
21394 id = syn_name2id(eap->arg);
21395 if (id == 0)
21396 echo_attr = 0;
21397 else
21398 echo_attr = syn_id2attr(id);
21399}
21400
21401/*
21402 * ":execute expr1 ..." execute the result of an expression.
21403 * ":echomsg expr1 ..." Print a message
21404 * ":echoerr expr1 ..." Print an error
21405 * Each gets spaces around each argument and a newline at the end for
21406 * echo commands
21407 */
21408 void
21409ex_execute(eap)
21410 exarg_T *eap;
21411{
21412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 int ret = OK;
21415 char_u *p;
21416 garray_T ga;
21417 int len;
21418 int save_did_emsg;
21419
21420 ga_init2(&ga, 1, 80);
21421
21422 if (eap->skip)
21423 ++emsg_skip;
21424 while (*arg != NUL && *arg != '|' && *arg != '\n')
21425 {
21426 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021427 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 {
21429 /*
21430 * Report the invalid expression unless the expression evaluation
21431 * has been cancelled due to an aborting error, an interrupt, or an
21432 * exception.
21433 */
21434 if (!aborting())
21435 EMSG2(_(e_invexpr2), p);
21436 ret = FAIL;
21437 break;
21438 }
21439
21440 if (!eap->skip)
21441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021442 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 len = (int)STRLEN(p);
21444 if (ga_grow(&ga, len + 2) == FAIL)
21445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021446 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 ret = FAIL;
21448 break;
21449 }
21450 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 ga.ga_len += len;
21454 }
21455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 arg = skipwhite(arg);
21458 }
21459
21460 if (ret != FAIL && ga.ga_data != NULL)
21461 {
21462 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021465 out_flush();
21466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 else if (eap->cmdidx == CMD_echoerr)
21468 {
21469 /* We don't want to abort following commands, restore did_emsg. */
21470 save_did_emsg = did_emsg;
21471 EMSG((char_u *)ga.ga_data);
21472 if (!force_abort)
21473 did_emsg = save_did_emsg;
21474 }
21475 else if (eap->cmdidx == CMD_execute)
21476 do_cmdline((char_u *)ga.ga_data,
21477 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21478 }
21479
21480 ga_clear(&ga);
21481
21482 if (eap->skip)
21483 --emsg_skip;
21484
21485 eap->nextcmd = check_nextcmd(arg);
21486}
21487
21488/*
21489 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21490 * "arg" points to the "&" or '+' when called, to "option" when returning.
21491 * Returns NULL when no option name found. Otherwise pointer to the char
21492 * after the option name.
21493 */
21494 static char_u *
21495find_option_end(arg, opt_flags)
21496 char_u **arg;
21497 int *opt_flags;
21498{
21499 char_u *p = *arg;
21500
21501 ++p;
21502 if (*p == 'g' && p[1] == ':')
21503 {
21504 *opt_flags = OPT_GLOBAL;
21505 p += 2;
21506 }
21507 else if (*p == 'l' && p[1] == ':')
21508 {
21509 *opt_flags = OPT_LOCAL;
21510 p += 2;
21511 }
21512 else
21513 *opt_flags = 0;
21514
21515 if (!ASCII_ISALPHA(*p))
21516 return NULL;
21517 *arg = p;
21518
21519 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21520 p += 4; /* termcap option */
21521 else
21522 while (ASCII_ISALPHA(*p))
21523 ++p;
21524 return p;
21525}
21526
21527/*
21528 * ":function"
21529 */
21530 void
21531ex_function(eap)
21532 exarg_T *eap;
21533{
21534 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021535 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 int j;
21537 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021539 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 char_u *name = NULL;
21541 char_u *p;
21542 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021543 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 garray_T newargs;
21545 garray_T newlines;
21546 int varargs = FALSE;
21547 int mustend = FALSE;
21548 int flags = 0;
21549 ufunc_T *fp;
21550 int indent;
21551 int nesting;
21552 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 dictitem_T *v;
21554 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021555 static int func_nr = 0; /* number for nameless function */
21556 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021557 hashtab_T *ht;
21558 int todo;
21559 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021560 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561
21562 /*
21563 * ":function" without argument: list functions.
21564 */
21565 if (ends_excmd(*eap->arg))
21566 {
21567 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021568 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021569 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021570 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021571 {
21572 if (!HASHITEM_EMPTY(hi))
21573 {
21574 --todo;
21575 fp = HI2UF(hi);
21576 if (!isdigit(*fp->uf_name))
21577 list_func_head(fp, FALSE);
21578 }
21579 }
21580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 eap->nextcmd = check_nextcmd(eap->arg);
21582 return;
21583 }
21584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021585 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021586 * ":function /pat": list functions matching pattern.
21587 */
21588 if (*eap->arg == '/')
21589 {
21590 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21591 if (!eap->skip)
21592 {
21593 regmatch_T regmatch;
21594
21595 c = *p;
21596 *p = NUL;
21597 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21598 *p = c;
21599 if (regmatch.regprog != NULL)
21600 {
21601 regmatch.rm_ic = p_ic;
21602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021603 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021604 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21605 {
21606 if (!HASHITEM_EMPTY(hi))
21607 {
21608 --todo;
21609 fp = HI2UF(hi);
21610 if (!isdigit(*fp->uf_name)
21611 && vim_regexec(&regmatch, fp->uf_name, 0))
21612 list_func_head(fp, FALSE);
21613 }
21614 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021615 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021616 }
21617 }
21618 if (*p == '/')
21619 ++p;
21620 eap->nextcmd = check_nextcmd(p);
21621 return;
21622 }
21623
21624 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021625 * Get the function name. There are these situations:
21626 * func normal function name
21627 * "name" == func, "fudi.fd_dict" == NULL
21628 * dict.func new dictionary entry
21629 * "name" == NULL, "fudi.fd_dict" set,
21630 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21631 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021632 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21634 * dict.func existing dict entry that's not a Funcref
21635 * "name" == NULL, "fudi.fd_dict" set,
21636 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021637 * s:func script-local function name
21638 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021641 name = trans_function_name(&p, eap->skip, 0, &fudi);
21642 paren = (vim_strchr(p, '(') != NULL);
21643 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 {
21645 /*
21646 * Return on an invalid expression in braces, unless the expression
21647 * evaluation has been cancelled due to an aborting error, an
21648 * interrupt, or an exception.
21649 */
21650 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021652 if (!eap->skip && fudi.fd_newkey != NULL)
21653 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021654 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 else
21658 eap->skip = TRUE;
21659 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021660
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 /* An error in a function call during evaluation of an expression in magic
21662 * braces should not cause the function not to be defined. */
21663 saved_did_emsg = did_emsg;
21664 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665
21666 /*
21667 * ":function func" with only function name: list function.
21668 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021669 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 {
21671 if (!ends_excmd(*skipwhite(p)))
21672 {
21673 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021674 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 }
21676 eap->nextcmd = check_nextcmd(p);
21677 if (eap->nextcmd != NULL)
21678 *p = NUL;
21679 if (!eap->skip && !got_int)
21680 {
21681 fp = find_func(name);
21682 if (fp != NULL)
21683 {
21684 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021685 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021687 if (FUNCLINE(fp, j) == NULL)
21688 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 msg_putchar('\n');
21690 msg_outnum((long)(j + 1));
21691 if (j < 9)
21692 msg_putchar(' ');
21693 if (j < 99)
21694 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021695 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 out_flush(); /* show a line at a time */
21697 ui_breakcheck();
21698 }
21699 if (!got_int)
21700 {
21701 msg_putchar('\n');
21702 msg_puts((char_u *)" endfunction");
21703 }
21704 }
21705 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021706 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 }
21710
21711 /*
21712 * ":function name(arg1, arg2)" Define function.
21713 */
21714 p = skipwhite(p);
21715 if (*p != '(')
21716 {
21717 if (!eap->skip)
21718 {
21719 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 }
21722 /* attempt to continue by skipping some text */
21723 if (vim_strchr(p, '(') != NULL)
21724 p = vim_strchr(p, '(');
21725 }
21726 p = skipwhite(p + 1);
21727
21728 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21729 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21730
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021731 if (!eap->skip)
21732 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021733 /* Check the name of the function. Unless it's a dictionary function
21734 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021735 if (name != NULL)
21736 arg = name;
21737 else
21738 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021739 if (arg != NULL && (fudi.fd_di == NULL
21740 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021741 {
21742 if (*arg == K_SPECIAL)
21743 j = 3;
21744 else
21745 j = 0;
21746 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21747 : eval_isnamec(arg[j])))
21748 ++j;
21749 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021750 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021751 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021752 /* Disallow using the g: dict. */
21753 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21754 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021755 }
21756
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 /*
21758 * Isolate the arguments: "arg1, arg2, ...)"
21759 */
21760 while (*p != ')')
21761 {
21762 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21763 {
21764 varargs = TRUE;
21765 p += 3;
21766 mustend = TRUE;
21767 }
21768 else
21769 {
21770 arg = p;
21771 while (ASCII_ISALNUM(*p) || *p == '_')
21772 ++p;
21773 if (arg == p || isdigit(*arg)
21774 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21775 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21776 {
21777 if (!eap->skip)
21778 EMSG2(_("E125: Illegal argument: %s"), arg);
21779 break;
21780 }
21781 if (ga_grow(&newargs, 1) == FAIL)
21782 goto erret;
21783 c = *p;
21784 *p = NUL;
21785 arg = vim_strsave(arg);
21786 if (arg == NULL)
21787 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021788
21789 /* Check for duplicate argument name. */
21790 for (i = 0; i < newargs.ga_len; ++i)
21791 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21792 {
21793 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021794 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021795 goto erret;
21796 }
21797
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21799 *p = c;
21800 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 if (*p == ',')
21802 ++p;
21803 else
21804 mustend = TRUE;
21805 }
21806 p = skipwhite(p);
21807 if (mustend && *p != ')')
21808 {
21809 if (!eap->skip)
21810 EMSG2(_(e_invarg2), eap->arg);
21811 break;
21812 }
21813 }
21814 ++p; /* skip the ')' */
21815
Bram Moolenaare9a41262005-01-15 22:18:47 +000021816 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 for (;;)
21818 {
21819 p = skipwhite(p);
21820 if (STRNCMP(p, "range", 5) == 0)
21821 {
21822 flags |= FC_RANGE;
21823 p += 5;
21824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021825 else if (STRNCMP(p, "dict", 4) == 0)
21826 {
21827 flags |= FC_DICT;
21828 p += 4;
21829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 else if (STRNCMP(p, "abort", 5) == 0)
21831 {
21832 flags |= FC_ABORT;
21833 p += 5;
21834 }
21835 else
21836 break;
21837 }
21838
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021839 /* When there is a line break use what follows for the function body.
21840 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21841 if (*p == '\n')
21842 line_arg = p + 1;
21843 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 EMSG(_(e_trailing));
21845
21846 /*
21847 * Read the body of the function, until ":endfunction" is found.
21848 */
21849 if (KeyTyped)
21850 {
21851 /* Check if the function already exists, don't let the user type the
21852 * whole function before telling him it doesn't work! For a script we
21853 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021854 if (!eap->skip && !eap->forceit)
21855 {
21856 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21857 EMSG(_(e_funcdict));
21858 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021859 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021862 if (!eap->skip && did_emsg)
21863 goto erret;
21864
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 msg_putchar('\n'); /* don't overwrite the function name */
21866 cmdline_row = msg_row;
21867 }
21868
21869 indent = 2;
21870 nesting = 0;
21871 for (;;)
21872 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021873 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021874 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021875 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021876 saved_wait_return = FALSE;
21877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021879 sourcing_lnum_off = sourcing_lnum;
21880
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021881 if (line_arg != NULL)
21882 {
21883 /* Use eap->arg, split up in parts by line breaks. */
21884 theline = line_arg;
21885 p = vim_strchr(theline, '\n');
21886 if (p == NULL)
21887 line_arg += STRLEN(line_arg);
21888 else
21889 {
21890 *p = NUL;
21891 line_arg = p + 1;
21892 }
21893 }
21894 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 theline = getcmdline(':', 0L, indent);
21896 else
21897 theline = eap->getline(':', eap->cookie, indent);
21898 if (KeyTyped)
21899 lines_left = Rows - 1;
21900 if (theline == NULL)
21901 {
21902 EMSG(_("E126: Missing :endfunction"));
21903 goto erret;
21904 }
21905
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021906 /* Detect line continuation: sourcing_lnum increased more than one. */
21907 if (sourcing_lnum > sourcing_lnum_off + 1)
21908 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21909 else
21910 sourcing_lnum_off = 0;
21911
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 if (skip_until != NULL)
21913 {
21914 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21915 * don't check for ":endfunc". */
21916 if (STRCMP(theline, skip_until) == 0)
21917 {
21918 vim_free(skip_until);
21919 skip_until = NULL;
21920 }
21921 }
21922 else
21923 {
21924 /* skip ':' and blanks*/
21925 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21926 ;
21927
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021928 /* Check for "endfunction". */
21929 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021931 if (line_arg == NULL)
21932 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 break;
21934 }
21935
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021936 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 * at "end". */
21938 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21939 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021940 else if (STRNCMP(p, "if", 2) == 0
21941 || STRNCMP(p, "wh", 2) == 0
21942 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 || STRNCMP(p, "try", 3) == 0)
21944 indent += 2;
21945
21946 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021947 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021949 if (*p == '!')
21950 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 p += eval_fname_script(p);
21952 if (ASCII_ISALPHA(*p))
21953 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021954 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 if (*skipwhite(p) == '(')
21956 {
21957 ++nesting;
21958 indent += 2;
21959 }
21960 }
21961 }
21962
21963 /* Check for ":append" or ":insert". */
21964 p = skip_range(p, NULL);
21965 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21966 || (p[0] == 'i'
21967 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21968 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21969 skip_until = vim_strsave((char_u *)".");
21970
21971 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21972 arg = skipwhite(skiptowhite(p));
21973 if (arg[0] == '<' && arg[1] =='<'
21974 && ((p[0] == 'p' && p[1] == 'y'
21975 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21976 || (p[0] == 'p' && p[1] == 'e'
21977 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21978 || (p[0] == 't' && p[1] == 'c'
21979 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021980 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21981 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21983 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021984 || (p[0] == 'm' && p[1] == 'z'
21985 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 ))
21987 {
21988 /* ":python <<" continues until a dot, like ":append" */
21989 p = skipwhite(arg + 2);
21990 if (*p == NUL)
21991 skip_until = vim_strsave((char_u *)".");
21992 else
21993 skip_until = vim_strsave(p);
21994 }
21995 }
21996
21997 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021998 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021999 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022000 if (line_arg == NULL)
22001 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022003 }
22004
22005 /* Copy the line to newly allocated memory. get_one_sourceline()
22006 * allocates 250 bytes per line, this saves 80% on average. The cost
22007 * is an extra alloc/free. */
22008 p = vim_strsave(theline);
22009 if (p != NULL)
22010 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022011 if (line_arg == NULL)
22012 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022013 theline = p;
22014 }
22015
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022016 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22017
22018 /* Add NULL lines for continuation lines, so that the line count is
22019 * equal to the index in the growarray. */
22020 while (sourcing_lnum_off-- > 0)
22021 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022022
22023 /* Check for end of eap->arg. */
22024 if (line_arg != NULL && *line_arg == NUL)
22025 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 }
22027
22028 /* Don't define the function when skipping commands or when an error was
22029 * detected. */
22030 if (eap->skip || did_emsg)
22031 goto erret;
22032
22033 /*
22034 * If there are no errors, add the function
22035 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022036 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022037 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022038 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022039 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022040 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022041 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022042 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 goto erret;
22044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022046 fp = find_func(name);
22047 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022049 if (!eap->forceit)
22050 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022051 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022052 goto erret;
22053 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022054 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022055 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022056 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022057 name);
22058 goto erret;
22059 }
22060 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022061 ga_clear_strings(&(fp->uf_args));
22062 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 vim_free(name);
22064 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 }
22067 else
22068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022069 char numbuf[20];
22070
22071 fp = NULL;
22072 if (fudi.fd_newkey == NULL && !eap->forceit)
22073 {
22074 EMSG(_(e_funcdict));
22075 goto erret;
22076 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022077 if (fudi.fd_di == NULL)
22078 {
22079 /* Can't add a function to a locked dictionary */
22080 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22081 goto erret;
22082 }
22083 /* Can't change an existing function if it is locked */
22084 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22085 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022086
22087 /* Give the function a sequential number. Can only be used with a
22088 * Funcref! */
22089 vim_free(name);
22090 sprintf(numbuf, "%d", ++func_nr);
22091 name = vim_strsave((char_u *)numbuf);
22092 if (name == NULL)
22093 goto erret;
22094 }
22095
22096 if (fp == NULL)
22097 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022098 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022099 {
22100 int slen, plen;
22101 char_u *scriptname;
22102
22103 /* Check that the autoload name matches the script name. */
22104 j = FAIL;
22105 if (sourcing_name != NULL)
22106 {
22107 scriptname = autoload_name(name);
22108 if (scriptname != NULL)
22109 {
22110 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022111 plen = (int)STRLEN(p);
22112 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022113 if (slen > plen && fnamecmp(p,
22114 sourcing_name + slen - plen) == 0)
22115 j = OK;
22116 vim_free(scriptname);
22117 }
22118 }
22119 if (j == FAIL)
22120 {
22121 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22122 goto erret;
22123 }
22124 }
22125
22126 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 if (fp == NULL)
22128 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022129
22130 if (fudi.fd_dict != NULL)
22131 {
22132 if (fudi.fd_di == NULL)
22133 {
22134 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022135 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022136 if (fudi.fd_di == NULL)
22137 {
22138 vim_free(fp);
22139 goto erret;
22140 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022141 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22142 {
22143 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022144 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022145 goto erret;
22146 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022147 }
22148 else
22149 /* overwrite existing dict entry */
22150 clear_tv(&fudi.fd_di->di_tv);
22151 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022152 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022153 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022154 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022155
22156 /* behave like "dict" was used */
22157 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022158 }
22159
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022161 STRCPY(fp->uf_name, name);
22162 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022164 fp->uf_args = newargs;
22165 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022166#ifdef FEAT_PROFILE
22167 fp->uf_tml_count = NULL;
22168 fp->uf_tml_total = NULL;
22169 fp->uf_tml_self = NULL;
22170 fp->uf_profiling = FALSE;
22171 if (prof_def_func())
22172 func_do_profile(fp);
22173#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022174 fp->uf_varargs = varargs;
22175 fp->uf_flags = flags;
22176 fp->uf_calls = 0;
22177 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022178 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179
22180erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 ga_clear_strings(&newargs);
22182 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183ret_free:
22184 vim_free(skip_until);
22185 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022188 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189}
22190
22191/*
22192 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022193 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022195 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022196 * TFN_INT: internal function name OK
22197 * TFN_QUIET: be quiet
22198 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 * Advances "pp" to just after the function name (if no error).
22200 */
22201 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022202trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 char_u **pp;
22204 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022205 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022206 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022208 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 char_u *start;
22210 char_u *end;
22211 int lead;
22212 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022215
22216 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022219
22220 /* Check for hard coded <SNR>: already translated function ID (from a user
22221 * command). */
22222 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22223 && (*pp)[2] == (int)KE_SNR)
22224 {
22225 *pp += 3;
22226 len = get_id_len(pp) + 3;
22227 return vim_strnsave(start, len);
22228 }
22229
22230 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22231 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022233 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022235
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022236 /* Note that TFN_ flags use the same values as GLV_ flags. */
22237 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022238 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022239 if (end == start)
22240 {
22241 if (!skip)
22242 EMSG(_("E129: Function name required"));
22243 goto theend;
22244 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022245 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022246 {
22247 /*
22248 * Report an invalid expression in braces, unless the expression
22249 * evaluation has been cancelled due to an aborting error, an
22250 * interrupt, or an exception.
22251 */
22252 if (!aborting())
22253 {
22254 if (end != NULL)
22255 EMSG2(_(e_invarg2), start);
22256 }
22257 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022258 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022259 goto theend;
22260 }
22261
22262 if (lv.ll_tv != NULL)
22263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022264 if (fdp != NULL)
22265 {
22266 fdp->fd_dict = lv.ll_dict;
22267 fdp->fd_newkey = lv.ll_newkey;
22268 lv.ll_newkey = NULL;
22269 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022270 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022271 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22272 {
22273 name = vim_strsave(lv.ll_tv->vval.v_string);
22274 *pp = end;
22275 }
22276 else
22277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022278 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22279 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022280 EMSG(_(e_funcref));
22281 else
22282 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022283 name = NULL;
22284 }
22285 goto theend;
22286 }
22287
22288 if (lv.ll_name == NULL)
22289 {
22290 /* Error found, but continue after the function name. */
22291 *pp = end;
22292 goto theend;
22293 }
22294
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022295 /* Check if the name is a Funcref. If so, use the value. */
22296 if (lv.ll_exp_name != NULL)
22297 {
22298 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022299 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022300 if (name == lv.ll_exp_name)
22301 name = NULL;
22302 }
22303 else
22304 {
22305 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022306 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022307 if (name == *pp)
22308 name = NULL;
22309 }
22310 if (name != NULL)
22311 {
22312 name = vim_strsave(name);
22313 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022314 if (STRNCMP(name, "<SNR>", 5) == 0)
22315 {
22316 /* Change "<SNR>" to the byte sequence. */
22317 name[0] = K_SPECIAL;
22318 name[1] = KS_EXTRA;
22319 name[2] = (int)KE_SNR;
22320 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22321 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022322 goto theend;
22323 }
22324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022325 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022327 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022328 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22329 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22330 {
22331 /* When there was "s:" already or the name expanded to get a
22332 * leading "s:" then remove it. */
22333 lv.ll_name += 2;
22334 len -= 2;
22335 lead = 2;
22336 }
22337 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022338 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022339 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022340 /* skip over "s:" and "g:" */
22341 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022342 lv.ll_name += 2;
22343 len = (int)(end - lv.ll_name);
22344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022345
22346 /*
22347 * Copy the function name to allocated memory.
22348 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22349 * Accept <SNR>123_name() outside a script.
22350 */
22351 if (skip)
22352 lead = 0; /* do nothing */
22353 else if (lead > 0)
22354 {
22355 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022356 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22357 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022358 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022359 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022360 if (current_SID <= 0)
22361 {
22362 EMSG(_(e_usingsid));
22363 goto theend;
22364 }
22365 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22366 lead += (int)STRLEN(sid_buf);
22367 }
22368 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022369 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022370 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022371 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022372 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022373 goto theend;
22374 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022375 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022376 {
22377 char_u *cp = vim_strchr(lv.ll_name, ':');
22378
22379 if (cp != NULL && cp < end)
22380 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022381 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022382 goto theend;
22383 }
22384 }
22385
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022386 name = alloc((unsigned)(len + lead + 1));
22387 if (name != NULL)
22388 {
22389 if (lead > 0)
22390 {
22391 name[0] = K_SPECIAL;
22392 name[1] = KS_EXTRA;
22393 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022394 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022395 STRCPY(name + 3, sid_buf);
22396 }
22397 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022398 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022399 }
22400 *pp = end;
22401
22402theend:
22403 clear_lval(&lv);
22404 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405}
22406
22407/*
22408 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22409 * Return 2 if "p" starts with "s:".
22410 * Return 0 otherwise.
22411 */
22412 static int
22413eval_fname_script(p)
22414 char_u *p;
22415{
22416 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22417 || STRNICMP(p + 1, "SNR>", 4) == 0))
22418 return 5;
22419 if (p[0] == 's' && p[1] == ':')
22420 return 2;
22421 return 0;
22422}
22423
22424/*
22425 * Return TRUE if "p" starts with "<SID>" or "s:".
22426 * Only works if eval_fname_script() returned non-zero for "p"!
22427 */
22428 static int
22429eval_fname_sid(p)
22430 char_u *p;
22431{
22432 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22433}
22434
22435/*
22436 * List the head of the function: "name(arg1, arg2)".
22437 */
22438 static void
22439list_func_head(fp, indent)
22440 ufunc_T *fp;
22441 int indent;
22442{
22443 int j;
22444
22445 msg_start();
22446 if (indent)
22447 MSG_PUTS(" ");
22448 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022449 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 {
22451 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022452 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 }
22454 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022455 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022457 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 {
22459 if (j)
22460 MSG_PUTS(", ");
22461 msg_puts(FUNCARG(fp, j));
22462 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022463 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 {
22465 if (j)
22466 MSG_PUTS(", ");
22467 MSG_PUTS("...");
22468 }
22469 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022470 if (fp->uf_flags & FC_ABORT)
22471 MSG_PUTS(" abort");
22472 if (fp->uf_flags & FC_RANGE)
22473 MSG_PUTS(" range");
22474 if (fp->uf_flags & FC_DICT)
22475 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022476 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022477 if (p_verbose > 0)
22478 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479}
22480
22481/*
22482 * Find a function by name, return pointer to it in ufuncs.
22483 * Return NULL for unknown function.
22484 */
22485 static ufunc_T *
22486find_func(name)
22487 char_u *name;
22488{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022489 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022491 hi = hash_find(&func_hashtab, name);
22492 if (!HASHITEM_EMPTY(hi))
22493 return HI2UF(hi);
22494 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495}
22496
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022497#if defined(EXITFREE) || defined(PROTO)
22498 void
22499free_all_functions()
22500{
22501 hashitem_T *hi;
22502
22503 /* Need to start all over every time, because func_free() may change the
22504 * hash table. */
22505 while (func_hashtab.ht_used > 0)
22506 for (hi = func_hashtab.ht_array; ; ++hi)
22507 if (!HASHITEM_EMPTY(hi))
22508 {
22509 func_free(HI2UF(hi));
22510 break;
22511 }
22512}
22513#endif
22514
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022515 int
22516translated_function_exists(name)
22517 char_u *name;
22518{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022519 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022520 return find_internal_func(name) >= 0;
22521 return find_func(name) != NULL;
22522}
22523
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022524/*
22525 * Return TRUE if a function "name" exists.
22526 */
22527 static int
22528function_exists(name)
22529 char_u *name;
22530{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022531 char_u *nm = name;
22532 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533 int n = FALSE;
22534
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022535 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22536 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022537 nm = skipwhite(nm);
22538
22539 /* Only accept "funcname", "funcname ", "funcname (..." and
22540 * "funcname(...", not "funcname!...". */
22541 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022542 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022543 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022544 return n;
22545}
22546
Bram Moolenaara1544c02013-05-30 12:35:52 +020022547 char_u *
22548get_expanded_name(name, check)
22549 char_u *name;
22550 int check;
22551{
22552 char_u *nm = name;
22553 char_u *p;
22554
22555 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22556
22557 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022558 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022559 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022560
Bram Moolenaara1544c02013-05-30 12:35:52 +020022561 vim_free(p);
22562 return NULL;
22563}
22564
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022565/*
22566 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022567 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22568 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022569 */
22570 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022571builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022572 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022573 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022574{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022575 char_u *p;
22576
22577 if (!ASCII_ISLOWER(name[0]))
22578 return FALSE;
22579 p = vim_strchr(name, AUTOLOAD_CHAR);
22580 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022581}
22582
Bram Moolenaar05159a02005-02-26 23:04:13 +000022583#if defined(FEAT_PROFILE) || defined(PROTO)
22584/*
22585 * Start profiling function "fp".
22586 */
22587 static void
22588func_do_profile(fp)
22589 ufunc_T *fp;
22590{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022591 int len = fp->uf_lines.ga_len;
22592
22593 if (len == 0)
22594 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022595 fp->uf_tm_count = 0;
22596 profile_zero(&fp->uf_tm_self);
22597 profile_zero(&fp->uf_tm_total);
22598 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022599 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022600 if (fp->uf_tml_total == NULL)
22601 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022602 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022603 if (fp->uf_tml_self == NULL)
22604 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022605 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022606 fp->uf_tml_idx = -1;
22607 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22608 || fp->uf_tml_self == NULL)
22609 return; /* out of memory */
22610
22611 fp->uf_profiling = TRUE;
22612}
22613
22614/*
22615 * Dump the profiling results for all functions in file "fd".
22616 */
22617 void
22618func_dump_profile(fd)
22619 FILE *fd;
22620{
22621 hashitem_T *hi;
22622 int todo;
22623 ufunc_T *fp;
22624 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022625 ufunc_T **sorttab;
22626 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022628 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022629 if (todo == 0)
22630 return; /* nothing to dump */
22631
Bram Moolenaar73830342005-02-28 22:48:19 +000022632 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22633
Bram Moolenaar05159a02005-02-26 23:04:13 +000022634 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22635 {
22636 if (!HASHITEM_EMPTY(hi))
22637 {
22638 --todo;
22639 fp = HI2UF(hi);
22640 if (fp->uf_profiling)
22641 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022642 if (sorttab != NULL)
22643 sorttab[st_len++] = fp;
22644
Bram Moolenaar05159a02005-02-26 23:04:13 +000022645 if (fp->uf_name[0] == K_SPECIAL)
22646 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22647 else
22648 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22649 if (fp->uf_tm_count == 1)
22650 fprintf(fd, "Called 1 time\n");
22651 else
22652 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22653 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22654 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22655 fprintf(fd, "\n");
22656 fprintf(fd, "count total (s) self (s)\n");
22657
22658 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22659 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022660 if (FUNCLINE(fp, i) == NULL)
22661 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022662 prof_func_line(fd, fp->uf_tml_count[i],
22663 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022664 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22665 }
22666 fprintf(fd, "\n");
22667 }
22668 }
22669 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022670
22671 if (sorttab != NULL && st_len > 0)
22672 {
22673 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22674 prof_total_cmp);
22675 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22676 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22677 prof_self_cmp);
22678 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22679 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022680
22681 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022682}
Bram Moolenaar73830342005-02-28 22:48:19 +000022683
22684 static void
22685prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22686 FILE *fd;
22687 ufunc_T **sorttab;
22688 int st_len;
22689 char *title;
22690 int prefer_self; /* when equal print only self time */
22691{
22692 int i;
22693 ufunc_T *fp;
22694
22695 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22696 fprintf(fd, "count total (s) self (s) function\n");
22697 for (i = 0; i < 20 && i < st_len; ++i)
22698 {
22699 fp = sorttab[i];
22700 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22701 prefer_self);
22702 if (fp->uf_name[0] == K_SPECIAL)
22703 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22704 else
22705 fprintf(fd, " %s()\n", fp->uf_name);
22706 }
22707 fprintf(fd, "\n");
22708}
22709
22710/*
22711 * Print the count and times for one function or function line.
22712 */
22713 static void
22714prof_func_line(fd, count, total, self, prefer_self)
22715 FILE *fd;
22716 int count;
22717 proftime_T *total;
22718 proftime_T *self;
22719 int prefer_self; /* when equal print only self time */
22720{
22721 if (count > 0)
22722 {
22723 fprintf(fd, "%5d ", count);
22724 if (prefer_self && profile_equal(total, self))
22725 fprintf(fd, " ");
22726 else
22727 fprintf(fd, "%s ", profile_msg(total));
22728 if (!prefer_self && profile_equal(total, self))
22729 fprintf(fd, " ");
22730 else
22731 fprintf(fd, "%s ", profile_msg(self));
22732 }
22733 else
22734 fprintf(fd, " ");
22735}
22736
22737/*
22738 * Compare function for total time sorting.
22739 */
22740 static int
22741#ifdef __BORLANDC__
22742_RTLENTRYF
22743#endif
22744prof_total_cmp(s1, s2)
22745 const void *s1;
22746 const void *s2;
22747{
22748 ufunc_T *p1, *p2;
22749
22750 p1 = *(ufunc_T **)s1;
22751 p2 = *(ufunc_T **)s2;
22752 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22753}
22754
22755/*
22756 * Compare function for self time sorting.
22757 */
22758 static int
22759#ifdef __BORLANDC__
22760_RTLENTRYF
22761#endif
22762prof_self_cmp(s1, s2)
22763 const void *s1;
22764 const void *s2;
22765{
22766 ufunc_T *p1, *p2;
22767
22768 p1 = *(ufunc_T **)s1;
22769 p2 = *(ufunc_T **)s2;
22770 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22771}
22772
Bram Moolenaar05159a02005-02-26 23:04:13 +000022773#endif
22774
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022775/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022776 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022777 * Return TRUE if a package was loaded.
22778 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022779 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022780script_autoload(name, reload)
22781 char_u *name;
22782 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022783{
22784 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022785 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022786 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022787 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022788
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022789 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022790 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022791 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022792 return FALSE;
22793
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022794 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022795
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022796 /* Find the name in the list of previously loaded package names. Skip
22797 * "autoload/", it's always the same. */
22798 for (i = 0; i < ga_loaded.ga_len; ++i)
22799 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22800 break;
22801 if (!reload && i < ga_loaded.ga_len)
22802 ret = FALSE; /* was loaded already */
22803 else
22804 {
22805 /* Remember the name if it wasn't loaded already. */
22806 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22807 {
22808 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22809 tofree = NULL;
22810 }
22811
22812 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022813 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022814 ret = TRUE;
22815 }
22816
22817 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022818 return ret;
22819}
22820
22821/*
22822 * Return the autoload script name for a function or variable name.
22823 * Returns NULL when out of memory.
22824 */
22825 static char_u *
22826autoload_name(name)
22827 char_u *name;
22828{
22829 char_u *p;
22830 char_u *scriptname;
22831
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022832 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022833 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22834 if (scriptname == NULL)
22835 return FALSE;
22836 STRCPY(scriptname, "autoload/");
22837 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022838 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022839 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022840 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022841 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022842 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022843}
22844
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22846
22847/*
22848 * Function given to ExpandGeneric() to obtain the list of user defined
22849 * function names.
22850 */
22851 char_u *
22852get_user_func_name(xp, idx)
22853 expand_T *xp;
22854 int idx;
22855{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022856 static long_u done;
22857 static hashitem_T *hi;
22858 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859
22860 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022862 done = 0;
22863 hi = func_hashtab.ht_array;
22864 }
22865 if (done < func_hashtab.ht_used)
22866 {
22867 if (done++ > 0)
22868 ++hi;
22869 while (HASHITEM_EMPTY(hi))
22870 ++hi;
22871 fp = HI2UF(hi);
22872
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022873 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022874 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022875
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022876 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22877 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878
22879 cat_func_name(IObuff, fp);
22880 if (xp->xp_context != EXPAND_USER_FUNC)
22881 {
22882 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022883 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 STRCAT(IObuff, ")");
22885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 return IObuff;
22887 }
22888 return NULL;
22889}
22890
22891#endif /* FEAT_CMDL_COMPL */
22892
22893/*
22894 * Copy the function name of "fp" to buffer "buf".
22895 * "buf" must be able to hold the function name plus three bytes.
22896 * Takes care of script-local function names.
22897 */
22898 static void
22899cat_func_name(buf, fp)
22900 char_u *buf;
22901 ufunc_T *fp;
22902{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 {
22905 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 }
22908 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022909 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910}
22911
22912/*
22913 * ":delfunction {name}"
22914 */
22915 void
22916ex_delfunction(eap)
22917 exarg_T *eap;
22918{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022919 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 char_u *p;
22921 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923
22924 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022925 name = trans_function_name(&p, eap->skip, 0, &fudi);
22926 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022928 {
22929 if (fudi.fd_dict != NULL && !eap->skip)
22930 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 if (!ends_excmd(*skipwhite(p)))
22934 {
22935 vim_free(name);
22936 EMSG(_(e_trailing));
22937 return;
22938 }
22939 eap->nextcmd = check_nextcmd(p);
22940 if (eap->nextcmd != NULL)
22941 *p = NUL;
22942
22943 if (!eap->skip)
22944 fp = find_func(name);
22945 vim_free(name);
22946
22947 if (!eap->skip)
22948 {
22949 if (fp == NULL)
22950 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022951 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 return;
22953 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022954 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 {
22956 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22957 return;
22958 }
22959
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022960 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022962 /* Delete the dict item that refers to the function, it will
22963 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022964 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022966 else
22967 func_free(fp);
22968 }
22969}
22970
22971/*
22972 * Free a function and remove it from the list of functions.
22973 */
22974 static void
22975func_free(fp)
22976 ufunc_T *fp;
22977{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022978 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979
22980 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022981 ga_clear_strings(&(fp->uf_args));
22982 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022983#ifdef FEAT_PROFILE
22984 vim_free(fp->uf_tml_count);
22985 vim_free(fp->uf_tml_total);
22986 vim_free(fp->uf_tml_self);
22987#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022988
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022989 /* remove the function from the function hashtable */
22990 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22991 if (HASHITEM_EMPTY(hi))
22992 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022993 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022994 hash_remove(&func_hashtab, hi);
22995
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022996 vim_free(fp);
22997}
22998
22999/*
23000 * Unreference a Function: decrement the reference count and free it when it
23001 * becomes zero. Only for numbered functions.
23002 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023003 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023004func_unref(name)
23005 char_u *name;
23006{
23007 ufunc_T *fp;
23008
23009 if (name != NULL && isdigit(*name))
23010 {
23011 fp = find_func(name);
23012 if (fp == NULL)
23013 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023014 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023015 {
23016 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023017 * when "uf_calls" becomes zero. */
23018 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023019 func_free(fp);
23020 }
23021 }
23022}
23023
23024/*
23025 * Count a reference to a Function.
23026 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023027 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023028func_ref(name)
23029 char_u *name;
23030{
23031 ufunc_T *fp;
23032
23033 if (name != NULL && isdigit(*name))
23034 {
23035 fp = find_func(name);
23036 if (fp == NULL)
23037 EMSG2(_(e_intern2), "func_ref()");
23038 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023039 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 }
23041}
23042
23043/*
23044 * Call a user function.
23045 */
23046 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023047call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 ufunc_T *fp; /* pointer to function */
23049 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 typval_T *argvars; /* arguments */
23051 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 linenr_T firstline; /* first line of range */
23053 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055{
Bram Moolenaar33570922005-01-25 22:26:29 +000023056 char_u *save_sourcing_name;
23057 linenr_T save_sourcing_lnum;
23058 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023059 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 int save_did_emsg;
23061 static int depth = 0;
23062 dictitem_T *v;
23063 int fixvar_idx = 0; /* index in fixvar[] */
23064 int i;
23065 int ai;
23066 char_u numbuf[NUMBUFLEN];
23067 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023068#ifdef FEAT_PROFILE
23069 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023070 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072
23073 /* If depth of calling is getting too high, don't execute the function */
23074 if (depth >= p_mfd)
23075 {
23076 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023077 rettv->v_type = VAR_NUMBER;
23078 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 return;
23080 }
23081 ++depth;
23082
23083 line_breakcheck(); /* check for CTRL-C hit */
23084
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023085 fc = (funccall_T *)alloc(sizeof(funccall_T));
23086 fc->caller = current_funccal;
23087 current_funccal = fc;
23088 fc->func = fp;
23089 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023090 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023091 fc->linenr = 0;
23092 fc->returned = FALSE;
23093 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023095 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23096 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023099 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23101 * each argument variable and saves a lot of time.
23102 */
23103 /*
23104 * Init l: variables.
23105 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023106 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023107 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023108 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023109 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23110 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023111 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023112 name = v->di_key;
23113 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023114 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023115 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023117 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 v->di_tv.vval.v_dict = selfdict;
23119 ++selfdict->dv_refcount;
23120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023121
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 /*
23123 * Init a: variables.
23124 * Set a:0 to "argcount".
23125 * Set a:000 to a list with room for the "..." arguments.
23126 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023127 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023128 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023129 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023130 /* Use "name" to avoid a warning from some compiler that checks the
23131 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023132 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023133 name = v->di_key;
23134 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023136 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023138 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023139 v->di_tv.vval.v_list = &fc->l_varlist;
23140 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23141 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23142 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023143
23144 /*
23145 * Set a:firstline to "firstline" and a:lastline to "lastline".
23146 * Set a:name to named arguments.
23147 * Set a:N to the "..." arguments.
23148 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023149 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023150 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023151 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 (varnumber_T)lastline);
23153 for (i = 0; i < argcount; ++i)
23154 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023155 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 if (ai < 0)
23157 /* named argument a:name */
23158 name = FUNCARG(fp, i);
23159 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023160 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023161 /* "..." argument a:1, a:2, etc. */
23162 sprintf((char *)numbuf, "%d", ai + 1);
23163 name = numbuf;
23164 }
23165 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23166 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023167 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23169 }
23170 else
23171 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023172 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23173 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023174 if (v == NULL)
23175 break;
23176 v->di_flags = DI_FLAGS_RO;
23177 }
23178 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023179 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023180
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023181 /* Note: the values are copied directly to avoid alloc/free.
23182 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023183 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023184 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023185
23186 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23187 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023188 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23189 fc->l_listitems[ai].li_tv = argvars[i];
23190 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023191 }
23192 }
23193
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 /* Don't redraw while executing the function. */
23195 ++RedrawingDisabled;
23196 save_sourcing_name = sourcing_name;
23197 save_sourcing_lnum = sourcing_lnum;
23198 sourcing_lnum = 1;
23199 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023200 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 if (sourcing_name != NULL)
23202 {
23203 if (save_sourcing_name != NULL
23204 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23205 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23206 else
23207 STRCPY(sourcing_name, "function ");
23208 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23209
23210 if (p_verbose >= 12)
23211 {
23212 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023213 verbose_enter_scroll();
23214
Bram Moolenaar555b2802005-05-19 21:08:39 +000023215 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 if (p_verbose >= 14)
23217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023219 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023220 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023221 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
23223 msg_puts((char_u *)"(");
23224 for (i = 0; i < argcount; ++i)
23225 {
23226 if (i > 0)
23227 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023228 if (argvars[i].v_type == VAR_NUMBER)
23229 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 else
23231 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023232 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23233 if (s != NULL)
23234 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023235 if (vim_strsize(s) > MSG_BUF_CLEN)
23236 {
23237 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23238 s = buf;
23239 }
23240 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023241 vim_free(tofree);
23242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 }
23244 }
23245 msg_puts((char_u *)")");
23246 }
23247 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023248
23249 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 --no_wait_return;
23251 }
23252 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023253#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023254 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023255 {
23256 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23257 func_do_profile(fp);
23258 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023259 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023260 {
23261 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023262 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023263 profile_zero(&fp->uf_tm_children);
23264 }
23265 script_prof_save(&wait_start);
23266 }
23267#endif
23268
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023270 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 save_did_emsg = did_emsg;
23272 did_emsg = FALSE;
23273
23274 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023275 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23277
23278 --RedrawingDisabled;
23279
23280 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023281 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023283 clear_tv(rettv);
23284 rettv->v_type = VAR_NUMBER;
23285 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 }
23287
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023289 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023290 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023291 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023292 profile_end(&call_start);
23293 profile_sub_wait(&wait_start, &call_start);
23294 profile_add(&fp->uf_tm_total, &call_start);
23295 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023296 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023297 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023298 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23299 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023300 }
23301 }
23302#endif
23303
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 /* when being verbose, mention the return value */
23305 if (p_verbose >= 12)
23306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023308 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023311 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023312 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023313 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023314 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023315 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023317 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023318 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023319 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023320 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023321
Bram Moolenaar555b2802005-05-19 21:08:39 +000023322 /* The value may be very long. Skip the middle part, so that we
23323 * have some idea how it starts and ends. smsg() would always
23324 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023325 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023326 if (s != NULL)
23327 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023328 if (vim_strsize(s) > MSG_BUF_CLEN)
23329 {
23330 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23331 s = buf;
23332 }
23333 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023334 vim_free(tofree);
23335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 }
23337 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023338
23339 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 --no_wait_return;
23341 }
23342
23343 vim_free(sourcing_name);
23344 sourcing_name = save_sourcing_name;
23345 sourcing_lnum = save_sourcing_lnum;
23346 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023348 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023349 script_prof_restore(&wait_start);
23350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351
23352 if (p_verbose >= 12 && sourcing_name != NULL)
23353 {
23354 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023355 verbose_enter_scroll();
23356
Bram Moolenaar555b2802005-05-19 21:08:39 +000023357 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023359
23360 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 --no_wait_return;
23362 }
23363
23364 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023365 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023367
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023368 /* If the a:000 list and the l: and a: dicts are not referenced we can
23369 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023370 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23371 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23372 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23373 {
23374 free_funccal(fc, FALSE);
23375 }
23376 else
23377 {
23378 hashitem_T *hi;
23379 listitem_T *li;
23380 int todo;
23381
23382 /* "fc" is still in use. This can happen when returning "a:000" or
23383 * assigning "l:" to a global variable.
23384 * Link "fc" in the list for garbage collection later. */
23385 fc->caller = previous_funccal;
23386 previous_funccal = fc;
23387
23388 /* Make a copy of the a: variables, since we didn't do that above. */
23389 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23390 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23391 {
23392 if (!HASHITEM_EMPTY(hi))
23393 {
23394 --todo;
23395 v = HI2DI(hi);
23396 copy_tv(&v->di_tv, &v->di_tv);
23397 }
23398 }
23399
23400 /* Make a copy of the a:000 items, since we didn't do that above. */
23401 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23402 copy_tv(&li->li_tv, &li->li_tv);
23403 }
23404}
23405
23406/*
23407 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023408 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023409 */
23410 static int
23411can_free_funccal(fc, copyID)
23412 funccall_T *fc;
23413 int copyID;
23414{
23415 return (fc->l_varlist.lv_copyID != copyID
23416 && fc->l_vars.dv_copyID != copyID
23417 && fc->l_avars.dv_copyID != copyID);
23418}
23419
23420/*
23421 * Free "fc" and what it contains.
23422 */
23423 static void
23424free_funccal(fc, free_val)
23425 funccall_T *fc;
23426 int free_val; /* a: vars were allocated */
23427{
23428 listitem_T *li;
23429
23430 /* The a: variables typevals may not have been allocated, only free the
23431 * allocated variables. */
23432 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23433
23434 /* free all l: variables */
23435 vars_clear(&fc->l_vars.dv_hashtab);
23436
23437 /* Free the a:000 variables if they were allocated. */
23438 if (free_val)
23439 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23440 clear_tv(&li->li_tv);
23441
23442 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443}
23444
23445/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023446 * Add a number variable "name" to dict "dp" with value "nr".
23447 */
23448 static void
23449add_nr_var(dp, v, name, nr)
23450 dict_T *dp;
23451 dictitem_T *v;
23452 char *name;
23453 varnumber_T nr;
23454{
23455 STRCPY(v->di_key, name);
23456 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23457 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23458 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023459 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023460 v->di_tv.vval.v_number = nr;
23461}
23462
23463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 * ":return [expr]"
23465 */
23466 void
23467ex_return(eap)
23468 exarg_T *eap;
23469{
23470 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 int returning = FALSE;
23473
23474 if (current_funccal == NULL)
23475 {
23476 EMSG(_("E133: :return not inside a function"));
23477 return;
23478 }
23479
23480 if (eap->skip)
23481 ++emsg_skip;
23482
23483 eap->nextcmd = NULL;
23484 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023485 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 {
23487 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023488 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023490 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 }
23492 /* It's safer to return also on error. */
23493 else if (!eap->skip)
23494 {
23495 /*
23496 * Return unless the expression evaluation has been cancelled due to an
23497 * aborting error, an interrupt, or an exception.
23498 */
23499 if (!aborting())
23500 returning = do_return(eap, FALSE, TRUE, NULL);
23501 }
23502
23503 /* When skipping or the return gets pending, advance to the next command
23504 * in this line (!returning). Otherwise, ignore the rest of the line.
23505 * Following lines will be ignored by get_func_line(). */
23506 if (returning)
23507 eap->nextcmd = NULL;
23508 else if (eap->nextcmd == NULL) /* no argument */
23509 eap->nextcmd = check_nextcmd(arg);
23510
23511 if (eap->skip)
23512 --emsg_skip;
23513}
23514
23515/*
23516 * Return from a function. Possibly makes the return pending. Also called
23517 * for a pending return at the ":endtry" or after returning from an extra
23518 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023519 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023520 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 * FALSE when the return gets pending.
23522 */
23523 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023524do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 exarg_T *eap;
23526 int reanimate;
23527 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023528 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529{
23530 int idx;
23531 struct condstack *cstack = eap->cstack;
23532
23533 if (reanimate)
23534 /* Undo the return. */
23535 current_funccal->returned = FALSE;
23536
23537 /*
23538 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23539 * not in its finally clause (which then is to be executed next) is found.
23540 * In this case, make the ":return" pending for execution at the ":endtry".
23541 * Otherwise, return normally.
23542 */
23543 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23544 if (idx >= 0)
23545 {
23546 cstack->cs_pending[idx] = CSTP_RETURN;
23547
23548 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023549 /* A pending return again gets pending. "rettv" points to an
23550 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023552 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 else
23554 {
23555 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023556 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023558 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023560 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 {
23562 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023563 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023564 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 else
23566 EMSG(_(e_outofmem));
23567 }
23568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023569 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570
23571 if (reanimate)
23572 {
23573 /* The pending return value could be overwritten by a ":return"
23574 * without argument in a finally clause; reset the default
23575 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023576 current_funccal->rettv->v_type = VAR_NUMBER;
23577 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 }
23579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023580 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 }
23582 else
23583 {
23584 current_funccal->returned = TRUE;
23585
23586 /* If the return is carried out now, store the return value. For
23587 * a return immediately after reanimation, the value is already
23588 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023589 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023591 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023592 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023594 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 }
23596 }
23597
23598 return idx < 0;
23599}
23600
23601/*
23602 * Free the variable with a pending return value.
23603 */
23604 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023605discard_pending_return(rettv)
23606 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607{
Bram Moolenaar33570922005-01-25 22:26:29 +000023608 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609}
23610
23611/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023612 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 * is an allocated string. Used by report_pending() for verbose messages.
23614 */
23615 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023616get_return_cmd(rettv)
23617 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023619 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023620 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023621 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023623 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023624 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023625 if (s == NULL)
23626 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023627
23628 STRCPY(IObuff, ":return ");
23629 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23630 if (STRLEN(s) + 8 >= IOSIZE)
23631 STRCPY(IObuff + IOSIZE - 4, "...");
23632 vim_free(tofree);
23633 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634}
23635
23636/*
23637 * Get next function line.
23638 * Called by do_cmdline() to get the next line.
23639 * Returns allocated string, or NULL for end of function.
23640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 char_u *
23642get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023643 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023645 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646{
Bram Moolenaar33570922005-01-25 22:26:29 +000023647 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648 ufunc_T *fp = fcp->func;
23649 char_u *retval;
23650 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651
23652 /* If breakpoints have been added/deleted need to check for it. */
23653 if (fcp->dbg_tick != debug_tick)
23654 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023655 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 sourcing_lnum);
23657 fcp->dbg_tick = debug_tick;
23658 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023659#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023660 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023661 func_line_end(cookie);
23662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663
Bram Moolenaar05159a02005-02-26 23:04:13 +000023664 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023665 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23666 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 retval = NULL;
23668 else
23669 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023670 /* Skip NULL lines (continuation lines). */
23671 while (fcp->linenr < gap->ga_len
23672 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23673 ++fcp->linenr;
23674 if (fcp->linenr >= gap->ga_len)
23675 retval = NULL;
23676 else
23677 {
23678 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23679 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023680#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023681 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023682 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023683#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 }
23686
23687 /* Did we encounter a breakpoint? */
23688 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23689 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023692 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 sourcing_lnum);
23694 fcp->dbg_tick = debug_tick;
23695 }
23696
23697 return retval;
23698}
23699
Bram Moolenaar05159a02005-02-26 23:04:13 +000023700#if defined(FEAT_PROFILE) || defined(PROTO)
23701/*
23702 * Called when starting to read a function line.
23703 * "sourcing_lnum" must be correct!
23704 * When skipping lines it may not actually be executed, but we won't find out
23705 * until later and we need to store the time now.
23706 */
23707 void
23708func_line_start(cookie)
23709 void *cookie;
23710{
23711 funccall_T *fcp = (funccall_T *)cookie;
23712 ufunc_T *fp = fcp->func;
23713
23714 if (fp->uf_profiling && sourcing_lnum >= 1
23715 && sourcing_lnum <= fp->uf_lines.ga_len)
23716 {
23717 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023718 /* Skip continuation lines. */
23719 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23720 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023721 fp->uf_tml_execed = FALSE;
23722 profile_start(&fp->uf_tml_start);
23723 profile_zero(&fp->uf_tml_children);
23724 profile_get_wait(&fp->uf_tml_wait);
23725 }
23726}
23727
23728/*
23729 * Called when actually executing a function line.
23730 */
23731 void
23732func_line_exec(cookie)
23733 void *cookie;
23734{
23735 funccall_T *fcp = (funccall_T *)cookie;
23736 ufunc_T *fp = fcp->func;
23737
23738 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23739 fp->uf_tml_execed = TRUE;
23740}
23741
23742/*
23743 * Called when done with a function line.
23744 */
23745 void
23746func_line_end(cookie)
23747 void *cookie;
23748{
23749 funccall_T *fcp = (funccall_T *)cookie;
23750 ufunc_T *fp = fcp->func;
23751
23752 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23753 {
23754 if (fp->uf_tml_execed)
23755 {
23756 ++fp->uf_tml_count[fp->uf_tml_idx];
23757 profile_end(&fp->uf_tml_start);
23758 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023759 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023760 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23761 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023762 }
23763 fp->uf_tml_idx = -1;
23764 }
23765}
23766#endif
23767
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768/*
23769 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023770 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 */
23772 int
23773func_has_ended(cookie)
23774 void *cookie;
23775{
Bram Moolenaar33570922005-01-25 22:26:29 +000023776 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777
23778 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23779 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023780 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 || fcp->returned);
23782}
23783
23784/*
23785 * return TRUE if cookie indicates a function which "abort"s on errors.
23786 */
23787 int
23788func_has_abort(cookie)
23789 void *cookie;
23790{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023791 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792}
23793
23794#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23795typedef enum
23796{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023797 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23798 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23799 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800} var_flavour_T;
23801
23802static var_flavour_T var_flavour __ARGS((char_u *varname));
23803
23804 static var_flavour_T
23805var_flavour(varname)
23806 char_u *varname;
23807{
23808 char_u *p = varname;
23809
23810 if (ASCII_ISUPPER(*p))
23811 {
23812 while (*(++p))
23813 if (ASCII_ISLOWER(*p))
23814 return VAR_FLAVOUR_SESSION;
23815 return VAR_FLAVOUR_VIMINFO;
23816 }
23817 else
23818 return VAR_FLAVOUR_DEFAULT;
23819}
23820#endif
23821
23822#if defined(FEAT_VIMINFO) || defined(PROTO)
23823/*
23824 * Restore global vars that start with a capital from the viminfo file
23825 */
23826 int
23827read_viminfo_varlist(virp, writing)
23828 vir_T *virp;
23829 int writing;
23830{
23831 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023832 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023833 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834
23835 if (!writing && (find_viminfo_parameter('!') != NULL))
23836 {
23837 tab = vim_strchr(virp->vir_line + 1, '\t');
23838 if (tab != NULL)
23839 {
23840 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023841 switch (*tab)
23842 {
23843 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023844#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023845 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023846#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023847 case 'D': type = VAR_DICT; break;
23848 case 'L': type = VAR_LIST; break;
23849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850
23851 tab = vim_strchr(tab, '\t');
23852 if (tab != NULL)
23853 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023854 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023855 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023856 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023858#ifdef FEAT_FLOAT
23859 else if (type == VAR_FLOAT)
23860 (void)string2float(tab + 1, &tv.vval.v_float);
23861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023863 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023864 if (type == VAR_DICT || type == VAR_LIST)
23865 {
23866 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23867
23868 if (etv == NULL)
23869 /* Failed to parse back the dict or list, use it as a
23870 * string. */
23871 tv.v_type = VAR_STRING;
23872 else
23873 {
23874 vim_free(tv.vval.v_string);
23875 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023876 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023877 }
23878 }
23879
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023880 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023881
23882 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023883 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023884 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23885 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
23887 }
23888 }
23889
23890 return viminfo_readline(virp);
23891}
23892
23893/*
23894 * Write global vars that start with a capital to the viminfo file
23895 */
23896 void
23897write_viminfo_varlist(fp)
23898 FILE *fp;
23899{
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 hashitem_T *hi;
23901 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023902 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023903 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023904 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023905 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023906 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907
23908 if (find_viminfo_parameter('!') == NULL)
23909 return;
23910
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023911 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023912
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023913 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023914 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023916 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023918 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 this_var = HI2DI(hi);
23920 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023921 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023922 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023923 {
23924 case VAR_STRING: s = "STR"; break;
23925 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023926#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023927 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023928#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023929 case VAR_DICT: s = "DIC"; break;
23930 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023931 default: continue;
23932 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023933 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023934 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023935 if (p != NULL)
23936 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023937 vim_free(tofree);
23938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 }
23940 }
23941}
23942#endif
23943
23944#if defined(FEAT_SESSION) || defined(PROTO)
23945 int
23946store_session_globals(fd)
23947 FILE *fd;
23948{
Bram Moolenaar33570922005-01-25 22:26:29 +000023949 hashitem_T *hi;
23950 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023951 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 char_u *p, *t;
23953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023954 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023955 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023957 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023959 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023960 this_var = HI2DI(hi);
23961 if ((this_var->di_tv.v_type == VAR_NUMBER
23962 || this_var->di_tv.v_type == VAR_STRING)
23963 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023964 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023965 /* Escape special characters with a backslash. Turn a LF and
23966 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023967 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023968 (char_u *)"\\\"\n\r");
23969 if (p == NULL) /* out of memory */
23970 break;
23971 for (t = p; *t != NUL; ++t)
23972 if (*t == '\n')
23973 *t = 'n';
23974 else if (*t == '\r')
23975 *t = 'r';
23976 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023977 this_var->di_key,
23978 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23979 : ' ',
23980 p,
23981 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23982 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023983 || put_eol(fd) == FAIL)
23984 {
23985 vim_free(p);
23986 return FAIL;
23987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 vim_free(p);
23989 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023990#ifdef FEAT_FLOAT
23991 else if (this_var->di_tv.v_type == VAR_FLOAT
23992 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23993 {
23994 float_T f = this_var->di_tv.vval.v_float;
23995 int sign = ' ';
23996
23997 if (f < 0)
23998 {
23999 f = -f;
24000 sign = '-';
24001 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024002 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024003 this_var->di_key, sign, f) < 0)
24004 || put_eol(fd) == FAIL)
24005 return FAIL;
24006 }
24007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
24009 }
24010 return OK;
24011}
24012#endif
24013
Bram Moolenaar661b1822005-07-28 22:36:45 +000024014/*
24015 * Display script name where an item was last set.
24016 * Should only be invoked when 'verbose' is non-zero.
24017 */
24018 void
24019last_set_msg(scriptID)
24020 scid_T scriptID;
24021{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024022 char_u *p;
24023
Bram Moolenaar661b1822005-07-28 22:36:45 +000024024 if (scriptID != 0)
24025 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024026 p = home_replace_save(NULL, get_scriptname(scriptID));
24027 if (p != NULL)
24028 {
24029 verbose_enter();
24030 MSG_PUTS(_("\n\tLast set from "));
24031 MSG_PUTS(p);
24032 vim_free(p);
24033 verbose_leave();
24034 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024035 }
24036}
24037
Bram Moolenaard812df62008-11-09 12:46:09 +000024038/*
24039 * List v:oldfiles in a nice way.
24040 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024041 void
24042ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024043 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024044{
24045 list_T *l = vimvars[VV_OLDFILES].vv_list;
24046 listitem_T *li;
24047 int nr = 0;
24048
24049 if (l == NULL)
24050 msg((char_u *)_("No old files"));
24051 else
24052 {
24053 msg_start();
24054 msg_scroll = TRUE;
24055 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24056 {
24057 msg_outnum((long)++nr);
24058 MSG_PUTS(": ");
24059 msg_outtrans(get_tv_string(&li->li_tv));
24060 msg_putchar('\n');
24061 out_flush(); /* output one line at a time */
24062 ui_breakcheck();
24063 }
24064 /* Assume "got_int" was set to truncate the listing. */
24065 got_int = FALSE;
24066
24067#ifdef FEAT_BROWSE_CMD
24068 if (cmdmod.browse)
24069 {
24070 quit_more = FALSE;
24071 nr = prompt_for_number(FALSE);
24072 msg_starthere();
24073 if (nr > 0)
24074 {
24075 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24076 (long)nr);
24077
24078 if (p != NULL)
24079 {
24080 p = expand_env_save(p);
24081 eap->arg = p;
24082 eap->cmdidx = CMD_edit;
24083 cmdmod.browse = FALSE;
24084 do_exedit(eap, NULL);
24085 vim_free(p);
24086 }
24087 }
24088 }
24089#endif
24090 }
24091}
24092
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093#endif /* FEAT_EVAL */
24094
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024096#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097
24098#ifdef WIN3264
24099/*
24100 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24101 */
24102static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24103static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24104static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24105
24106/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024107 * Get the short path (8.3) for the filename in "fnamep".
24108 * Only works for a valid file name.
24109 * When the path gets longer "fnamep" is changed and the allocated buffer
24110 * is put in "bufp".
24111 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24112 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 */
24114 static int
24115get_short_pathname(fnamep, bufp, fnamelen)
24116 char_u **fnamep;
24117 char_u **bufp;
24118 int *fnamelen;
24119{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024120 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 char_u *newbuf;
24122
24123 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 l = GetShortPathName(*fnamep, *fnamep, len);
24125 if (l > len - 1)
24126 {
24127 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024128 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 newbuf = vim_strnsave(*fnamep, l);
24130 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024131 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132
24133 vim_free(*bufp);
24134 *fnamep = *bufp = newbuf;
24135
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024136 /* Really should always succeed, as the buffer is big enough. */
24137 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 }
24139
24140 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024141 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142}
24143
24144/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024145 * Get the short path (8.3) for the filename in "fname". The converted
24146 * path is returned in "bufp".
24147 *
24148 * Some of the directories specified in "fname" may not exist. This function
24149 * will shorten the existing directories at the beginning of the path and then
24150 * append the remaining non-existing path.
24151 *
24152 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024153 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024154 * bufp - Pointer to an allocated buffer for the filename.
24155 * fnamelen - Length of the filename pointed to by fname
24156 *
24157 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 */
24159 static int
24160shortpath_for_invalid_fname(fname, bufp, fnamelen)
24161 char_u **fname;
24162 char_u **bufp;
24163 int *fnamelen;
24164{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024165 char_u *short_fname, *save_fname, *pbuf_unused;
24166 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024168 int old_len, len;
24169 int new_len, sfx_len;
24170 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171
24172 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024173 old_len = *fnamelen;
24174 save_fname = vim_strnsave(*fname, old_len);
24175 pbuf_unused = NULL;
24176 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024178 endp = save_fname + old_len - 1; /* Find the end of the copy */
24179 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024181 /*
24182 * Try shortening the supplied path till it succeeds by removing one
24183 * directory at a time from the tail of the path.
24184 */
24185 len = 0;
24186 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024188 /* go back one path-separator */
24189 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24190 --endp;
24191 if (endp <= save_fname)
24192 break; /* processed the complete path */
24193
24194 /*
24195 * Replace the path separator with a NUL and try to shorten the
24196 * resulting path.
24197 */
24198 ch = *endp;
24199 *endp = 0;
24200 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024201 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024202 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24203 {
24204 retval = FAIL;
24205 goto theend;
24206 }
24207 *endp = ch; /* preserve the string */
24208
24209 if (len > 0)
24210 break; /* successfully shortened the path */
24211
24212 /* failed to shorten the path. Skip the path separator */
24213 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 }
24215
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024216 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024218 /*
24219 * Succeeded in shortening the path. Now concatenate the shortened
24220 * path with the remaining path at the tail.
24221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024223 /* Compute the length of the new path. */
24224 sfx_len = (int)(save_endp - endp) + 1;
24225 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024227 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024229 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024231 /* There is not enough space in the currently allocated string,
24232 * copy it to a buffer big enough. */
24233 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024235 {
24236 retval = FAIL;
24237 goto theend;
24238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 }
24240 else
24241 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024242 /* Transfer short_fname to the main buffer (it's big enough),
24243 * unless get_short_pathname() did its work in-place. */
24244 *fname = *bufp = save_fname;
24245 if (short_fname != save_fname)
24246 vim_strncpy(save_fname, short_fname, len);
24247 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024249
24250 /* concat the not-shortened part of the path */
24251 vim_strncpy(*fname + len, endp, sfx_len);
24252 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024254
24255theend:
24256 vim_free(pbuf_unused);
24257 vim_free(save_fname);
24258
24259 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260}
24261
24262/*
24263 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024264 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 */
24266 static int
24267shortpath_for_partial(fnamep, bufp, fnamelen)
24268 char_u **fnamep;
24269 char_u **bufp;
24270 int *fnamelen;
24271{
24272 int sepcount, len, tflen;
24273 char_u *p;
24274 char_u *pbuf, *tfname;
24275 int hasTilde;
24276
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024277 /* Count up the path separators from the RHS.. so we know which part
24278 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024280 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 if (vim_ispathsep(*p))
24282 ++sepcount;
24283
24284 /* Need full path first (use expand_env() to remove a "~/") */
24285 hasTilde = (**fnamep == '~');
24286 if (hasTilde)
24287 pbuf = tfname = expand_env_save(*fnamep);
24288 else
24289 pbuf = tfname = FullName_save(*fnamep, FALSE);
24290
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024291 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024293 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295
24296 if (len == 0)
24297 {
24298 /* Don't have a valid filename, so shorten the rest of the
24299 * path if we can. This CAN give us invalid 8.3 filenames, but
24300 * there's not a lot of point in guessing what it might be.
24301 */
24302 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024303 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 }
24306
24307 /* Count the paths backward to find the beginning of the desired string. */
24308 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024309 {
24310#ifdef FEAT_MBYTE
24311 if (has_mbyte)
24312 p -= mb_head_off(tfname, p);
24313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 if (vim_ispathsep(*p))
24315 {
24316 if (sepcount == 0 || (hasTilde && sepcount == 1))
24317 break;
24318 else
24319 sepcount --;
24320 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 if (hasTilde)
24323 {
24324 --p;
24325 if (p >= tfname)
24326 *p = '~';
24327 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 }
24330 else
24331 ++p;
24332
24333 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24334 vim_free(*bufp);
24335 *fnamelen = (int)STRLEN(p);
24336 *bufp = pbuf;
24337 *fnamep = p;
24338
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024339 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340}
24341#endif /* WIN3264 */
24342
24343/*
24344 * Adjust a filename, according to a string of modifiers.
24345 * *fnamep must be NUL terminated when called. When returning, the length is
24346 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024347 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 * When there is an error, *fnamep is set to NULL.
24349 */
24350 int
24351modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24352 char_u *src; /* string with modifiers */
24353 int *usedlen; /* characters after src that are used */
24354 char_u **fnamep; /* file name so far */
24355 char_u **bufp; /* buffer for allocated file name or NULL */
24356 int *fnamelen; /* length of fnamep */
24357{
24358 int valid = 0;
24359 char_u *tail;
24360 char_u *s, *p, *pbuf;
24361 char_u dirname[MAXPATHL];
24362 int c;
24363 int has_fullname = 0;
24364#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024365 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 int has_shortname = 0;
24367#endif
24368
24369repeat:
24370 /* ":p" - full path/file_name */
24371 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24372 {
24373 has_fullname = 1;
24374
24375 valid |= VALID_PATH;
24376 *usedlen += 2;
24377
24378 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24379 if ((*fnamep)[0] == '~'
24380#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24381 && ((*fnamep)[1] == '/'
24382# ifdef BACKSLASH_IN_FILENAME
24383 || (*fnamep)[1] == '\\'
24384# endif
24385 || (*fnamep)[1] == NUL)
24386
24387#endif
24388 )
24389 {
24390 *fnamep = expand_env_save(*fnamep);
24391 vim_free(*bufp); /* free any allocated file name */
24392 *bufp = *fnamep;
24393 if (*fnamep == NULL)
24394 return -1;
24395 }
24396
24397 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024398 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 {
24400 if (vim_ispathsep(*p)
24401 && p[1] == '.'
24402 && (p[2] == NUL
24403 || vim_ispathsep(p[2])
24404 || (p[2] == '.'
24405 && (p[3] == NUL || vim_ispathsep(p[3])))))
24406 break;
24407 }
24408
24409 /* FullName_save() is slow, don't use it when not needed. */
24410 if (*p != NUL || !vim_isAbsName(*fnamep))
24411 {
24412 *fnamep = FullName_save(*fnamep, *p != NUL);
24413 vim_free(*bufp); /* free any allocated file name */
24414 *bufp = *fnamep;
24415 if (*fnamep == NULL)
24416 return -1;
24417 }
24418
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024419#ifdef WIN3264
24420# if _WIN32_WINNT >= 0x0500
24421 if (vim_strchr(*fnamep, '~') != NULL)
24422 {
24423 /* Expand 8.3 filename to full path. Needed to make sure the same
24424 * file does not have two different names.
24425 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24426 p = alloc(_MAX_PATH + 1);
24427 if (p != NULL)
24428 {
24429 if (GetLongPathName(*fnamep, p, MAXPATHL))
24430 {
24431 vim_free(*bufp);
24432 *bufp = *fnamep = p;
24433 }
24434 else
24435 vim_free(p);
24436 }
24437 }
24438# endif
24439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 /* Append a path separator to a directory. */
24441 if (mch_isdir(*fnamep))
24442 {
24443 /* Make room for one or two extra characters. */
24444 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24445 vim_free(*bufp); /* free any allocated file name */
24446 *bufp = *fnamep;
24447 if (*fnamep == NULL)
24448 return -1;
24449 add_pathsep(*fnamep);
24450 }
24451 }
24452
24453 /* ":." - path relative to the current directory */
24454 /* ":~" - path relative to the home directory */
24455 /* ":8" - shortname path - postponed till after */
24456 while (src[*usedlen] == ':'
24457 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24458 {
24459 *usedlen += 2;
24460 if (c == '8')
24461 {
24462#ifdef WIN3264
24463 has_shortname = 1; /* Postpone this. */
24464#endif
24465 continue;
24466 }
24467 pbuf = NULL;
24468 /* Need full path first (use expand_env() to remove a "~/") */
24469 if (!has_fullname)
24470 {
24471 if (c == '.' && **fnamep == '~')
24472 p = pbuf = expand_env_save(*fnamep);
24473 else
24474 p = pbuf = FullName_save(*fnamep, FALSE);
24475 }
24476 else
24477 p = *fnamep;
24478
24479 has_fullname = 0;
24480
24481 if (p != NULL)
24482 {
24483 if (c == '.')
24484 {
24485 mch_dirname(dirname, MAXPATHL);
24486 s = shorten_fname(p, dirname);
24487 if (s != NULL)
24488 {
24489 *fnamep = s;
24490 if (pbuf != NULL)
24491 {
24492 vim_free(*bufp); /* free any allocated file name */
24493 *bufp = pbuf;
24494 pbuf = NULL;
24495 }
24496 }
24497 }
24498 else
24499 {
24500 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24501 /* Only replace it when it starts with '~' */
24502 if (*dirname == '~')
24503 {
24504 s = vim_strsave(dirname);
24505 if (s != NULL)
24506 {
24507 *fnamep = s;
24508 vim_free(*bufp);
24509 *bufp = s;
24510 }
24511 }
24512 }
24513 vim_free(pbuf);
24514 }
24515 }
24516
24517 tail = gettail(*fnamep);
24518 *fnamelen = (int)STRLEN(*fnamep);
24519
24520 /* ":h" - head, remove "/file_name", can be repeated */
24521 /* Don't remove the first "/" or "c:\" */
24522 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24523 {
24524 valid |= VALID_HEAD;
24525 *usedlen += 2;
24526 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024527 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024528 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 *fnamelen = (int)(tail - *fnamep);
24530#ifdef VMS
24531 if (*fnamelen > 0)
24532 *fnamelen += 1; /* the path separator is part of the path */
24533#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024534 if (*fnamelen == 0)
24535 {
24536 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24537 p = vim_strsave((char_u *)".");
24538 if (p == NULL)
24539 return -1;
24540 vim_free(*bufp);
24541 *bufp = *fnamep = tail = p;
24542 *fnamelen = 1;
24543 }
24544 else
24545 {
24546 while (tail > s && !after_pathsep(s, tail))
24547 mb_ptr_back(*fnamep, tail);
24548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 }
24550
24551 /* ":8" - shortname */
24552 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24553 {
24554 *usedlen += 2;
24555#ifdef WIN3264
24556 has_shortname = 1;
24557#endif
24558 }
24559
24560#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024561 /*
24562 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 */
24564 if (has_shortname)
24565 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024566 /* Copy the string if it is shortened by :h and when it wasn't copied
24567 * yet, because we are going to change it in place. Avoids changing
24568 * the buffer name for "%:8". */
24569 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 {
24571 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024572 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 return -1;
24574 vim_free(*bufp);
24575 *bufp = *fnamep = p;
24576 }
24577
24578 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024579 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 if (!has_fullname && !vim_isAbsName(*fnamep))
24581 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024582 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 return -1;
24584 }
24585 else
24586 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024587 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588
Bram Moolenaardc935552011-08-17 15:23:23 +020024589 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024590 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024591 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592 return -1;
24593
24594 if (l == 0)
24595 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024596 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024598 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 return -1;
24600 }
24601 *fnamelen = l;
24602 }
24603 }
24604#endif /* WIN3264 */
24605
24606 /* ":t" - tail, just the basename */
24607 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24608 {
24609 *usedlen += 2;
24610 *fnamelen -= (int)(tail - *fnamep);
24611 *fnamep = tail;
24612 }
24613
24614 /* ":e" - extension, can be repeated */
24615 /* ":r" - root, without extension, can be repeated */
24616 while (src[*usedlen] == ':'
24617 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24618 {
24619 /* find a '.' in the tail:
24620 * - for second :e: before the current fname
24621 * - otherwise: The last '.'
24622 */
24623 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24624 s = *fnamep - 2;
24625 else
24626 s = *fnamep + *fnamelen - 1;
24627 for ( ; s > tail; --s)
24628 if (s[0] == '.')
24629 break;
24630 if (src[*usedlen + 1] == 'e') /* :e */
24631 {
24632 if (s > tail)
24633 {
24634 *fnamelen += (int)(*fnamep - (s + 1));
24635 *fnamep = s + 1;
24636#ifdef VMS
24637 /* cut version from the extension */
24638 s = *fnamep + *fnamelen - 1;
24639 for ( ; s > *fnamep; --s)
24640 if (s[0] == ';')
24641 break;
24642 if (s > *fnamep)
24643 *fnamelen = s - *fnamep;
24644#endif
24645 }
24646 else if (*fnamep <= tail)
24647 *fnamelen = 0;
24648 }
24649 else /* :r */
24650 {
24651 if (s > tail) /* remove one extension */
24652 *fnamelen = (int)(s - *fnamep);
24653 }
24654 *usedlen += 2;
24655 }
24656
24657 /* ":s?pat?foo?" - substitute */
24658 /* ":gs?pat?foo?" - global substitute */
24659 if (src[*usedlen] == ':'
24660 && (src[*usedlen + 1] == 's'
24661 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24662 {
24663 char_u *str;
24664 char_u *pat;
24665 char_u *sub;
24666 int sep;
24667 char_u *flags;
24668 int didit = FALSE;
24669
24670 flags = (char_u *)"";
24671 s = src + *usedlen + 2;
24672 if (src[*usedlen + 1] == 'g')
24673 {
24674 flags = (char_u *)"g";
24675 ++s;
24676 }
24677
24678 sep = *s++;
24679 if (sep)
24680 {
24681 /* find end of pattern */
24682 p = vim_strchr(s, sep);
24683 if (p != NULL)
24684 {
24685 pat = vim_strnsave(s, (int)(p - s));
24686 if (pat != NULL)
24687 {
24688 s = p + 1;
24689 /* find end of substitution */
24690 p = vim_strchr(s, sep);
24691 if (p != NULL)
24692 {
24693 sub = vim_strnsave(s, (int)(p - s));
24694 str = vim_strnsave(*fnamep, *fnamelen);
24695 if (sub != NULL && str != NULL)
24696 {
24697 *usedlen = (int)(p + 1 - src);
24698 s = do_string_sub(str, pat, sub, flags);
24699 if (s != NULL)
24700 {
24701 *fnamep = s;
24702 *fnamelen = (int)STRLEN(s);
24703 vim_free(*bufp);
24704 *bufp = s;
24705 didit = TRUE;
24706 }
24707 }
24708 vim_free(sub);
24709 vim_free(str);
24710 }
24711 vim_free(pat);
24712 }
24713 }
24714 /* after using ":s", repeat all the modifiers */
24715 if (didit)
24716 goto repeat;
24717 }
24718 }
24719
Bram Moolenaar26df0922014-02-23 23:39:13 +010024720 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24721 {
24722 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24723 if (p == NULL)
24724 return -1;
24725 vim_free(*bufp);
24726 *bufp = *fnamep = p;
24727 *fnamelen = (int)STRLEN(p);
24728 *usedlen += 2;
24729 }
24730
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731 return valid;
24732}
24733
24734/*
24735 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24736 * "flags" can be "g" to do a global substitute.
24737 * Returns an allocated string, NULL for error.
24738 */
24739 char_u *
24740do_string_sub(str, pat, sub, flags)
24741 char_u *str;
24742 char_u *pat;
24743 char_u *sub;
24744 char_u *flags;
24745{
24746 int sublen;
24747 regmatch_T regmatch;
24748 int i;
24749 int do_all;
24750 char_u *tail;
24751 garray_T ga;
24752 char_u *ret;
24753 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024754 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755
24756 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24757 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024758 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759
24760 ga_init2(&ga, 1, 200);
24761
24762 do_all = (flags[0] == 'g');
24763
24764 regmatch.rm_ic = p_ic;
24765 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24766 if (regmatch.regprog != NULL)
24767 {
24768 tail = str;
24769 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24770 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024771 /* Skip empty match except for first match. */
24772 if (regmatch.startp[0] == regmatch.endp[0])
24773 {
24774 if (zero_width == regmatch.startp[0])
24775 {
24776 /* avoid getting stuck on a match with an empty string */
24777 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24778 ++ga.ga_len;
24779 continue;
24780 }
24781 zero_width = regmatch.startp[0];
24782 }
24783
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 /*
24785 * Get some space for a temporary buffer to do the substitution
24786 * into. It will contain:
24787 * - The text up to where the match is.
24788 * - The substituted text.
24789 * - The text after the match.
24790 */
24791 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24792 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24793 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24794 {
24795 ga_clear(&ga);
24796 break;
24797 }
24798
24799 /* copy the text up to where the match is */
24800 i = (int)(regmatch.startp[0] - tail);
24801 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24802 /* add the substituted text */
24803 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24804 + ga.ga_len + i, TRUE, TRUE, FALSE);
24805 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024806 tail = regmatch.endp[0];
24807 if (*tail == NUL)
24808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 if (!do_all)
24810 break;
24811 }
24812
24813 if (ga.ga_data != NULL)
24814 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24815
Bram Moolenaar473de612013-06-08 18:19:48 +020024816 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 }
24818
24819 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24820 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024821 if (p_cpo == empty_option)
24822 p_cpo = save_cpo;
24823 else
24824 /* Darn, evaluating {sub} expression changed the value. */
24825 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826
24827 return ret;
24828}
24829
24830#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */