blob: be8d4da3e90ad4c698b08e986182111cd45e3e92 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200729static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000731static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000732static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000733static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000734static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000736static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200737#ifdef FEAT_FLOAT
738static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
745static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200748static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200749static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100750static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000811static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200840static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200841static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200843
844#ifdef EBCDIC
845static int compare_func_name __ARGS((const void *s1, const void *s2));
846static void sortFunctions __ARGS(());
847#endif
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
859 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100883 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200921 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100922# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923
924 /* global variables */
925 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000927 /* autoloaded script names */
928 ga_clear_strings(&ga_loaded);
929
Bram Moolenaarcca74132013-09-25 21:00:28 +0200930 /* Script-local variables. First clear all the variables and in a second
931 * loop free the scriptvar_T, because a variable in one script might hold
932 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200936 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001864 expr = skipwhite(argend);
1865 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1866 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001868 /*
1869 * ":let" without "=": list variables
1870 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 if (*arg == '[')
1872 EMSG(_(e_invarg));
1873 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_glob_vars(&first);
1880 list_buf_vars(&first);
1881 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001884#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_script_vars(&first);
1886 list_func_vars(&first);
1887 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 eap->nextcmd = check_nextcmd(arg);
1890 }
1891 else
1892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op[0] = '=';
1894 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1898 op[0] = *expr; /* +=, -= or .= */
1899 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 else
1902 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (eap->skip)
1908 {
1909 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 --emsg_skip;
1912 }
1913 else if (i != FAIL)
1914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 }
1920}
1921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922/*
1923 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1924 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * When "nextchars" is not NULL it points to a string with characters that
1926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1927 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 * Returns OK or FAIL;
1929 */
1930 static int
1931ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1932 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int copy; /* copy values from "tv", don't move */
1935 int semicolon; /* from skip_var_list() */
1936 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938{
1939 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 listitem_T *item;
1943 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944
1945 if (*arg != '[')
1946 {
1947 /*
1948 * ":let var = expr" or ":for var in list"
1949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 return OK;
1953 }
1954
1955 /*
1956 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1957 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 {
1960 EMSG(_(e_listreq));
1961 return FAIL;
1962 }
1963
1964 i = list_len(l);
1965 if (semicolon == 0 && var_count < i)
1966 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001967 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return FAIL;
1969 }
1970 if (var_count - semicolon > i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975
1976 item = l->lv_first;
1977 while (*arg != ']')
1978 {
1979 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 item = item->li_next;
1982 if (arg == NULL)
1983 return FAIL;
1984
1985 arg = skipwhite(arg);
1986 if (*arg == ';')
1987 {
1988 /* Put the rest of the list (may be empty) in the var after ';'.
1989 * Create a new list for this. */
1990 l = list_alloc();
1991 if (l == NULL)
1992 return FAIL;
1993 while (item != NULL)
1994 {
1995 list_append_tv(l, &item->li_tv);
1996 item = item->li_next;
1997 }
1998
1999 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 ltv.vval.v_list = l;
2002 l->lv_refcount = 1;
2003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2005 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 clear_tv(&ltv);
2007 if (arg == NULL)
2008 return FAIL;
2009 break;
2010 }
2011 else if (*arg != ',' && *arg != ']')
2012 {
2013 EMSG2(_(e_intern2), "ex_let_vars()");
2014 return FAIL;
2015 }
2016 }
2017
2018 return OK;
2019}
2020
2021/*
2022 * Skip over assignable variable "var" or list of variables "[var, var]".
2023 * Used for ":let varvar = expr" and ":for varvar in expr".
2024 * For "[var, var]" increment "*var_count" for each variable.
2025 * for "[var, var; var]" set "semicolon".
2026 * Return NULL for an error.
2027 */
2028 static char_u *
2029skip_var_list(arg, var_count, semicolon)
2030 char_u *arg;
2031 int *var_count;
2032 int *semicolon;
2033{
2034 char_u *p, *s;
2035
2036 if (*arg == '[')
2037 {
2038 /* "[var, var]": find the matching ']'. */
2039 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002040 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2043 s = skip_var_one(p);
2044 if (s == p)
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 ++*var_count;
2050
2051 p = skipwhite(s);
2052 if (*p == ']')
2053 break;
2054 else if (*p == ';')
2055 {
2056 if (*semicolon == 1)
2057 {
2058 EMSG(_("Double ; in list of variables"));
2059 return NULL;
2060 }
2061 *semicolon = 1;
2062 }
2063 else if (*p != ',')
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 }
2069 return p + 1;
2070 }
2071 else
2072 return skip_var_one(arg);
2073}
2074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002077 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 static char_u *
2080skip_var_one(arg)
2081 char_u *arg;
2082{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 if (*arg == '@' && arg[1] != NUL)
2084 return arg + 2;
2085 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087}
2088
Bram Moolenaara7043832005-01-21 11:56:39 +00002089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 * List variables for hashtab "ht" with prefix "prefix".
2091 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashitem_T *hi;
2101 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 int todo;
2103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2106 {
2107 if (!HASHITEM_EMPTY(hi))
2108 {
2109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 di = HI2DI(hi);
2111 if (empty || di->di_tv.v_type != VAR_STRING
2112 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 }
2115 }
2116}
2117
2118/*
2119 * List global variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_glob_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List buffer variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_buf_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 char_u numbuf[NUMBUFLEN];
2136
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139
2140 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2142 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
2145/*
2146 * List window variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_win_vars(first)
2150 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154}
2155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156#ifdef FEAT_WINDOWS
2157/*
2158 * List tab page variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_tab_vars(first)
2162 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002164 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166}
2167#endif
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
2170 * List Vim variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_vim_vars(first)
2174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177}
2178
2179/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180 * List script-local variables, if there is a script.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_script_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2188 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
2192 * List function variables, if there is a function.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_func_vars(first)
2196 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197{
2198 if (current_funccal != NULL)
2199 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201}
2202
2203/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * List variables in "arg".
2205 */
2206 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 exarg_T *eap;
2209 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u *name_start;
2216 char_u *arg_subsc;
2217 char_u *tofree;
2218 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 while (!ends_excmd(*arg) && !got_int)
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2226 {
2227 emsg_severe = TRUE;
2228 EMSG(_(e_trailing));
2229 break;
2230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* get_name_len() takes care of expanding curly braces */
2235 name_start = name = arg;
2236 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2237 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* This is mainly to keep test 49 working: when expanding
2240 * curly braces fails overrule the exception error message. */
2241 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 emsg_severe = TRUE;
2244 EMSG2(_(e_invarg2), arg);
2245 break;
2246 }
2247 error = TRUE;
2248 }
2249 else
2250 {
2251 if (tofree != NULL)
2252 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002253 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 else
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* handle d.key, l[idx], f(expr) */
2258 arg_subsc = arg;
2259 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 'g': list_glob_vars(first); break;
2268 case 'b': list_buf_vars(first); break;
2269 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'v': list_vim_vars(first); break;
2274 case 's': list_script_vars(first); break;
2275 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 default:
2277 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
2280 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 {
2282 char_u numbuf[NUMBUFLEN];
2283 char_u *tf;
2284 int c;
2285 char_u *s;
2286
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002287 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 c = *arg;
2289 *arg = NUL;
2290 list_one_var_a((char_u *)"",
2291 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 tv.v_type,
2293 s == NULL ? (char_u *)"" : s,
2294 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 *arg = c;
2296 vim_free(tf);
2297 }
2298 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305
2306 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 return arg;
2310}
2311
2312/*
2313 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2314 * Returns a pointer to the char just after the var name.
2315 * Returns NULL if there is an error.
2316 */
2317 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324{
2325 int c1;
2326 char_u *name;
2327 char_u *p;
2328 char_u *arg_end = NULL;
2329 int len;
2330 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332
2333 /*
2334 * ":let $VAR = expr": Set environment variable.
2335 */
2336 if (*arg == '$')
2337 {
2338 /* Find the end of the name. */
2339 ++arg;
2340 name = arg;
2341 len = get_env_len(&arg);
2342 if (len == 0)
2343 EMSG2(_(e_invarg2), name - 1);
2344 else
2345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 if (op != NULL && (*op == '+' || *op == '-'))
2347 EMSG2(_(e_letwrong), op);
2348 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002351 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 {
2353 c1 = name[len];
2354 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 p = get_tv_string_chk(tv);
2356 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 {
2358 int mustfree = FALSE;
2359 char_u *s = vim_getenv(name, &mustfree);
2360
2361 if (s != NULL)
2362 {
2363 p = tofree = concat_str(s, p);
2364 if (mustfree)
2365 vim_free(s);
2366 }
2367 }
2368 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (STRICMP(name, "HOME") == 0)
2372 init_homedir();
2373 else if (didset_vim && STRICMP(name, "VIM") == 0)
2374 didset_vim = FALSE;
2375 else if (didset_vimruntime
2376 && STRICMP(name, "VIMRUNTIME") == 0)
2377 didset_vimruntime = FALSE;
2378 arg_end = arg;
2379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
2384 }
2385
2386 /*
2387 * ":let &option = expr": Set option value.
2388 * ":let &l:option = expr": Set local option value.
2389 * ":let &g:option = expr": Set global option value.
2390 */
2391 else if (*arg == '&')
2392 {
2393 /* Find the end of the name. */
2394 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002395 if (p == NULL || (endchars != NULL
2396 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 long n;
2401 int opt_type;
2402 long numval;
2403 char_u *stringval = NULL;
2404 char_u *s;
2405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 c1 = *p;
2407 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408
2409 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 s = get_tv_string_chk(tv); /* != NULL if number or string */
2411 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
2413 opt_type = get_option_value(arg, &numval,
2414 &stringval, opt_flags);
2415 if ((opt_type == 1 && *op == '.')
2416 || (opt_type == 0 && *op != '.'))
2417 EMSG2(_(e_letwrong), op);
2418 else
2419 {
2420 if (opt_type == 1) /* number */
2421 {
2422 if (*op == '+')
2423 n = numval + n;
2424 else
2425 n = numval - n;
2426 }
2427 else if (opt_type == 0 && stringval != NULL) /* string */
2428 {
2429 s = concat_str(stringval, s);
2430 vim_free(stringval);
2431 stringval = s;
2432 }
2433 }
2434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (s != NULL)
2436 {
2437 set_option_value(arg, n, s, opt_flags);
2438 arg_end = p;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let @r = expr": Set register contents.
2447 */
2448 else if (*arg == '@')
2449 {
2450 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 if (op != NULL && (*op == '+' || *op == '-'))
2452 EMSG2(_(e_letwrong), op);
2453 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *s;
2460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 p = get_tv_string_chk(tv);
2462 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (s != NULL)
2466 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(s);
2469 }
2470 }
2471 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 arg_end = arg + 1;
2475 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002476 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002488 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501
2502 else
2503 EMSG2(_(e_invarg2), arg);
2504
2505 return arg_end;
2506}
2507
2508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2510 */
2511 static int
2512check_changedtick(arg)
2513 char_u *arg;
2514{
2515 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2516 {
2517 EMSG2(_(e_readonlyvar), arg);
2518 return TRUE;
2519 }
2520 return FALSE;
2521}
2522
2523/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Get an lval: variable, Dict item or List item that can be assigned a value
2525 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2526 * "name.key", "name.key[expr]" etc.
2527 * Indexing only works if "name" is an existing List or Dictionary.
2528 * "name" points to the start of the name.
2529 * If "rettv" is not NULL it points to the value to be assigned.
2530 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2531 * wrong; must end in space or cmd separator.
2532 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533 * flags:
2534 * GLV_QUIET: do not give error messages
2535 * GLV_NO_AUTOLOAD: do not use script autoloading
2536 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002538 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
2541 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
2545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int unlet;
2547 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 char_u *expr_start, *expr_end;
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 dictitem_T *v;
2555 typval_T var1;
2556 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566
2567 if (skip)
2568 {
2569 /* When skipping just find the end of the name. */
2570 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 }
2573
2574 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (expr_start != NULL)
2577 {
2578 /* Don't expand the name when we already know there is an error. */
2579 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2580 && *p != '[' && *p != '.')
2581 {
2582 EMSG(_(e_trailing));
2583 return NULL;
2584 }
2585
2586 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2587 if (lp->ll_exp_name == NULL)
2588 {
2589 /* Report an invalid expression in braces, unless the
2590 * expression evaluation has been cancelled due to an
2591 * aborting error, an interrupt, or an exception. */
2592 if (!aborting() && !quiet)
2593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002594 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 EMSG2(_(e_invarg2), name);
2596 return NULL;
2597 }
2598 }
2599 lp->ll_name = lp->ll_exp_name;
2600 }
2601 else
2602 lp->ll_name = name;
2603
2604 /* Without [idx] or .key we are done. */
2605 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2606 return p;
2607
2608 cc = *p;
2609 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (v == NULL && !quiet)
2612 EMSG2(_(e_undefvar), lp->ll_name);
2613 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 if (v == NULL)
2615 return NULL;
2616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /*
2618 * Loop until no more [idx] or .key is following.
2619 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2624 && !(lp->ll_tv->v_type == VAR_DICT
2625 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E689: Can only index a List or Dictionary"));
2629 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E708: [:] must come last"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 len = -1;
2639 if (*p == '.')
2640 {
2641 key = p + 1;
2642 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2643 ;
2644 if (len == 0)
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_(e_emptykey));
2648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = key + len;
2651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 else
2653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*p == ':')
2657 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 empty1 = FALSE;
2661 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var1) == NULL)
2664 {
2665 /* not a number or string */
2666 clear_tv(&var1);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670
2671 /* Optionally get the second index [ :expr]. */
2672 if (*p == ':')
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (rettv != NULL && (rettv->v_type != VAR_LIST
2683 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = skipwhite(p + 1);
2692 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var2) == NULL)
2704 {
2705 /* not a number or string */
2706 if (!empty1)
2707 clear_tv(&var1);
2708 clear_tv(&var2);
2709 return NULL;
2710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Skip to past ']'. */
2729 ++p;
2730 }
2731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
2734 if (len == -1)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*key == NUL)
2739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_list = NULL;
2747 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 /* When assigning to a scope dictionary check that a function and
2751 * variable name is valid (only variable name unless it is l: or
2752 * g: dictionary). Disallow overwriting a builtin function. */
2753 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 int prevval;
2756 int wrong;
2757
2758 if (len != -1)
2759 {
2760 prevval = key[len];
2761 key[len] = NUL;
2762 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002763 else
2764 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2766 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 || !valid_varname(key);
2769 if (len != -1)
2770 key[len] = prevval;
2771 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 return NULL;
2773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* Can't add "v:" variable. */
2778 if (lp->ll_dict == &vimvardict)
2779 {
2780 EMSG2(_(e_illvar), name);
2781 return NULL;
2782 }
2783
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 p = NULL;
2801 break;
2802 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* existing variable, need to check if it can be changed */
2804 else if (var_check_ro(lp->ll_di->di_flags, name))
2805 return NULL;
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 else
2812 {
2813 /*
2814 * Get the number and item for the only or first index of the List.
2815 */
2816 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
2819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
2822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_list = lp->ll_tv->vval.v_list;
2825 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2826 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (lp->ll_n1 < 0)
2829 {
2830 lp->ll_n1 = 0;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 }
2833 }
2834 if (lp->ll_li == NULL)
2835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
2843 /*
2844 * May need to find the item or absolute index for the second
2845 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 * When no index given: "lp->ll_empty2" is TRUE.
2847 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 {
2858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2866 if (lp->ll_n1 < 0)
2867 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2868 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 {
2870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return p;
2881}
2882
2883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 */
2886 static void
2887clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 vim_free(lp->ll_exp_name);
2891 vim_free(lp->ll_newkey);
2892}
2893
2894/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906{
2907 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 listitem_T *ri;
2909 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 cc = *endp;
2916 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 if (op != NULL && *op != '=')
2918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002923 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 {
2925 if (tv_op(&tv, rettv, op) == OK)
2926 set_var(lp->ll_name, &tv, FALSE);
2927 clear_tv(&tv);
2928 }
2929 }
2930 else
2931 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 else if (tv_check_lock(lp->ll_newkey == NULL
2936 ? lp->ll_tv->v_lock
2937 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2938 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 else if (lp->ll_range)
2940 {
2941 /*
2942 * Assign the List values to the list items.
2943 */
2944 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2948 else
2949 {
2950 clear_tv(&lp->ll_li->li_tv);
2951 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ri = ri->li_next;
2954 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2955 break;
2956 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002959 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 ri = NULL;
2962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_li = lp->ll_li->li_next;
2966 ++lp->ll_n1;
2967 }
2968 if (ri != NULL)
2969 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else if (lp->ll_empty2
2971 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 : lp->ll_n1 != lp->ll_n2)
2973 EMSG(_("E711: List value has not enough items"));
2974 }
2975 else
2976 {
2977 /*
2978 * Assign to a List or Dictionary item.
2979 */
2980 if (lp->ll_newkey != NULL)
2981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
2984 EMSG2(_(e_letwrong), op);
2985 return;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 if (di == NULL)
2991 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2993 {
2994 vim_free(di);
2995 return;
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (op != NULL && *op != '=')
3000 {
3001 tv_op(lp->ll_tv, rettv, op);
3002 return;
3003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /*
3008 * Assign the value to the variable or list item.
3009 */
3010 if (copy)
3011 copy_tv(rettv, lp->ll_tv);
3012 else
3013 {
3014 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003015 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 }
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019}
3020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3023 * Returns OK or FAIL.
3024 */
3025 static int
3026tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T *tv1;
3028 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *op;
3030{
3031 long n;
3032 char_u numbuf[NUMBUFLEN];
3033 char_u *s;
3034
3035 /* Can't do anything with a Funcref or a Dict on the right. */
3036 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3037 {
3038 switch (tv1->v_type)
3039 {
3040 case VAR_DICT:
3041 case VAR_FUNC:
3042 break;
3043
3044 case VAR_LIST:
3045 if (*op != '+' || tv2->v_type != VAR_LIST)
3046 break;
3047 /* List += List */
3048 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3049 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3050 return OK;
3051
3052 case VAR_NUMBER:
3053 case VAR_STRING:
3054 if (tv2->v_type == VAR_LIST)
3055 break;
3056 if (*op == '+' || *op == '-')
3057 {
3058 /* nr += nr or nr -= nr*/
3059 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#ifdef FEAT_FLOAT
3061 if (tv2->v_type == VAR_FLOAT)
3062 {
3063 float_T f = n;
3064
3065 if (*op == '+')
3066 f += tv2->vval.v_float;
3067 else
3068 f -= tv2->vval.v_float;
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_FLOAT;
3071 tv1->vval.v_float = f;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
3075 {
3076 if (*op == '+')
3077 n += get_tv_number(tv2);
3078 else
3079 n -= get_tv_number(tv2);
3080 clear_tv(tv1);
3081 tv1->v_type = VAR_NUMBER;
3082 tv1->vval.v_number = n;
3083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 else
3086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 if (tv2->v_type == VAR_FLOAT)
3088 break;
3089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 /* str .= str */
3091 s = get_tv_string(tv1);
3092 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_STRING;
3095 tv1->vval.v_string = s;
3096 }
3097 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098
3099#ifdef FEAT_FLOAT
3100 case VAR_FLOAT:
3101 {
3102 float_T f;
3103
3104 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3105 && tv2->v_type != VAR_NUMBER
3106 && tv2->v_type != VAR_STRING))
3107 break;
3108 if (tv2->v_type == VAR_FLOAT)
3109 f = tv2->vval.v_float;
3110 else
3111 f = get_tv_number(tv2);
3112 if (*op == '+')
3113 tv1->vval.v_float += f;
3114 else
3115 tv1->vval.v_float -= f;
3116 }
3117 return OK;
3118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 }
3121
3122 EMSG2(_(e_letwrong), op);
3123 return FAIL;
3124}
3125
3126/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 * Add a watcher to a list.
3128 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003129 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
3134 lw->lw_next = l->lv_watch;
3135 l->lv_watch = lw;
3136}
3137
3138/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003139 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 * No warning when it isn't found...
3141 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 lwp = &l->lv_watch;
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 {
3152 if (lw == lwrem)
3153 {
3154 *lwp = lw->lw_next;
3155 break;
3156 }
3157 lwp = &lw->lw_next;
3158 }
3159}
3160
3161/*
3162 * Just before removing an item from a list: advance watchers to the next
3163 * item.
3164 */
3165 static void
3166list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 if (lw->lw_item == item)
3174 lw->lw_item = item->li_next;
3175}
3176
3177/*
3178 * Evaluate the expression used in a ":for var in expr" command.
3179 * "arg" points to "var".
3180 * Set "*errp" to TRUE for an error, FALSE otherwise;
3181 * Return a pointer that holds the info. Null when there is an error.
3182 */
3183 void *
3184eval_for_line(arg, errp, nextcmdp, skip)
3185 char_u *arg;
3186 int *errp;
3187 char_u **nextcmdp;
3188 int skip;
3189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 typval_T tv;
3193 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 *errp = TRUE; /* default: there is an error */
3196
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 if (fi == NULL)
3199 return NULL;
3200
3201 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3202 if (expr == NULL)
3203 return fi;
3204
3205 expr = skipwhite(expr);
3206 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003208 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 return fi;
3210 }
3211
3212 if (skip)
3213 ++emsg_skip;
3214 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3215 {
3216 *errp = FALSE;
3217 if (!skip)
3218 {
3219 l = tv.vval.v_list;
3220 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 clear_tv(&tv);
3224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 else
3226 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003227 /* No need to increment the refcount, it's already set for the
3228 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 fi->fi_list = l;
3230 list_add_watch(l, &fi->fi_lw);
3231 fi->fi_lw.lw_item = l->lv_first;
3232 }
3233 }
3234 }
3235 if (skip)
3236 --emsg_skip;
3237
3238 return fi;
3239}
3240
3241/*
3242 * Use the first item in a ":for" list. Advance to the next.
3243 * Assign the values to the variable (list). "arg" points to the first one.
3244 * Return TRUE when a valid item was found, FALSE when at end of list or
3245 * something wrong.
3246 */
3247 int
3248next_for_item(fi_void, arg)
3249 void *fi_void;
3250 char_u *arg;
3251{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 item = fi->fi_lw.lw_item;
3257 if (item == NULL)
3258 result = FALSE;
3259 else
3260 {
3261 fi->fi_lw.lw_item = item->li_next;
3262 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3263 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3264 }
3265 return result;
3266}
3267
3268/*
3269 * Free the structure used to store info used by ":for".
3270 */
3271 void
3272free_for_info(fi_void)
3273 void *fi_void;
3274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003277 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 list_unref(fi->fi_list);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 vim_free(fi);
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287 void
3288set_context_for_expression(xp, arg, cmdidx)
3289 expand_T *xp;
3290 char_u *arg;
3291 cmdidx_T cmdidx;
3292{
3293 int got_eq = FALSE;
3294 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (cmdidx == CMD_let)
3298 {
3299 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003303 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 {
3305 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 if (vim_iswhite(*p))
3308 break;
3309 }
3310 return;
3311 }
3312 }
3313 else
3314 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3315 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 while ((xp->xp_pattern = vim_strpbrk(arg,
3317 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3318 {
3319 c = *xp->xp_pattern;
3320 if (c == '&')
3321 {
3322 c = xp->xp_pattern[1];
3323 if (c == '&')
3324 {
3325 ++xp->xp_pattern;
3326 xp->xp_context = cmdidx != CMD_let || got_eq
3327 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3328 }
3329 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003332 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3333 xp->xp_pattern += 2;
3334
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (c == '$')
3338 {
3339 /* environment variable */
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 }
3342 else if (c == '=')
3343 {
3344 got_eq = TRUE;
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else if (c == '<'
3348 && xp->xp_context == EXPAND_FUNCTIONS
3349 && vim_strchr(xp->xp_pattern, '(') == NULL)
3350 {
3351 /* Function name can start with "<SNR>" */
3352 break;
3353 }
3354 else if (cmdidx != CMD_let || got_eq)
3355 {
3356 if (c == '"') /* string */
3357 {
3358 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3359 if (c == '\\' && xp->xp_pattern[1] != NUL)
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_NOTHING;
3362 }
3363 else if (c == '\'') /* literal string */
3364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3367 /* skip */ ;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '|')
3371 {
3372 if (xp->xp_pattern[1] == '|')
3373 {
3374 ++xp->xp_pattern;
3375 xp->xp_context = EXPAND_EXPRESSION;
3376 }
3377 else
3378 xp->xp_context = EXPAND_COMMANDS;
3379 }
3380 else
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 /* Doesn't look like something valid, expand as an expression
3385 * anyway. */
3386 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 arg = xp->xp_pattern;
3388 if (*arg != NUL)
3389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3390 /* skip */ ;
3391 }
3392 xp->xp_pattern = arg;
3393}
3394
3395#endif /* FEAT_CMDL_COMPL */
3396
3397/*
3398 * ":1,25call func(arg1, arg2)" function call.
3399 */
3400 void
3401ex_call(eap)
3402 exarg_T *eap;
3403{
3404 char_u *arg = eap->arg;
3405 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 linenr_T lnum;
3411 int doesrange;
3412 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 if (eap->skip)
3416 {
3417 /* trans_function_name() doesn't work well when skipping, use eval0()
3418 * instead to skip to any following command, e.g. for:
3419 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003420 ++emsg_skip;
3421 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3422 clear_tv(&rettv);
3423 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003424 return;
3425 }
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003428 if (fudi.fd_newkey != NULL)
3429 {
3430 /* Still need to give an error message for missing key. */
3431 EMSG2(_(e_dictkey), fudi.fd_newkey);
3432 vim_free(fudi.fd_newkey);
3433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 if (tofree == NULL)
3435 return;
3436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003437 /* Increase refcount on dictionary, it could get deleted when evaluating
3438 * the arguments. */
3439 if (fudi.fd_dict != NULL)
3440 ++fudi.fd_dict->dv_refcount;
3441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003444 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar532c7802005-01-27 14:44:31 +00003446 /* Skip white space to allow ":call func ()". Not good, but required for
3447 * backward compatibility. */
3448 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (*startarg != '(')
3452 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003453 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 goto end;
3455 }
3456
3457 /*
3458 * When skipping, evaluate the function once, to find the end of the
3459 * arguments.
3460 * When the function takes a range, this is discovered after the first
3461 * call, and the loop is broken.
3462 */
3463 if (eap->skip)
3464 {
3465 ++emsg_skip;
3466 lnum = eap->line2; /* do it once, also with an invalid range */
3467 }
3468 else
3469 lnum = eap->line1;
3470 for ( ; lnum <= eap->line2; ++lnum)
3471 {
3472 if (!eap->skip && eap->addr_count > 0)
3473 {
3474 curwin->w_cursor.lnum = lnum;
3475 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003476#ifdef FEAT_VIRTUALEDIT
3477 curwin->w_cursor.coladd = 0;
3478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 eap->line1, eap->line2, &doesrange,
3483 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 failed = TRUE;
3486 break;
3487 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
3489 /* Handle a function returning a Funcref, Dictionary or List. */
3490 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3491 {
3492 failed = TRUE;
3493 break;
3494 }
3495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (doesrange || eap->skip)
3498 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (aborting())
3505 break;
3506 }
3507 if (eap->skip)
3508 --emsg_skip;
3509
3510 if (!failed)
3511 {
3512 /* Check for trailing illegal characters and a following command. */
3513 if (!ends_excmd(*arg))
3514 {
3515 emsg_severe = TRUE;
3516 EMSG(_(e_trailing));
3517 }
3518 else
3519 eap->nextcmd = check_nextcmd(arg);
3520 }
3521
3522end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003524 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * ":unlet[!] var1 ... " command.
3529 */
3530 void
3531ex_unlet(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 ex_unletlock(eap, eap->arg, 0);
3535}
3536
3537/*
3538 * ":lockvar" and ":unlockvar" commands
3539 */
3540 void
3541ex_lockvar(eap)
3542 exarg_T *eap;
3543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 int deep = 2;
3546
3547 if (eap->forceit)
3548 deep = -1;
3549 else if (vim_isdigit(*arg))
3550 {
3551 deep = getdigits(&arg);
3552 arg = skipwhite(arg);
3553 }
3554
3555 ex_unletlock(eap, arg, deep);
3556}
3557
3558/*
3559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3560 */
3561 static void
3562ex_unletlock(eap, argstart, deep)
3563 exarg_T *eap;
3564 char_u *argstart;
3565 int deep;
3566{
3567 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 do
3573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003575 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003576 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 if (lv.ll_name == NULL)
3578 error = TRUE; /* error but continue parsing */
3579 if (name_end == NULL || (!vim_iswhite(*name_end)
3580 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (name_end != NULL)
3583 {
3584 emsg_severe = TRUE;
3585 EMSG(_(e_trailing));
3586 }
3587 if (!(eap->skip || error))
3588 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591
3592 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 {
3594 if (eap->cmdidx == CMD_unlet)
3595 {
3596 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3597 error = TRUE;
3598 }
3599 else
3600 {
3601 if (do_lock_var(&lv, name_end, deep,
3602 eap->cmdidx == CMD_lockvar) == FAIL)
3603 error = TRUE;
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (!eap->skip)
3608 clear_lval(&lv);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(name_end);
3611 } while (!ends_excmd(*arg));
3612
3613 eap->nextcmd = check_nextcmd(arg);
3614}
3615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 int forceit;
3621{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 int ret = OK;
3623 int cc;
3624
3625 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 cc = *name_end;
3628 *name_end = NUL;
3629
3630 /* Normal name or expanded name. */
3631 if (check_changedtick(lp->ll_name))
3632 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3638 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else if (lp->ll_range)
3640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642
3643 /* Delete a range of List items. */
3644 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 li = lp->ll_li->li_next;
3647 listitem_remove(lp->ll_list, lp->ll_li);
3648 lp->ll_li = li;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else
3653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a List item. */
3656 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003659 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 }
3661
3662 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663}
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hashtab_T *ht;
3675 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003677 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 ht = find_var_ht(name, &varname);
3680 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = hash_find(ht, varname);
3683 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003685 di = HI2DI(hi);
3686 if (var_check_fixed(di->di_flags, name)
3687 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 return FAIL;
3689 delete_var(ht, hi);
3690 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 if (forceit)
3694 return OK;
3695 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697}
3698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699/*
3700 * Lock or unlock variable indicated by "lp".
3701 * "deep" is the levels to go (-1 for unlimited);
3702 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3703 */
3704 static int
3705do_lock_var(lp, name_end, deep, lock)
3706 lval_T *lp;
3707 char_u *name_end;
3708 int deep;
3709 int lock;
3710{
3711 int ret = OK;
3712 int cc;
3713 dictitem_T *di;
3714
3715 if (deep == 0) /* nothing to do */
3716 return OK;
3717
3718 if (lp->ll_tv == NULL)
3719 {
3720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003728 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 if (di == NULL)
3730 ret = FAIL;
3731 else
3732 {
3733 if (lock)
3734 di->di_flags |= DI_FLAGS_LOCK;
3735 else
3736 di->di_flags &= ~DI_FLAGS_LOCK;
3737 item_lock(&di->di_tv, deep, lock);
3738 }
3739 }
3740 *name_end = cc;
3741 }
3742 else if (lp->ll_range)
3743 {
3744 listitem_T *li = lp->ll_li;
3745
3746 /* (un)lock a range of List items. */
3747 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3748 {
3749 item_lock(&li->li_tv, deep, lock);
3750 li = li->li_next;
3751 ++lp->ll_n1;
3752 }
3753 }
3754 else if (lp->ll_list != NULL)
3755 /* (un)lock a List item. */
3756 item_lock(&lp->ll_li->li_tv, deep, lock);
3757 else
3758 /* un(lock) a Dictionary item. */
3759 item_lock(&lp->ll_di->di_tv, deep, lock);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Lock or unlock an item. "deep" is nr of levels to go.
3766 */
3767 static void
3768item_lock(tv, deep, lock)
3769 typval_T *tv;
3770 int deep;
3771 int lock;
3772{
3773 static int recurse = 0;
3774 list_T *l;
3775 listitem_T *li;
3776 dict_T *d;
3777 hashitem_T *hi;
3778 int todo;
3779
3780 if (recurse >= DICT_MAXNEST)
3781 {
3782 EMSG(_("E743: variable nested too deep for (un)lock"));
3783 return;
3784 }
3785 if (deep == 0)
3786 return;
3787 ++recurse;
3788
3789 /* lock/unlock the item itself */
3790 if (lock)
3791 tv->v_lock |= VAR_LOCKED;
3792 else
3793 tv->v_lock &= ~VAR_LOCKED;
3794
3795 switch (tv->v_type)
3796 {
3797 case VAR_LIST:
3798 if ((l = tv->vval.v_list) != NULL)
3799 {
3800 if (lock)
3801 l->lv_lock |= VAR_LOCKED;
3802 else
3803 l->lv_lock &= ~VAR_LOCKED;
3804 if (deep < 0 || deep > 1)
3805 /* recursive: lock/unlock the items the List contains */
3806 for (li = l->lv_first; li != NULL; li = li->li_next)
3807 item_lock(&li->li_tv, deep - 1, lock);
3808 }
3809 break;
3810 case VAR_DICT:
3811 if ((d = tv->vval.v_dict) != NULL)
3812 {
3813 if (lock)
3814 d->dv_lock |= VAR_LOCKED;
3815 else
3816 d->dv_lock &= ~VAR_LOCKED;
3817 if (deep < 0 || deep > 1)
3818 {
3819 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
3826 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3827 }
3828 }
3829 }
3830 }
3831 }
3832 --recurse;
3833}
3834
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003836 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3837 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003838 */
3839 static int
3840tv_islocked(tv)
3841 typval_T *tv;
3842{
3843 return (tv->v_lock & VAR_LOCKED)
3844 || (tv->v_type == VAR_LIST
3845 && tv->vval.v_list != NULL
3846 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3847 || (tv->v_type == VAR_DICT
3848 && tv->vval.v_dict != NULL
3849 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3853/*
3854 * Delete all "menutrans_" variables.
3855 */
3856 void
3857del_menutrans_vars()
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3870 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 }
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875#endif
3876
3877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3878
3879/*
3880 * Local string buffer for the next two functions to store a variable name
3881 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3882 * get_user_var_name().
3883 */
3884
3885static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3886
3887static char_u *varnamebuf = NULL;
3888static int varnamebuflen = 0;
3889
3890/*
3891 * Function to concatenate a prefix and a variable name.
3892 */
3893 static char_u *
3894cat_prefix_varname(prefix, name)
3895 int prefix;
3896 char_u *name;
3897{
3898 int len;
3899
3900 len = (int)STRLEN(name) + 3;
3901 if (len > varnamebuflen)
3902 {
3903 vim_free(varnamebuf);
3904 len += 10; /* some additional space */
3905 varnamebuf = alloc(len);
3906 if (varnamebuf == NULL)
3907 {
3908 varnamebuflen = 0;
3909 return NULL;
3910 }
3911 varnamebuflen = len;
3912 }
3913 *varnamebuf = prefix;
3914 varnamebuf[1] = ':';
3915 STRCPY(varnamebuf + 2, name);
3916 return varnamebuf;
3917}
3918
3919/*
3920 * Function given to ExpandGeneric() to obtain the list of user defined
3921 * (global/buffer/window/built-in) variable names.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *
3924get_user_var_name(xp, idx)
3925 expand_T *xp;
3926 int idx;
3927{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 static long_u gdone;
3929 static long_u bdone;
3930 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 static long_u tdone;
3933#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static int vidx;
3935 static hashitem_T *hi;
3936 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941#ifdef FEAT_WINDOWS
3942 tdone = 0;
3943#endif
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* Global variables */
3947 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003951 else
3952 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3956 return cat_prefix_varname('g', hi->hi_key);
3957 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return (char_u *)"b:changedtick";
3976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977
3978 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003979 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003982 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 else
3985 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003993 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 if (tdone < ht->ht_used)
3995 {
3996 if (tdone++ == 0)
3997 hi = ht->ht_array;
3998 else
3999 ++hi;
4000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 return cat_prefix_varname('t', hi->hi_key);
4003 }
4004#endif
4005
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 /* v: variables */
4007 if (vidx < VV_LEN)
4008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 vim_free(varnamebuf);
4011 varnamebuf = NULL;
4012 varnamebuflen = 0;
4013 return NULL;
4014}
4015
4016#endif /* FEAT_CMDL_COMPL */
4017
4018/*
4019 * types for expressions.
4020 */
4021typedef enum
4022{
4023 TYPE_UNKNOWN = 0
4024 , TYPE_EQUAL /* == */
4025 , TYPE_NEQUAL /* != */
4026 , TYPE_GREATER /* > */
4027 , TYPE_GEQUAL /* >= */
4028 , TYPE_SMALLER /* < */
4029 , TYPE_SEQUAL /* <= */
4030 , TYPE_MATCH /* =~ */
4031 , TYPE_NOMATCH /* !~ */
4032} exptype_T;
4033
4034/*
4035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4038 */
4039
4040/*
4041 * Handle zero level expression.
4042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **nextcmd;
4052 int evaluate;
4053{
4054 int ret;
4055 char_u *p;
4056
4057 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (ret == FAIL || !ends_excmd(*p))
4060 {
4061 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Report the invalid expression unless the expression evaluation has
4065 * been cancelled due to an aborting error, an interrupt, or an
4066 * exception.
4067 */
4068 if (!aborting())
4069 EMSG2(_(e_invexpr2), arg);
4070 ret = FAIL;
4071 }
4072 if (nextcmd != NULL)
4073 *nextcmd = check_nextcmd(p);
4074
4075 return ret;
4076}
4077
4078/*
4079 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004080 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * "arg" must point to the first non-white of the expression.
4083 * "arg" is advanced to the next non-white after the recognized expression.
4084 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
4086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
4095 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Get the first variable.
4100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 if ((*arg)[0] == '?')
4105 {
4106 result = FALSE;
4107 if (evaluate)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int error = FALSE;
4110
4111 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Check for the ":".
4127 */
4128 if ((*arg)[0] != ':')
4129 {
4130 EMSG(_("E109: Missing ':' after '?'"));
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 /*
4137 * Get the third variable.
4138 */
4139 *arg = skipwhite(*arg + 1);
4140 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4141 {
4142 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 return OK;
4151}
4152
4153/*
4154 * Handle first level expression:
4155 * expr2 || expr2 || expr2 logical OR
4156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 long result;
4170 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat until there is no following "||".
4181 */
4182 first = TRUE;
4183 result = FALSE;
4184 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4185 {
4186 if (evaluate && first)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 first = FALSE;
4194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 2);
4200 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4201 return FAIL;
4202
4203 /*
4204 * Compute the result.
4205 */
4206 if (evaluate && !result)
4207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (error)
4212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (evaluate)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * Handle second level expression:
4226 * expr3 && expr3 && expr3 logical AND
4227 *
4228 * "arg" must point to the first non-white of the expression.
4229 * "arg" is advanced to the next non-white after the recognized expression.
4230 *
4231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int evaluate;
4238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "&&".
4252 */
4253 first = TRUE;
4254 result = TRUE;
4255 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval4(arg, &var2, evaluate && result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle third level expression:
4297 * var1 == var2
4298 * var1 =~ var2
4299 * var1 != var2
4300 * var1 !~ var2
4301 * var1 > var2
4302 * var1 >= var2
4303 * var1 < var2
4304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 * var1 is var2
4306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *p;
4321 int i;
4322 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int len = 2;
4325 long n1, n2;
4326 char_u *s1, *s2;
4327 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4328 regmatch_T regmatch;
4329 int ic;
4330 char_u *save_cpo;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 p = *arg;
4339 switch (p[0])
4340 {
4341 case '=': if (p[1] == '=')
4342 type = TYPE_EQUAL;
4343 else if (p[1] == '~')
4344 type = TYPE_MATCH;
4345 break;
4346 case '!': if (p[1] == '=')
4347 type = TYPE_NEQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_NOMATCH;
4350 break;
4351 case '>': if (p[1] != '=')
4352 {
4353 type = TYPE_GREATER;
4354 len = 1;
4355 }
4356 else
4357 type = TYPE_GEQUAL;
4358 break;
4359 case '<': if (p[1] != '=')
4360 {
4361 type = TYPE_SMALLER;
4362 len = 1;
4363 }
4364 else
4365 type = TYPE_SEQUAL;
4366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 case 'i': if (p[1] == 's')
4368 {
4369 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4370 len = 5;
4371 if (!vim_isIDc(p[len]))
4372 {
4373 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4374 type_is = TRUE;
4375 }
4376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 if (type != TYPE_UNKNOWN)
4384 {
4385 /* extra question mark appended: ignore case */
4386 if (p[len] == '?')
4387 {
4388 ic = TRUE;
4389 ++len;
4390 }
4391 /* extra '#' appended: match case */
4392 else if (p[len] == '#')
4393 {
4394 ic = FALSE;
4395 ++len;
4396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 ic = p_ic;
4400
4401 /*
4402 * Get the second variable.
4403 */
4404 *arg = skipwhite(p + len);
4405 if (eval5(arg, &var2, evaluate) == FAIL)
4406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410
4411 if (evaluate)
4412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type_is && rettv->v_type != var2.v_type)
4414 {
4415 /* For "is" a different type always means FALSE, for "notis"
4416 * it means TRUE. */
4417 n1 = (type == TYPE_NEQUAL);
4418 }
4419 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4420 {
4421 if (type_is)
4422 {
4423 n1 = (rettv->v_type == var2.v_type
4424 && rettv->vval.v_list == var2.vval.v_list);
4425 if (type == TYPE_NEQUAL)
4426 n1 = !n1;
4427 }
4428 else if (rettv->v_type != var2.v_type
4429 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4430 {
4431 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004434 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 clear_tv(rettv);
4436 clear_tv(&var2);
4437 return FAIL;
4438 }
4439 else
4440 {
4441 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004442 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4443 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_dict == var2.vval.v_dict);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
4462 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4463 else
4464 EMSG(_("E736: Invalid operation for Dictionary"));
4465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4473 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4480 {
4481 if (rettv->v_type != var2.v_type
4482 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4483 {
4484 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 clear_tv(rettv);
4489 clear_tv(&var2);
4490 return FAIL;
4491 }
4492 else
4493 {
4494 /* Compare two Funcrefs for being equal or unequal. */
4495 if (rettv->vval.v_string == NULL
4496 || var2.vval.v_string == NULL)
4497 n1 = FALSE;
4498 else
4499 n1 = STRCMP(rettv->vval.v_string,
4500 var2.vval.v_string) == 0;
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506#ifdef FEAT_FLOAT
4507 /*
4508 * If one of the two variables is a float, compare as a float.
4509 * When using "=~" or "!~", always compare as string.
4510 */
4511 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4512 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4513 {
4514 float_T f1, f2;
4515
4516 if (rettv->v_type == VAR_FLOAT)
4517 f1 = rettv->vval.v_float;
4518 else
4519 f1 = get_tv_number(rettv);
4520 if (var2.v_type == VAR_FLOAT)
4521 f2 = var2.vval.v_float;
4522 else
4523 f2 = get_tv_number(&var2);
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (f1 == f2); break;
4528 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4529 case TYPE_GREATER: n1 = (f1 > f2); break;
4530 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4531 case TYPE_SMALLER: n1 = (f1 < f2); break;
4532 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /*
4541 * If one of the two variables is a number, compare as a number.
4542 * When using "=~" or "!~", always compare as string.
4543 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(rettv);
4548 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (n1 == n2); break;
4552 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4553 case TYPE_GREATER: n1 = (n1 > n2); break;
4554 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4555 case TYPE_SMALLER: n1 = (n1 < n2); break;
4556 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4557 case TYPE_UNKNOWN:
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH: break; /* avoid gcc warning */
4560 }
4561 }
4562 else
4563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 s1 = get_tv_string_buf(rettv, buf1);
4565 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4567 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4568 else
4569 i = 0;
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (i == 0); break;
4574 case TYPE_NEQUAL: n1 = (i != 0); break;
4575 case TYPE_GREATER: n1 = (i > 0); break;
4576 case TYPE_GEQUAL: n1 = (i >= 0); break;
4577 case TYPE_SMALLER: n1 = (i < 0); break;
4578 case TYPE_SEQUAL: n1 = (i <= 0); break;
4579
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH:
4582 /* avoid 'l' flag in 'cpoptions' */
4583 save_cpo = p_cpo;
4584 p_cpo = (char_u *)"";
4585 regmatch.regprog = vim_regcomp(s2,
4586 RE_MAGIC + RE_STRING);
4587 regmatch.rm_ic = ic;
4588 if (regmatch.regprog != NULL)
4589 {
4590 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (type == TYPE_NOMATCH)
4593 n1 = !n1;
4594 }
4595 p_cpo = save_cpo;
4596 break;
4597
4598 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4599 }
4600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 clear_tv(rettv);
4602 clear_tv(&var2);
4603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * Handle fourth level expression:
4613 * + number addition
4614 * - number subtraction
4615 * . string concatenation
4616 *
4617 * "arg" must point to the first non-white of the expression.
4618 * "arg" is advanced to the next non-white after the recognized expression.
4619 *
4620 * Return OK or FAIL.
4621 */
4622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int evaluate;
4627{
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 typval_T var2;
4629 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int op;
4631 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632#ifdef FEAT_FLOAT
4633 float_T f1 = 0, f2 = 0;
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *s1, *s2;
4636 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4637 char_u *p;
4638
4639 /*
4640 * Get the first variable.
4641 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
4644
4645 /*
4646 * Repeat computing, until no '+', '-' or '.' is following.
4647 */
4648 for (;;)
4649 {
4650 op = **arg;
4651 if (op != '+' && op != '-' && op != '.')
4652 break;
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 if ((op != '+' || rettv->v_type != VAR_LIST)
4655#ifdef FEAT_FLOAT
4656 && (op == '.' || rettv->v_type != VAR_FLOAT)
4657#endif
4658 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 {
4660 /* For "list + ...", an illegal use of the first operand as
4661 * a number cannot be determined before evaluating the 2nd
4662 * operand: if this is also a list, all is ok.
4663 * For "something . ...", "something - ..." or "non-list + ...",
4664 * we know that the first operand needs to be a string or number
4665 * without evaluating the 2nd operand. So check before to avoid
4666 * side effects after an error. */
4667 if (evaluate && get_tv_string_chk(rettv) == NULL)
4668 {
4669 clear_tv(rettv);
4670 return FAIL;
4671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
4675 * Get the second variable.
4676 */
4677 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004678 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683
4684 if (evaluate)
4685 {
4686 /*
4687 * Compute the result.
4688 */
4689 if (op == '.')
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4692 s2 = get_tv_string_buf_chk(&var2, buf2);
4693 if (s2 == NULL) /* type error ? */
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 rettv->v_type = VAR_STRING;
4702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 else if (op == '+' && rettv->v_type == VAR_LIST
4705 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 {
4707 /* concatenate Lists */
4708 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4709 &var3) == FAIL)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715 clear_tv(rettv);
4716 *rettv = var3;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 f1 = rettv->vval.v_float;
4726 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 else
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 n1 = get_tv_number_chk(rettv, &error);
4732 if (error)
4733 {
4734 /* This can only happen for "list + non-list". For
4735 * "non-list + ..." or "something - ...", we returned
4736 * before evaluating the 2nd operand. */
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (var2.v_type == VAR_FLOAT)
4742 f1 = n1;
4743#endif
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 {
4748 f2 = var2.vval.v_float;
4749 n2 = 0;
4750 }
4751 else
4752#endif
4753 {
4754 n2 = get_tv_number_chk(&var2, &error);
4755 if (error)
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
4761#ifdef FEAT_FLOAT
4762 if (rettv->v_type == VAR_FLOAT)
4763 f2 = n2;
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767
4768#ifdef FEAT_FLOAT
4769 /* If there is a float on either side the result is a float. */
4770 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4771 {
4772 if (op == '+')
4773 f1 = f1 + f2;
4774 else
4775 f1 = f1 - f2;
4776 rettv->v_type = VAR_FLOAT;
4777 rettv->vval.v_float = f1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#endif
4781 {
4782 if (op == '+')
4783 n1 = n1 + n2;
4784 else
4785 n1 = n1 - n2;
4786 rettv->v_type = VAR_NUMBER;
4787 rettv->vval.v_number = n1;
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793 return OK;
4794}
4795
4796/*
4797 * Handle fifth level expression:
4798 * * number multiplication
4799 * / number division
4800 * % number modulo
4801 *
4802 * "arg" must point to the first non-white of the expression.
4803 * "arg" is advanced to the next non-white after the recognized expression.
4804 *
4805 * Return OK or FAIL.
4806 */
4807 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int op;
4816 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 int use_float = FALSE;
4819 float_T f1 = 0, f2;
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 /*
4824 * Get the first variable.
4825 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 /*
4830 * Repeat computing, until no '*', '/' or '%' is following.
4831 */
4832 for (;;)
4833 {
4834 op = **arg;
4835 if (op != '*' && op != '/' && op != '%')
4836 break;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 {
4843 f1 = rettv->vval.v_float;
4844 use_float = TRUE;
4845 n1 = 0;
4846 }
4847 else
4848#endif
4849 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 if (error)
4852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else
4855 n1 = 0;
4856
4857 /*
4858 * Get the second variable.
4859 */
4860 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 if (evaluate)
4865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (var2.v_type == VAR_FLOAT)
4868 {
4869 if (!use_float)
4870 {
4871 f1 = n1;
4872 use_float = TRUE;
4873 }
4874 f2 = var2.vval.v_float;
4875 n2 = 0;
4876 }
4877 else
4878#endif
4879 {
4880 n2 = get_tv_number_chk(&var2, &error);
4881 clear_tv(&var2);
4882 if (error)
4883 return FAIL;
4884#ifdef FEAT_FLOAT
4885 if (use_float)
4886 f2 = n2;
4887#endif
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 if (op == '*')
4898 f1 = f1 * f2;
4899 else if (op == '/')
4900 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# ifdef VMS
4902 /* VMS crashes on divide by zero, work around it */
4903 if (f2 == 0.0)
4904 {
4905 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004910 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004911 }
4912 else
4913 f1 = f1 / f2;
4914# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 /* We rely on the floating point library to handle divide
4916 * by zero to result in "inf" and not a crash. */
4917 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004922 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 return FAIL;
4924 }
4925 rettv->v_type = VAR_FLOAT;
4926 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 n1 = n1 * n2;
4933 else if (op == '/')
4934 {
4935 if (n2 == 0) /* give an error message? */
4936 {
4937 if (n1 == 0)
4938 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4939 else if (n1 < 0)
4940 n1 = -0x7fffffffL;
4941 else
4942 n1 = 0x7fffffffL;
4943 }
4944 else
4945 n1 = n1 / n2;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
4949 if (n2 == 0) /* give an error message? */
4950 n1 = 0;
4951 else
4952 n1 = n1 % n2;
4953 }
4954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 }
4959
4960 return OK;
4961}
4962
4963/*
4964 * Handle sixth level expression:
4965 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004966 * "string" string constant
4967 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * &option-name option value
4969 * @r register contents
4970 * identifier variable value
4971 * function() function call
4972 * $VAR environment variable
4973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004974 * [expr, expr] List
4975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 *
4977 * Also handle:
4978 * ! in front logical NOT
4979 * - in front unary minus
4980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * trailing [] subscript in String or List
4982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * "arg" must point to the first non-white of the expression.
4985 * "arg" is advanced to the next non-white after the recognized expression.
4986 *
4987 * Return OK or FAIL.
4988 */
4989 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004990eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004994 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 long n;
4997 int len;
4998 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 char_u *start_leader, *end_leader;
5000 int ret = OK;
5001 char_u *alias;
5002
5003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Skip '!' and '-' characters. They are handled later.
5011 */
5012 start_leader = *arg;
5013 while (**arg == '!' || **arg == '-' || **arg == '+')
5014 *arg = skipwhite(*arg + 1);
5015 end_leader = *arg;
5016
5017 switch (**arg)
5018 {
5019 /*
5020 * Number constant.
5021 */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
5029 case '7':
5030 case '8':
5031 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 {
5033#ifdef FEAT_FLOAT
5034 char_u *p = skipdigits(*arg + 1);
5035 int get_float = FALSE;
5036
5037 /* We accept a float when the format matches
5038 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005039 * strict to avoid backwards compatibility problems.
5040 * Don't look for a float after the "." operator, so that
5041 * ":let vers = 1.2.3" doesn't fail. */
5042 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 get_float = TRUE;
5045 p = skipdigits(p + 2);
5046 if (*p == 'e' || *p == 'E')
5047 {
5048 ++p;
5049 if (*p == '-' || *p == '+')
5050 ++p;
5051 if (!vim_isdigit(*p))
5052 get_float = FALSE;
5053 else
5054 p = skipdigits(p + 1);
5055 }
5056 if (ASCII_ISALPHA(*p) || *p == '.')
5057 get_float = FALSE;
5058 }
5059 if (get_float)
5060 {
5061 float_T f;
5062
5063 *arg += string2float(*arg, &f);
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_FLOAT;
5067 rettv->vval.v_float = f;
5068 }
5069 }
5070 else
5071#endif
5072 {
5073 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5074 *arg += len;
5075 if (evaluate)
5076 {
5077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = n;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * String constant: "string".
5086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 break;
5095
5096 /*
5097 * List: [expr, expr]
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Dictionary: {key: val, key: val}
5104 */
5105 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5106 break;
5107
5108 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
5115 * Environment variable: $VAR.
5116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Register contents: @r.
5122 */
5123 case '@': ++*arg;
5124 if (evaluate)
5125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005127 rettv->vval.v_string = get_reg_contents(**arg,
5128 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 if (**arg != NUL)
5131 ++*arg;
5132 break;
5133
5134 /*
5135 * nested expression: (expression).
5136 */
5137 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (**arg == ')')
5140 ++*arg;
5141 else if (ret == OK)
5142 {
5143 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ret = FAIL;
5146 }
5147 break;
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 if (ret == NOTDONE)
5154 {
5155 /*
5156 * Must be a variable or function name.
5157 * Can also be a curly-braces kind of name: {expr}.
5158 */
5159 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (alias != NULL)
5162 s = alias;
5163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 ret = FAIL;
5166 else
5167 {
5168 if (**arg == '(') /* recursive! */
5169 {
5170 /* If "s" is the name of a variable of type VAR_FUNC
5171 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005172 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173
5174 /* Invoke the function. */
5175 ret = get_func_tv(s, len, rettv, arg,
5176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005178
5179 /* If evaluate is FALSE rettv->v_type was not set in
5180 * get_func_tv, but it's needed in handle_subscript() to parse
5181 * what follows. So set it here. */
5182 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5183 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005184 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185 rettv->v_type = VAR_FUNC;
5186 }
5187
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /* Stop the expression evaluation when immediately
5189 * aborting on error, or when an interrupt occurred or
5190 * an exception was thrown but not caught. */
5191 if (aborting())
5192 {
5193 if (ret == OK)
5194 clear_tv(rettv);
5195 ret = FAIL;
5196 }
5197 }
5198 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005199 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005200 else
5201 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005203 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *arg = skipwhite(*arg);
5207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5209 * expr(expr). */
5210 if (ret == OK)
5211 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /*
5214 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5215 */
5216 if (ret == OK && evaluate && end_leader > start_leader)
5217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 int val = 0;
5220#ifdef FEAT_FLOAT
5221 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 if (rettv->v_type == VAR_FLOAT)
5224 f = rettv->vval.v_float;
5225 else
5226#endif
5227 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 clear_tv(rettv);
5231 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else
5234 {
5235 while (end_leader > start_leader)
5236 {
5237 --end_leader;
5238 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 {
5240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 f = !f;
5243 else
5244#endif
5245 val = !val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 {
5249#ifdef FEAT_FLOAT
5250 if (rettv->v_type == VAR_FLOAT)
5251 f = -f;
5252 else
5253#endif
5254 val = -val;
5255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257#ifdef FEAT_FLOAT
5258 if (rettv->v_type == VAR_FLOAT)
5259 {
5260 clear_tv(rettv);
5261 rettv->vval.v_float = f;
5262 }
5263 else
5264#endif
5265 {
5266 clear_tv(rettv);
5267 rettv->v_type = VAR_NUMBER;
5268 rettv->vval.v_number = val;
5269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
5273 return ret;
5274}
5275
5276/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005277 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5278 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5280 */
5281 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
5288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 long len = -1;
5292 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005296 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 return FAIL;
5301 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302#ifdef FEAT_FLOAT
5303 else if (rettv->v_type == VAR_FLOAT)
5304 {
5305 if (verbose)
5306 EMSG(_(e_float_as_string));
5307 return FAIL;
5308 }
5309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 /*
5314 * dict.name
5315 */
5316 key = *arg + 1;
5317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5318 ;
5319 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /*
5326 * something[idx]
5327 *
5328 * Get the (first) variable from inside the [].
5329 */
5330 *arg = skipwhite(*arg + 1);
5331 if (**arg == ':')
5332 empty1 = TRUE;
5333 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5334 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5336 {
5337 /* not a number or string */
5338 clear_tv(&var1);
5339 return FAIL;
5340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 /*
5343 * Get the second variable from inside the [:].
5344 */
5345 if (**arg == ':')
5346 {
5347 range = TRUE;
5348 *arg = skipwhite(*arg + 1);
5349 if (**arg == ']')
5350 empty2 = TRUE;
5351 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (!empty1)
5354 clear_tv(&var1);
5355 return FAIL;
5356 }
5357 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5358 {
5359 /* not a number or string */
5360 if (!empty1)
5361 clear_tv(&var1);
5362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 return FAIL;
5364 }
5365 }
5366
5367 /* Check for the ']'. */
5368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
5377 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 n1 = 0;
5383 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 n1 = get_tv_number(&var1);
5386 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 if (range)
5389 {
5390 if (empty2)
5391 n2 = -1;
5392 else
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 n2 = get_tv_number(&var2);
5395 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
5401 case VAR_NUMBER:
5402 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 len = (long)STRLEN(s);
5405 if (range)
5406 {
5407 /* The resulting variable is a substring. If the indexes
5408 * are out of range the result is empty. */
5409 if (n1 < 0)
5410 {
5411 n1 = len + n1;
5412 if (n1 < 0)
5413 n1 = 0;
5414 }
5415 if (n2 < 0)
5416 n2 = len + n2;
5417 else if (n2 >= len)
5418 n2 = len;
5419 if (n1 >= len || n2 < 0 || n1 > n2)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5423 }
5424 else
5425 {
5426 /* The resulting variable is a string of a single
5427 * character. If the index is too big or negative the
5428 * result is empty. */
5429 if (n1 >= len || n1 < 0)
5430 s = NULL;
5431 else
5432 s = vim_strnsave(s + n1, 1);
5433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 clear_tv(rettv);
5435 rettv->v_type = VAR_STRING;
5436 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 break;
5438
5439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 if (n1 < 0)
5442 n1 = len + n1;
5443 if (!empty1 && (n1 < 0 || n1 >= len))
5444 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005445 /* For a range we allow invalid values and return an empty
5446 * list. A list index out of range is an error. */
5447 if (!range)
5448 {
5449 if (verbose)
5450 EMSGN(_(e_listidx), n1);
5451 return FAIL;
5452 }
5453 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 if (range)
5456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 list_T *l;
5458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459
5460 if (n2 < 0)
5461 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005462 else if (n2 >= len)
5463 n2 = len - 1;
5464 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 l = list_alloc();
5467 if (l == NULL)
5468 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 n1 <= n2; ++n1)
5471 {
5472 if (list_append_tv(l, &item->li_tv) == FAIL)
5473 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005474 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
5476 }
5477 item = item->li_next;
5478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 clear_tv(rettv);
5480 rettv->v_type = VAR_LIST;
5481 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005482 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 else
5485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 clear_tv(rettv);
5488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
5492 case VAR_DICT:
5493 if (range)
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (len == -1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 if (len == -1)
5505 {
5506 key = get_tv_string(&var1);
5507 if (*key == NUL)
5508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005509 if (verbose)
5510 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 }
5515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 if (len == -1)
5521 clear_tv(&var1);
5522 if (item == NULL)
5523 return FAIL;
5524
5525 copy_tv(&item->di_tv, &var1);
5526 clear_tv(rettv);
5527 *rettv = var1;
5528 }
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Get an option value.
5538 * "arg" points to the '&' or '+' before the option name.
5539 * "arg" is advanced to character after the option name.
5540 * Return OK or FAIL.
5541 */
5542 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int evaluate;
5547{
5548 char_u *option_end;
5549 long numval;
5550 char_u *stringval;
5551 int opt_type;
5552 int c;
5553 int working = (**arg == '+'); /* has("+option") */
5554 int ret = OK;
5555 int opt_flags;
5556
5557 /*
5558 * Isolate the option name and find its value.
5559 */
5560 option_end = find_option_end(arg, &opt_flags);
5561 if (option_end == NULL)
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 EMSG2(_("E112: Option name missing: %s"), *arg);
5565 return FAIL;
5566 }
5567
5568 if (!evaluate)
5569 {
5570 *arg = option_end;
5571 return OK;
5572 }
5573
5574 c = *option_end;
5575 *option_end = NUL;
5576 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (opt_type == -3) /* invalid name */
5580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 EMSG2(_("E113: Unknown option: %s"), *arg);
5583 ret = FAIL;
5584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (opt_type == -2) /* hidden string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == -1) /* hidden number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == 1) /* number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else /* string option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 }
5608 else if (working && (opt_type == -2 || opt_type == -1))
5609 ret = FAIL;
5610
5611 *option_end = c; /* put back for error messages */
5612 *arg = option_end;
5613
5614 return ret;
5615}
5616
5617/*
5618 * Allocate a variable for a string constant.
5619 * Return OK or FAIL.
5620 */
5621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int evaluate;
5626{
5627 char_u *p;
5628 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int extra = 0;
5630
5631 /*
5632 * Find the end of the string, skipping backslashed characters.
5633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (*p == '\\' && p[1] != NUL)
5637 {
5638 ++p;
5639 /* A "\<x>" form occupies at least 4 characters, and produces up
5640 * to 6 characters: reserve space for 2 extra */
5641 if (*p == '<')
5642 extra += 2;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
5646 if (*p != '"')
5647 {
5648 EMSG2(_("E114: Missing quote: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 /* If only parsing, set *arg and return here */
5653 if (!evaluate)
5654 {
5655 *arg = p + 1;
5656 return OK;
5657 }
5658
5659 /*
5660 * Copy the string into allocated memory, handling backslashed
5661 * characters.
5662 */
5663 name = alloc((unsigned)(p - *arg + extra));
5664 if (name == NULL)
5665 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 rettv->v_type = VAR_STRING;
5667 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\')
5672 {
5673 switch (*++p)
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case 'b': *name++ = BS; ++p; break;
5676 case 'e': *name++ = ESC; ++p; break;
5677 case 'f': *name++ = FF; ++p; break;
5678 case 'n': *name++ = NL; ++p; break;
5679 case 'r': *name++ = CAR; ++p; break;
5680 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 case 'X': /* hex: "\x1", "\x12" */
5683 case 'x':
5684 case 'u': /* Unicode: "\u0023" */
5685 case 'U':
5686 if (vim_isxdigit(p[1]))
5687 {
5688 int n, nr;
5689 int c = toupper(*p);
5690
5691 if (c == 'X')
5692 n = 2;
5693 else
5694 n = 4;
5695 nr = 0;
5696 while (--n >= 0 && vim_isxdigit(p[1]))
5697 {
5698 ++p;
5699 nr = (nr << 4) + hex2nr(*p);
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#ifdef FEAT_MBYTE
5703 /* For "\u" store the number according to
5704 * 'encoding'. */
5705 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712
5713 /* octal: "\1", "\12", "\123" */
5714 case '0':
5715 case '1':
5716 case '2':
5717 case '3':
5718 case '4':
5719 case '5':
5720 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 case '7': *name = *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = (*name << 3) + *p++ - '0';
5725 if (*p >= '0' && *p <= '7')
5726 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 break;
5730
5731 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (extra != 0)
5734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 /* FALLTHROUGH */
5739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 }
5744 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg = p + 1;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return OK;
5752}
5753
5754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Return OK or FAIL.
5757 */
5758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 int evaluate;
5763{
5764 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 int reduce = 0;
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++reduce;
5778 ++p;
5779 }
5780 }
5781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return FAIL;
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 if (!evaluate)
5790 {
5791 *arg = p + 1;
5792 return OK;
5793 }
5794
5795 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 */
5798 str = alloc((unsigned)((p - *arg) - reduce));
5799 if (str == NULL)
5800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 rettv->v_type = VAR_STRING;
5802 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 break;
5810 ++p;
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 *arg = p + 1;
5816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 return OK;
5818}
5819
5820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 * Allocate a variable for a List and fill it from "*arg".
5822 * Return OK or FAIL.
5823 */
5824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 int evaluate;
5829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 list_T *l = NULL;
5831 typval_T tv;
5832 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
5834 if (evaluate)
5835 {
5836 l = list_alloc();
5837 if (l == NULL)
5838 return FAIL;
5839 }
5840
5841 *arg = skipwhite(*arg + 1);
5842 while (**arg != ']' && **arg != NUL)
5843 {
5844 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5845 goto failret;
5846 if (evaluate)
5847 {
5848 item = listitem_alloc();
5849 if (item != NULL)
5850 {
5851 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 list_append(l, item);
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 else
5856 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
5859 if (**arg == ']')
5860 break;
5861 if (**arg != ',')
5862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 goto failret;
5865 }
5866 *arg = skipwhite(*arg + 1);
5867 }
5868
5869 if (**arg != ']')
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872failret:
5873 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 return FAIL;
5876 }
5877
5878 *arg = skipwhite(*arg + 1);
5879 if (evaluate)
5880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 rettv->v_type = VAR_LIST;
5882 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 ++l->lv_refcount;
5884 }
5885
5886 return OK;
5887}
5888
5889/*
5890 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005891 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005893 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894list_alloc()
5895{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 list_T *l;
5897
5898 l = (list_T *)alloc_clear(sizeof(list_T));
5899 if (l != NULL)
5900 {
5901 /* Prepend the list to the list of lists for garbage collection. */
5902 if (first_list != NULL)
5903 first_list->lv_used_prev = l;
5904 l->lv_used_prev = NULL;
5905 l->lv_used_next = first_list;
5906 first_list = l;
5907 }
5908 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005912 * Allocate an empty list for a return value.
5913 * Returns OK or FAIL.
5914 */
5915 static int
5916rettv_list_alloc(rettv)
5917 typval_T *rettv;
5918{
5919 list_T *l = list_alloc();
5920
5921 if (l == NULL)
5922 return FAIL;
5923
5924 rettv->vval.v_list = l;
5925 rettv->v_type = VAR_LIST;
5926 ++l->lv_refcount;
5927 return OK;
5928}
5929
5930/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 * Unreference a list: decrement the reference count and free it when it
5932 * becomes zero.
5933 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005934 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (l != NULL && --l->lv_refcount <= 0)
5939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940}
5941
5942/*
5943 * Free a list, including all items it points to.
5944 * Ignores the reference count.
5945 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005946 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947list_free(l, recurse)
5948 list_T *l;
5949 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 /* Remove the list from the list of lists for garbage collection. */
5954 if (l->lv_used_prev == NULL)
5955 first_list = l->lv_used_next;
5956 else
5957 l->lv_used_prev->lv_used_next = l->lv_used_next;
5958 if (l->lv_used_next != NULL)
5959 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5960
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it. */
5964 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005965 if (recurse || (item->li_tv.v_type != VAR_LIST
5966 && item->li_tv.v_type != VAR_DICT))
5967 clear_tv(&item->li_tv);
5968 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 }
5970 vim_free(l);
5971}
5972
5973/*
5974 * Allocate a list item.
5975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977listitem_alloc()
5978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980}
5981
5982/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005983 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 vim_free(item);
5991}
5992
5993/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994 * Remove a list item from a List and free it. Also clears the value.
5995 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l;
5999 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000{
6001 list_remove(l, item, item);
6002 listitem_free(item);
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Get the number of items in a list.
6007 */
6008 static long
6009list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 if (l == NULL)
6013 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006014 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * Return TRUE when two lists have exactly the same values.
6019 */
6020 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 list_T *l1;
6023 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006029 if (l1 == NULL || l2 == NULL)
6030 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 if (l1 == l2)
6032 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 if (list_len(l1) != list_len(l2))
6034 return FALSE;
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 for (item1 = l1->lv_first, item2 = l2->lv_first;
6037 item1 != NULL && item2 != NULL;
6038 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return FALSE;
6041 return item1 == NULL && item2 == NULL;
6042}
6043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006044#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6045 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046/*
6047 * Return the dictitem that an entry in a hashtable points to.
6048 */
6049 dictitem_T *
6050dict_lookup(hi)
6051 hashitem_T *hi;
6052{
6053 return HI2DI(hi);
6054}
6055#endif
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 * Return TRUE when two dictionaries have exactly the same key/values.
6059 */
6060 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d1;
6063 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 hashitem_T *hi;
6068 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (d1 == NULL || d2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (d1 == d2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (dict_len(d1) != dict_len(d2))
6076 return FALSE;
6077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006078 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006081 if (!HASHITEM_EMPTY(hi))
6082 {
6083 item2 = dict_find(d2, hi->hi_key, -1);
6084 if (item2 == NULL)
6085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 return FALSE;
6088 --todo;
6089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006090 }
6091 return TRUE;
6092}
6093
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094static int tv_equal_recurse_limit;
6095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 typval_T *tv1;
6104 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 int ic; /* ignore case */
6106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
6108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 * recursiveness to a limit. We guess they are equal then.
6118 * A fixed limit has the problem of still taking an awful long time.
6119 * Reduce the limit every time running into it. That should work fine for
6120 * deeply linked structures that are not recursively linked and catch
6121 * recursiveness quickly. */
6122 if (!recursive)
6123 tv_equal_recurse_limit = 1000;
6124 if (recursive_cnt >= tv_equal_recurse_limit)
6125 {
6126 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 ++recursive_cnt;
6134 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6135 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006136 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_FUNC:
6145 return (tv1->vval.v_string != NULL
6146 && tv2->vval.v_string != NULL
6147 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6148
6149 case VAR_NUMBER:
6150 return tv1->vval.v_number == tv2->vval.v_number;
6151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152#ifdef FEAT_FLOAT
6153 case VAR_FLOAT:
6154 return tv1->vval.v_float == tv2->vval.v_float;
6155#endif
6156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 case VAR_STRING:
6158 s1 = get_tv_string_buf(tv1, buf1);
6159 s2 = get_tv_string_buf(tv2, buf2);
6160 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 return TRUE;
6165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Locate item with index "n" in list "l" and return it.
6169 * A negative index is counted from the end; -1 is the last item.
6170 * Returns NULL when "n" is out of range.
6171 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long n;
6176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 long idx;
6179
6180 if (l == NULL)
6181 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 n = l->lv_len + n;
6186
6187 /* Check for index out of range. */
6188 if (n < 0 || n >= l->lv_len)
6189 return NULL;
6190
6191 /* When there is a cached index may start search from there. */
6192 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_idx / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else if (n > (l->lv_idx + l->lv_len) / 2)
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
6206 else
6207 {
6208 /* closest to the cached index */
6209 item = l->lv_idx_item;
6210 idx = l->lv_idx;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
6213 else
6214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 if (n < l->lv_len / 2)
6216 {
6217 /* closest to the start of the list */
6218 item = l->lv_first;
6219 idx = 0;
6220 }
6221 else
6222 {
6223 /* closest to the end of the list */
6224 item = l->lv_last;
6225 idx = l->lv_len - 1;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228
6229 while (n > idx)
6230 {
6231 /* search forward */
6232 item = item->li_next;
6233 ++idx;
6234 }
6235 while (n < idx)
6236 {
6237 /* search backward */
6238 item = item->li_prev;
6239 --idx;
6240 }
6241
6242 /* cache the used index */
6243 l->lv_idx = idx;
6244 l->lv_idx_item = item;
6245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return item;
6247}
6248
6249/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 * Get list item "l[idx]" as a number.
6251 */
6252 static long
6253list_find_nr(l, idx, errorp)
6254 list_T *l;
6255 long idx;
6256 int *errorp; /* set to TRUE when something wrong */
6257{
6258 listitem_T *li;
6259
6260 li = list_find(l, idx);
6261 if (li == NULL)
6262 {
6263 if (errorp != NULL)
6264 *errorp = TRUE;
6265 return -1L;
6266 }
6267 return get_tv_number_chk(&li->li_tv, errorp);
6268}
6269
6270/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006271 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6272 */
6273 char_u *
6274list_find_str(l, idx)
6275 list_T *l;
6276 long idx;
6277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx - 1);
6281 if (li == NULL)
6282 {
6283 EMSGN(_(e_listidx), idx);
6284 return NULL;
6285 }
6286 return get_tv_string(&li->li_tv);
6287}
6288
6289/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290 * Locate "item" list "l" and return its index.
6291 * Returns -1 when "item" is not in the list.
6292 */
6293 static long
6294list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297{
6298 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006300
6301 if (l == NULL)
6302 return -1;
6303 idx = 0;
6304 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6305 ++idx;
6306 if (li == NULL)
6307 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006308 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Append item "item" to the end of list "l".
6313 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
6319 if (l->lv_last == NULL)
6320 {
6321 /* empty list */
6322 l->lv_first = item;
6323 l->lv_last = item;
6324 item->li_prev = NULL;
6325 }
6326 else
6327 {
6328 l->lv_last->li_next = item;
6329 item->li_prev = l->lv_last;
6330 l->lv_last = item;
6331 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 item->li_next = NULL;
6334}
6335
6336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006340 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l;
6343 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006349 copy_tv(tv, &li->li_tv);
6350 list_append(l, li);
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 * Return FAIL when out of memory.
6357 */
6358 int
6359list_append_dict(list, dict)
6360 list_T *list;
6361 dict_T *dict;
6362{
6363 listitem_T *li = listitem_alloc();
6364
6365 if (li == NULL)
6366 return FAIL;
6367 li->li_tv.v_type = VAR_DICT;
6368 li->li_tv.v_lock = 0;
6369 li->li_tv.vval.v_dict = dict;
6370 list_append(list, li);
6371 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 * Returns FAIL when out of memory.
6379 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 list_T *l;
6383 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385{
6386 listitem_T *li = listitem_alloc();
6387
6388 if (li == NULL)
6389 return FAIL;
6390 list_append(l, li);
6391 li->li_tv.v_type = VAR_STRING;
6392 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 if (str == NULL)
6394 li->li_tv.vval.v_string = NULL;
6395 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006396 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006397 return FAIL;
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006402 * Append "n" to list "l".
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_append_number(l, n)
6407 list_T *l;
6408 varnumber_T n;
6409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l;
6430 typval_T *tv;
6431 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
6443list_insert(l, ni, item)
6444 list_T *l;
6445 listitem_T *ni;
6446 listitem_T *item;
6447{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (item == NULL)
6449 /* Append new item at end of list. */
6450 list_append(l, ni);
6451 else
6452 {
6453 /* Insert new item before existing item. */
6454 ni->li_prev = item->li_prev;
6455 ni->li_next = item;
6456 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 ++l->lv_idx;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 l->lv_idx_item = NULL;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469}
6470
6471/*
6472 * Extend "l1" with "l2".
6473 * If "bef" is NULL append at the end, otherwise insert before this item.
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
6477list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l1;
6479 list_T *l2;
6480 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006485 /* We also quit the loop when we have inserted the original item count of
6486 * the list, avoid a hang when we extend a list with itself. */
6487 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6489 return FAIL;
6490 return OK;
6491}
6492
6493/*
6494 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6495 * Return FAIL when out of memory.
6496 */
6497 static int
6498list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l1;
6500 list_T *l2;
6501 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006505 if (l1 == NULL || l2 == NULL)
6506 return FAIL;
6507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (l == NULL)
6511 return FAIL;
6512 tv->v_type = VAR_LIST;
6513 tv->vval.v_list = l;
6514
6515 /* append all items from the second list */
6516 return list_extend(l, l2, NULL);
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Returns NULL when out of memory.
6524 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *copy;
6532 listitem_T *item;
6533 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 if (orig == NULL)
6536 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 copy = list_alloc();
6539 if (copy != NULL)
6540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (copyID != 0)
6542 {
6543 /* Do this before adding the items, because one of the items may
6544 * refer back to this list. */
6545 orig->lv_copyID = copyID;
6546 orig->lv_copylist = copy;
6547 }
6548 for (item = orig->lv_first; item != NULL && !got_int;
6549 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
6551 ni = listitem_alloc();
6552 if (ni == NULL)
6553 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6557 {
6558 vim_free(ni);
6559 break;
6560 }
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006563 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 list_append(copy, ni);
6565 }
6566 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (item != NULL)
6568 {
6569 list_unref(copy);
6570 copy = NULL;
6571 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 return copy;
6575}
6576
6577/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006581 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006582list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *l;
6584 listitem_T *item;
6585 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 /* notify watchers */
6590 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006592 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 list_fix_watch(l, ip);
6594 if (ip == item2)
6595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597
6598 if (item2->li_next == NULL)
6599 l->lv_last = item->li_prev;
6600 else
6601 item2->li_next->li_prev = item->li_prev;
6602 if (item->li_prev == NULL)
6603 l->lv_first = item2->li_next;
6604 else
6605 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607}
6608
6609/*
6610 * Return an allocated string with the string representation of a list.
6611 * May return NULL.
6612 */
6613 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
6618 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (tv->vval.v_list == NULL)
6621 return NULL;
6622 ga_init2(&ga, (int)sizeof(char), 80);
6623 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 {
6626 vim_free(ga.ga_data);
6627 return NULL;
6628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 ga_append(&ga, ']');
6630 ga_append(&ga, NUL);
6631 return (char_u *)ga.ga_data;
6632}
6633
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006634typedef struct join_S {
6635 char_u *s;
6636 char_u *tofree;
6637} join_T;
6638
6639 static int
6640list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6641 garray_T *gap; /* to store the result in */
6642 list_T *l;
6643 char_u *sep;
6644 int echo_style;
6645 int copyID;
6646 garray_T *join_gap; /* to keep each list item string */
6647{
6648 int i;
6649 join_T *p;
6650 int len;
6651 int sumlen = 0;
6652 int first = TRUE;
6653 char_u *tofree;
6654 char_u numbuf[NUMBUFLEN];
6655 listitem_T *item;
6656 char_u *s;
6657
6658 /* Stringify each item in the list. */
6659 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6660 {
6661 if (echo_style)
6662 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6663 else
6664 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6665 if (s == NULL)
6666 return FAIL;
6667
6668 len = (int)STRLEN(s);
6669 sumlen += len;
6670
6671 ga_grow(join_gap, 1);
6672 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6673 if (tofree != NULL || s != numbuf)
6674 {
6675 p->s = s;
6676 p->tofree = tofree;
6677 }
6678 else
6679 {
6680 p->s = vim_strnsave(s, len);
6681 p->tofree = p->s;
6682 }
6683
6684 line_breakcheck();
6685 }
6686
6687 /* Allocate result buffer with its total size, avoid re-allocation and
6688 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6689 if (join_gap->ga_len >= 2)
6690 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6691 if (ga_grow(gap, sumlen + 2) == FAIL)
6692 return FAIL;
6693
6694 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6695 {
6696 if (first)
6697 first = FALSE;
6698 else
6699 ga_concat(gap, sep);
6700 p = ((join_T *)join_gap->ga_data) + i;
6701
6702 if (p->s != NULL)
6703 ga_concat(gap, p->s);
6704 line_breakcheck();
6705 }
6706
6707 return OK;
6708}
6709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006710/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006712 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006713 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006716list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006718 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006720 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006721 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 garray_T join_ga;
6724 int retval;
6725 join_T *p;
6726 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6729 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6730
6731 /* Dispose each item in join_ga. */
6732 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 p = (join_T *)join_ga.ga_data;
6735 for (i = 0; i < join_ga.ga_len; ++i)
6736 {
6737 vim_free(p->tofree);
6738 ++p;
6739 }
6740 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006742
6743 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744}
6745
6746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 * Garbage collection for lists and dictionaries.
6748 *
6749 * We use reference counts to be able to free most items right away when they
6750 * are no longer used. But for composite items it's possible that it becomes
6751 * unused while the reference count is > 0: When there is a recursive
6752 * reference. Example:
6753 * :let l = [1, 2, 3]
6754 * :let d = {9: l}
6755 * :let l[1] = d
6756 *
6757 * Since this is quite unusual we handle this with garbage collection: every
6758 * once in a while find out which lists and dicts are not referenced from any
6759 * variable.
6760 *
6761 * Here is a good reference text about garbage collection (refers to Python
6762 * but it applies to all reference-counting mechanisms):
6763 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 * Do garbage collection for lists and dicts.
6768 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 int
6771garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006773 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 buf_T *buf;
6775 win_T *wp;
6776 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006777 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 int did_free;
6779 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006780#ifdef FEAT_WINDOWS
6781 tabpage_T *tp;
6782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006784 /* Only do this once. */
6785 want_garbage_collect = FALSE;
6786 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006787 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006788
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006789 /* We advance by two because we add one for items referenced through
6790 * previous_funccal. */
6791 current_copyID += COPYID_INC;
6792 copyID = current_copyID;
6793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /*
6795 * 1. Go through all accessible variables and mark all lists and dicts
6796 * with copyID.
6797 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798
6799 /* Don't free variables in the previous_funccal list unless they are only
6800 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006801 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006802 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6803 {
6804 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6805 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6806 }
6807
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006808 /* script-local variables */
6809 for (i = 1; i <= ga_scripts.ga_len; ++i)
6810 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6811
6812 /* buffer-local variables */
6813 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006814 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006818 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006819#ifdef FEAT_AUTOCMD
6820 if (aucmd_win != NULL)
6821 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824#ifdef FEAT_WINDOWS
6825 /* tabpage-local variables */
6826 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006827 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828#endif
6829
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 /* global variables */
6831 set_ref_in_ht(&globvarht, copyID);
6832
6833 /* function-local variables */
6834 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6835 {
6836 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6837 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6838 }
6839
Bram Moolenaard812df62008-11-09 12:46:09 +00006840 /* v: vars */
6841 set_ref_in_ht(&vimvarht, copyID);
6842
Bram Moolenaar1dced572012-04-05 16:54:08 +02006843#ifdef FEAT_LUA
6844 set_ref_in_lua(copyID);
6845#endif
6846
Bram Moolenaardb913952012-06-29 12:54:53 +02006847#ifdef FEAT_PYTHON
6848 set_ref_in_python(copyID);
6849#endif
6850
6851#ifdef FEAT_PYTHON3
6852 set_ref_in_python3(copyID);
6853#endif
6854
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006855 /*
6856 * 2. Free lists and dictionaries that are not referenced.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 did_free = free_unref_items(copyID);
6859
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006860 /*
6861 * 3. Check if any funccal can be freed now.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 for (pfc = &previous_funccal; *pfc != NULL; )
6864 {
6865 if (can_free_funccal(*pfc, copyID))
6866 {
6867 fc = *pfc;
6868 *pfc = fc->caller;
6869 free_funccal(fc, TRUE);
6870 did_free = TRUE;
6871 did_free_funccal = TRUE;
6872 }
6873 else
6874 pfc = &(*pfc)->caller;
6875 }
6876 if (did_free_funccal)
6877 /* When a funccal was freed some more items might be garbage
6878 * collected, so run again. */
6879 (void)garbage_collect();
6880
6881 return did_free;
6882}
6883
6884/*
6885 * Free lists and dictionaries that are no longer referenced.
6886 */
6887 static int
6888free_unref_items(copyID)
6889 int copyID;
6890{
6891 dict_T *dd;
6892 list_T *ll;
6893 int did_free = FALSE;
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 */
6898 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006901 /* Free the Dictionary and ordinary items it contains, but don't
6902 * recurse into Lists and Dictionaries, they will be in the list
6903 * of dicts or list of lists. */
6904 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 did_free = TRUE;
6906
6907 /* restart, next dict may also have been freed */
6908 dd = first_dict;
6909 }
6910 else
6911 dd = dd->dv_used_next;
6912
6913 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 * Go through the list of lists and free items without the copyID.
6915 * But don't free a list that has a watcher (used in a for loop), these
6916 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 */
6918 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006919 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6920 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006922 /* Free the List and ordinary items it contains, but don't recurse
6923 * into Lists and Dictionaries, they will be in the list of dicts
6924 * or list of lists. */
6925 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 did_free = TRUE;
6927
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006928 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 ll = first_list;
6930 }
6931 else
6932 ll = ll->lv_used_next;
6933
6934 return did_free;
6935}
6936
6937/*
6938 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6939 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006940 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941set_ref_in_ht(ht, copyID)
6942 hashtab_T *ht;
6943 int copyID;
6944{
6945 int todo;
6946 hashitem_T *hi;
6947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006948 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 for (hi = ht->ht_array; todo > 0; ++hi)
6950 if (!HASHITEM_EMPTY(hi))
6951 {
6952 --todo;
6953 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6954 }
6955}
6956
6957/*
6958 * Mark all lists and dicts referenced through list "l" with "copyID".
6959 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006960 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961set_ref_in_list(l, copyID)
6962 list_T *l;
6963 int copyID;
6964{
6965 listitem_T *li;
6966
6967 for (li = l->lv_first; li != NULL; li = li->li_next)
6968 set_ref_in_item(&li->li_tv, copyID);
6969}
6970
6971/*
6972 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006974 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975set_ref_in_item(tv, copyID)
6976 typval_T *tv;
6977 int copyID;
6978{
6979 dict_T *dd;
6980 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981
6982 switch (tv->v_type)
6983 {
6984 case VAR_DICT:
6985 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this dict yet. */
6989 dd->dv_copyID = copyID;
6990 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993
6994 case VAR_LIST:
6995 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006996 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 /* Didn't see this list yet. */
6999 ll->lv_copyID = copyID;
7000 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005}
7006
7007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 * Allocate an empty header for a dictionary.
7009 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007010 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011dict_alloc()
7012{
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007017 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007018 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 if (first_dict != NULL)
7020 first_dict->dv_used_prev = d;
7021 d->dv_used_next = first_dict;
7022 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007027 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007031 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032}
7033
7034/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007035 * Allocate an empty dict for a return value.
7036 * Returns OK or FAIL.
7037 */
7038 static int
7039rettv_dict_alloc(rettv)
7040 typval_T *rettv;
7041{
7042 dict_T *d = dict_alloc();
7043
7044 if (d == NULL)
7045 return FAIL;
7046
7047 rettv->vval.v_dict = d;
7048 rettv->v_type = VAR_DICT;
7049 ++d->dv_refcount;
7050 return OK;
7051}
7052
7053
7054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 * Unreference a Dictionary: decrement the reference count and free it when it
7056 * becomes zero.
7057 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007058 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007062 if (d != NULL && --d->dv_refcount <= 0)
7063 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064}
7065
7066/*
7067 * Free a Dictionary, including all items it contains.
7068 * Ignores the reference count.
7069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007070 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007071dict_free(d, recurse)
7072 dict_T *d;
7073 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079 /* Remove the dict from the list of dicts for garbage collection. */
7080 if (d->dv_used_prev == NULL)
7081 first_dict = d->dv_used_next;
7082 else
7083 d->dv_used_prev->dv_used_next = d->dv_used_next;
7084 if (d->dv_used_next != NULL)
7085 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7086
7087 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007088 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007089 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (!HASHITEM_EMPTY(hi))
7093 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007094 /* Remove the item before deleting it, just in case there is
7095 * something recursive causing trouble. */
7096 di = HI2DI(hi);
7097 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007098 if (recurse || (di->di_tv.v_type != VAR_LIST
7099 && di->di_tv.v_type != VAR_DICT))
7100 clear_tv(&di->di_tv);
7101 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102 --todo;
7103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 vim_free(d);
7107}
7108
7109/*
7110 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 * The "key" is copied to the new item.
7112 * Note that the value of the item "di_tv" still needs to be initialized!
7113 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007115 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116dictitem_alloc(key)
7117 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118{
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007121 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 di->di_flags = 0;
7126 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 * Make a copy of a Dictionary item.
7132 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136{
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007139 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7140 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 if (di != NULL)
7142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007144 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 copy_tv(&org->di_tv, &di->di_tv);
7146 }
7147 return di;
7148}
7149
7150/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 * Remove item "item" from Dictionary "dict" and free it.
7152 */
7153 static void
7154dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 dict_T *dict;
7156 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157{
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 if (HASHITEM_EMPTY(hi))
7162 EMSG2(_(e_intern2), "dictitem_remove()");
7163 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 dictitem_free(item);
7166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Free a dict item. Also clears the value.
7170 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007171 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 clear_tv(&item->di_tv);
7176 vim_free(item);
7177}
7178
7179/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7181 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 * Returns NULL when out of memory.
7184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007188 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190{
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 dict_T *copy;
7192 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195
7196 if (orig == NULL)
7197 return NULL;
7198
7199 copy = dict_alloc();
7200 if (copy != NULL)
7201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007202 if (copyID != 0)
7203 {
7204 orig->dv_copyID = copyID;
7205 orig->dv_copydict = copy;
7206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007207 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 --todo;
7213
7214 di = dictitem_alloc(hi->hi_key);
7215 if (di == NULL)
7216 break;
7217 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 {
7219 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7220 copyID) == FAIL)
7221 {
7222 vim_free(di);
7223 break;
7224 }
7225 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 else
7227 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7228 if (dict_add(copy, di) == FAIL)
7229 {
7230 dictitem_free(di);
7231 break;
7232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 if (todo > 0)
7238 {
7239 dict_unref(copy);
7240 copy = NULL;
7241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 }
7243
7244 return copy;
7245}
7246
7247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007249 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007251 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 dict_T *d;
7254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255{
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257}
7258
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007260 * Add a number or string entry to dictionary "d".
7261 * When "str" is NULL use number "nr", otherwise use "str".
7262 * Returns FAIL when out of memory and when key already exists.
7263 */
7264 int
7265dict_add_nr_str(d, key, nr, str)
7266 dict_T *d;
7267 char *key;
7268 long nr;
7269 char_u *str;
7270{
7271 dictitem_T *item;
7272
7273 item = dictitem_alloc((char_u *)key);
7274 if (item == NULL)
7275 return FAIL;
7276 item->di_tv.v_lock = 0;
7277 if (str == NULL)
7278 {
7279 item->di_tv.v_type = VAR_NUMBER;
7280 item->di_tv.vval.v_number = nr;
7281 }
7282 else
7283 {
7284 item->di_tv.v_type = VAR_STRING;
7285 item->di_tv.vval.v_string = vim_strsave(str);
7286 }
7287 if (dict_add(d, item) == FAIL)
7288 {
7289 dictitem_free(item);
7290 return FAIL;
7291 }
7292 return OK;
7293}
7294
7295/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007296 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007297 * Returns FAIL when out of memory and when key already exists.
7298 */
7299 int
7300dict_add_list(d, key, list)
7301 dict_T *d;
7302 char *key;
7303 list_T *list;
7304{
7305 dictitem_T *item;
7306
7307 item = dictitem_alloc((char_u *)key);
7308 if (item == NULL)
7309 return FAIL;
7310 item->di_tv.v_lock = 0;
7311 item->di_tv.v_type = VAR_LIST;
7312 item->di_tv.vval.v_list = list;
7313 if (dict_add(d, item) == FAIL)
7314 {
7315 dictitem_free(item);
7316 return FAIL;
7317 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007318 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007319 return OK;
7320}
7321
7322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 * Get the number of items in a Dictionary.
7324 */
7325 static long
7326dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (d == NULL)
7330 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007331 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332}
7333
7334/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 * Find item "key[len]" in Dictionary "d".
7336 * If "len" is negative use strlen(key).
7337 * Returns NULL when not found.
7338 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007339 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 char_u *key;
7343 int len;
7344{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345#define AKEYLEN 200
7346 char_u buf[AKEYLEN];
7347 char_u *akey;
7348 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 if (len < 0)
7352 akey = key;
7353 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 tofree = akey = vim_strnsave(key, len);
7356 if (akey == NULL)
7357 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007358 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 else
7360 {
7361 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007362 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 akey = buf;
7364 }
7365
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 vim_free(tofree);
7368 if (HASHITEM_EMPTY(hi))
7369 return NULL;
7370 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371}
7372
7373/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007374 * Get a string item from a dictionary.
7375 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376 * Returns NULL if the entry doesn't exist or out of memory.
7377 */
7378 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007379get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007380 dict_T *d;
7381 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007383{
7384 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007386
7387 di = dict_find(d, key, -1);
7388 if (di == NULL)
7389 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007390 s = get_tv_string(&di->di_tv);
7391 if (save && s != NULL)
7392 s = vim_strsave(s);
7393 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007394}
7395
7396/*
7397 * Get a number item from a dictionary.
7398 * Returns 0 if the entry doesn't exist or out of memory.
7399 */
7400 long
7401get_dict_number(d, key)
7402 dict_T *d;
7403 char_u *key;
7404{
7405 dictitem_T *di;
7406
7407 di = dict_find(d, key, -1);
7408 if (di == NULL)
7409 return 0;
7410 return get_tv_number(&di->di_tv);
7411}
7412
7413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 * Return an allocated string with the string representation of a Dictionary.
7415 * May return NULL.
7416 */
7417 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007418dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421{
7422 garray_T ga;
7423 int first = TRUE;
7424 char_u *tofree;
7425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 return NULL;
7433 ga_init2(&ga, (int)sizeof(char), 80);
7434 ga_append(&ga, '{');
7435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007436 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 --todo;
7442
7443 if (first)
7444 first = FALSE;
7445 else
7446 ga_concat(&ga, (char_u *)", ");
7447
7448 tofree = string_quote(hi->hi_key, FALSE);
7449 if (tofree != NULL)
7450 {
7451 ga_concat(&ga, tofree);
7452 vim_free(tofree);
7453 }
7454 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 if (s != NULL)
7457 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 if (s == NULL)
7460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007463 if (todo > 0)
7464 {
7465 vim_free(ga.ga_data);
7466 return NULL;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 ga_append(&ga, '}');
7470 ga_append(&ga, NUL);
7471 return (char_u *)ga.ga_data;
7472}
7473
7474/*
7475 * Allocate a variable for a Dictionary and fill it from "*arg".
7476 * Return OK or FAIL. Returns NOTDONE for {expr}.
7477 */
7478 static int
7479get_dict_tv(arg, rettv, evaluate)
7480 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 int evaluate;
7483{
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dict_T *d = NULL;
7485 typval_T tvkey;
7486 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007487 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491
7492 /*
7493 * First check if it's not a curly-braces thing: {expr}.
7494 * Must do this without evaluating, otherwise a function may be called
7495 * twice. Unfortunately this means we need to call eval1() twice for the
7496 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 if (*start != '}')
7500 {
7501 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7502 return FAIL;
7503 if (*start == '}')
7504 return NOTDONE;
7505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
7507 if (evaluate)
7508 {
7509 d = dict_alloc();
7510 if (d == NULL)
7511 return FAIL;
7512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 tvkey.v_type = VAR_UNKNOWN;
7514 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
7516 *arg = skipwhite(*arg + 1);
7517 while (**arg != '}' && **arg != NUL)
7518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 goto failret;
7521 if (**arg != ':')
7522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007523 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 goto failret;
7526 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007527 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 key = get_tv_string_buf_chk(&tvkey, buf);
7530 if (key == NULL || *key == NUL)
7531 {
7532 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7533 if (key != NULL)
7534 EMSG(_(e_emptykey));
7535 clear_tv(&tvkey);
7536 goto failret;
7537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539
7540 *arg = skipwhite(*arg + 1);
7541 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7542 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007543 if (evaluate)
7544 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 if (evaluate)
7548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 if (item != NULL)
7551 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007552 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 clear_tv(&tv);
7555 goto failret;
7556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 item = dictitem_alloc(key);
7558 clear_tv(&tvkey);
7559 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 if (dict_add(d, item) == FAIL)
7564 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 }
7566 }
7567
7568 if (**arg == '}')
7569 break;
7570 if (**arg != ',')
7571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007572 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 goto failret;
7574 }
7575 *arg = skipwhite(*arg + 1);
7576 }
7577
7578 if (**arg != '}')
7579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581failret:
7582 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007583 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 return FAIL;
7585 }
7586
7587 *arg = skipwhite(*arg + 1);
7588 if (evaluate)
7589 {
7590 rettv->v_type = VAR_DICT;
7591 rettv->vval.v_dict = d;
7592 ++d->dv_refcount;
7593 }
7594
7595 return OK;
7596}
7597
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 * Return a string with the string representation of a variable.
7600 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007601 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007602 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007604 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 */
7606 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007610 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 static int recurse = 0;
7614 char_u *r = NULL;
7615
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007618 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 *tofree = NULL;
7620 return NULL;
7621 }
7622 ++recurse;
7623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007624 switch (tv->v_type)
7625 {
7626 case VAR_FUNC:
7627 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 r = tv->vval.v_string;
7629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632 if (tv->vval.v_list == NULL)
7633 {
7634 *tofree = NULL;
7635 r = NULL;
7636 }
7637 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7638 {
7639 *tofree = NULL;
7640 r = (char_u *)"[...]";
7641 }
7642 else
7643 {
7644 tv->vval.v_list->lv_copyID = copyID;
7645 *tofree = list2string(tv, copyID);
7646 r = *tofree;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651 if (tv->vval.v_dict == NULL)
7652 {
7653 *tofree = NULL;
7654 r = NULL;
7655 }
7656 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7657 {
7658 *tofree = NULL;
7659 r = (char_u *)"{...}";
7660 }
7661 else
7662 {
7663 tv->vval.v_dict->dv_copyID = copyID;
7664 *tofree = dict2string(tv, copyID);
7665 r = *tofree;
7666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669 case VAR_STRING:
7670 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 *tofree = NULL;
7672 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 case VAR_FLOAT:
7677 *tofree = NULL;
7678 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7679 r = numbuf;
7680 break;
7681#endif
7682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687
7688 --recurse;
7689 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690}
7691
7692/*
7693 * Return a string with the string representation of a variable.
7694 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7695 * "numbuf" is used for a number.
7696 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007697 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 */
7699 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 char_u **tofree;
7703 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007704 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705{
7706 switch (tv->v_type)
7707 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708 case VAR_FUNC:
7709 *tofree = string_quote(tv->vval.v_string, TRUE);
7710 return *tofree;
7711 case VAR_STRING:
7712 *tofree = string_quote(tv->vval.v_string, FALSE);
7713 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714#ifdef FEAT_FLOAT
7715 case VAR_FLOAT:
7716 *tofree = NULL;
7717 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7718 return numbuf;
7719#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728}
7729
7730/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 * Return string "str" in ' quotes, doubling ' characters.
7732 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007734 */
7735 static char_u *
7736string_quote(str, function)
7737 char_u *str;
7738 int function;
7739{
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 char_u *p, *r, *s;
7742
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 len = (function ? 13 : 3);
7744 if (str != NULL)
7745 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007746 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007747 for (p = str; *p != NUL; mb_ptr_adv(p))
7748 if (*p == '\'')
7749 ++len;
7750 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007751 s = r = alloc(len);
7752 if (r != NULL)
7753 {
7754 if (function)
7755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757 r += 10;
7758 }
7759 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 if (str != NULL)
7762 for (p = str; *p != NUL; )
7763 {
7764 if (*p == '\'')
7765 *r++ = '\'';
7766 MB_COPY_CHAR(p, r);
7767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 if (function)
7770 *r++ = ')';
7771 *r++ = NUL;
7772 }
7773 return s;
7774}
7775
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
7777/*
7778 * Convert the string "text" to a floating point number.
7779 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7780 * this always uses a decimal point.
7781 * Returns the length of the text that was consumed.
7782 */
7783 static int
7784string2float(text, value)
7785 char_u *text;
7786 float_T *value; /* result stored here */
7787{
7788 char *s = (char *)text;
7789 float_T f;
7790
7791 f = strtod(s, &s);
7792 *value = f;
7793 return (int)((char_u *)s - text);
7794}
7795#endif
7796
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * Get the value of an environment variable.
7799 * "arg" is pointing to the '$'. It is advanced to after the name.
7800 * If the environment variable was not set, silently assume it is empty.
7801 * Always return OK.
7802 */
7803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int evaluate;
7808{
7809 char_u *string = NULL;
7810 int len;
7811 int cc;
7812 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007813 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815 ++*arg;
7816 name = *arg;
7817 len = get_env_len(arg);
7818 if (evaluate)
7819 {
7820 if (len != 0)
7821 {
7822 cc = name[len];
7823 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824 /* first try vim_getenv(), fast for normal environment vars */
7825 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007827 {
7828 if (!mustfree)
7829 string = vim_strsave(string);
7830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 else
7832 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007833 if (mustfree)
7834 vim_free(string);
7835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 /* next try expanding things like $VIM and ${HOME} */
7837 string = expand_env_save(name - 1);
7838 if (string != NULL && *string == '$')
7839 {
7840 vim_free(string);
7841 string = NULL;
7842 }
7843 }
7844 name[len] = cc;
7845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849
7850 return OK;
7851}
7852
7853/*
7854 * Array with names and number of arguments of all internal functions
7855 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7856 */
7857static struct fst
7858{
7859 char *f_name; /* function name */
7860 char f_min_argc; /* minimal number of arguments */
7861 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007863 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864} functions[] =
7865{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
7867 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007868 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007870 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007871 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"append", 2, 2, f_append},
7873 {"argc", 0, 0, f_argc},
7874 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007875 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007877 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007879 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007882 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"bufexists", 1, 1, f_bufexists},
7884 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7885 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7886 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7887 {"buflisted", 1, 1, f_buflisted},
7888 {"bufloaded", 1, 1, f_bufloaded},
7889 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007890 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"bufwinnr", 1, 1, f_bufwinnr},
7892 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007893 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007894 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896#ifdef FEAT_FLOAT
7897 {"ceil", 1, 1, f_ceil},
7898#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007899 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007900 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007902 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007904#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007905 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007906 {"complete_add", 1, 1, f_complete_add},
7907 {"complete_check", 0, 0, f_complete_check},
7908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#ifdef FEAT_FLOAT
7912 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007913 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007917 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007918 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"delete", 1, 1, f_delete},
7920 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007921 {"diff_filler", 1, 1, f_diff_filler},
7922 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007923 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007925 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"eventhandler", 0, 0, f_eventhandler},
7927 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007928 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930#ifdef FEAT_FLOAT
7931 {"exp", 1, 1, f_exp},
7932#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007933 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007934 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007935 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7937 {"filereadable", 1, 1, f_filereadable},
7938 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007940 {"finddir", 1, 3, f_finddir},
7941 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#ifdef FEAT_FLOAT
7943 {"float2nr", 1, 1, f_float2nr},
7944 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007945 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007947 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"fnamemodify", 2, 2, f_fnamemodify},
7949 {"foldclosed", 1, 1, f_foldclosed},
7950 {"foldclosedend", 1, 1, f_foldclosedend},
7951 {"foldlevel", 1, 1, f_foldlevel},
7952 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007953 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007956 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007957 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007958 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007959 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"getchar", 0, 1, f_getchar},
7961 {"getcharmod", 0, 0, f_getcharmod},
7962 {"getcmdline", 0, 0, f_getcmdline},
7963 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007964 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007966 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007967 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getfsize", 1, 1, f_getfsize},
7969 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007971 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007972 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007973 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007974 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007975 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007976 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007977 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007979 {"gettabvar", 2, 3, f_gettabvar},
7980 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getwinposx", 0, 0, f_getwinposx},
7982 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007983 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007984 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007985 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007988 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007989 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7991 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7992 {"histadd", 2, 2, f_histadd},
7993 {"histdel", 1, 2, f_histdel},
7994 {"histget", 1, 2, f_histget},
7995 {"histnr", 1, 1, f_histnr},
7996 {"hlID", 1, 1, f_hlID},
7997 {"hlexists", 1, 1, f_hlexists},
7998 {"hostname", 0, 0, f_hostname},
7999 {"iconv", 3, 3, f_iconv},
8000 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008002 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008004 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"inputrestore", 0, 0, f_inputrestore},
8006 {"inputsave", 0, 0, f_inputsave},
8007 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008011 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008016 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"libcall", 3, 3, f_libcall},
8018 {"libcallnr", 3, 3, f_libcallnr},
8019 {"line", 1, 1, f_line},
8020 {"line2byte", 1, 1, f_line2byte},
8021 {"lispindent", 1, 1, f_lispindent},
8022 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008024 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025 {"log10", 1, 1, f_log10},
8026#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008027#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008028 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008029#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008031 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008032 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008034 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008035 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008036 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008037 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008038 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008039 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008040 {"max", 1, 1, f_max},
8041 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008042#ifdef vim_mkdir
8043 {"mkdir", 1, 3, f_mkdir},
8044#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008045 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008046#ifdef FEAT_MZSCHEME
8047 {"mzeval", 1, 1, f_mzeval},
8048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008050 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008051 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008052 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
8054 {"pow", 2, 2, f_pow},
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008057 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008058 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008059#ifdef FEAT_PYTHON3
8060 {"py3eval", 1, 1, f_py3eval},
8061#endif
8062#ifdef FEAT_PYTHON
8063 {"pyeval", 1, 1, f_pyeval},
8064#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008066 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008067 {"reltime", 0, 2, f_reltime},
8068 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"remote_expr", 2, 3, f_remote_expr},
8070 {"remote_foreground", 1, 1, f_remote_foreground},
8071 {"remote_peek", 1, 2, f_remote_peek},
8072 {"remote_read", 1, 1, f_remote_read},
8073 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008074 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008076 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008078 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"round", 1, 1, f_round},
8081#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008082 {"screenattr", 2, 2, f_screenattr},
8083 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008084 {"screencol", 0, 0, f_screencol},
8085 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008086 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008087 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008088 {"searchpair", 3, 7, f_searchpair},
8089 {"searchpairpos", 3, 7, f_searchpairpos},
8090 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"server2client", 2, 2, f_server2client},
8092 {"serverlist", 0, 0, f_serverlist},
8093 {"setbufvar", 3, 3, f_setbufvar},
8094 {"setcmdpos", 1, 1, f_setcmdpos},
8095 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008096 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008097 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008098 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008099 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008101 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008102 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008104#ifdef FEAT_CRYPT
8105 {"sha256", 1, 1, f_sha256},
8106#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008107 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008108 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110#ifdef FEAT_FLOAT
8111 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008112 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008114 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008115 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008116 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008117 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008118 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#ifdef FEAT_FLOAT
8120 {"sqrt", 1, 1, f_sqrt},
8121 {"str2float", 1, 1, f_str2float},
8122#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008123 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008125 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#ifdef HAVE_STRFTIME
8127 {"strftime", 1, 2, f_strftime},
8128#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"strlen", 1, 1, f_strlen},
8132 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008133 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008135 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008136 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"substitute", 4, 4, f_substitute},
8138 {"synID", 3, 3, f_synID},
8139 {"synIDattr", 2, 3, f_synIDattr},
8140 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008141 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008142 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008143 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008144 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008145 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008146 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008147 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008148 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008149 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150#ifdef FEAT_FLOAT
8151 {"tan", 1, 1, f_tan},
8152 {"tanh", 1, 1, f_tanh},
8153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008155 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"tolower", 1, 1, f_tolower},
8157 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008158 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159#ifdef FEAT_FLOAT
8160 {"trunc", 1, 1, f_trunc},
8161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008163 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008164 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008165 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008166 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"virtcol", 1, 1, f_virtcol},
8168 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008169 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"winbufnr", 1, 1, f_winbufnr},
8171 {"wincol", 0, 0, f_wincol},
8172 {"winheight", 1, 1, f_winheight},
8173 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008174 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008176 {"winrestview", 1, 1, f_winrestview},
8177 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008179 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008180 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181};
8182
8183#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8184
8185/*
8186 * Function given to ExpandGeneric() to obtain the list of internal
8187 * or user defined function names.
8188 */
8189 char_u *
8190get_function_name(xp, idx)
8191 expand_T *xp;
8192 int idx;
8193{
8194 static int intidx = -1;
8195 char_u *name;
8196
8197 if (idx == 0)
8198 intidx = -1;
8199 if (intidx < 0)
8200 {
8201 name = get_user_func_name(xp, idx);
8202 if (name != NULL)
8203 return name;
8204 }
8205 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8206 {
8207 STRCPY(IObuff, functions[intidx].f_name);
8208 STRCAT(IObuff, "(");
8209 if (functions[intidx].f_max_argc == 0)
8210 STRCAT(IObuff, ")");
8211 return IObuff;
8212 }
8213
8214 return NULL;
8215}
8216
8217/*
8218 * Function given to ExpandGeneric() to obtain the list of internal or
8219 * user defined variable or function names.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 char_u *
8222get_expr_name(xp, idx)
8223 expand_T *xp;
8224 int idx;
8225{
8226 static int intidx = -1;
8227 char_u *name;
8228
8229 if (idx == 0)
8230 intidx = -1;
8231 if (intidx < 0)
8232 {
8233 name = get_function_name(xp, idx);
8234 if (name != NULL)
8235 return name;
8236 }
8237 return get_user_var_name(xp, ++intidx);
8238}
8239
8240#endif /* FEAT_CMDL_COMPL */
8241
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008242#if defined(EBCDIC) || defined(PROTO)
8243/*
8244 * Compare struct fst by function name.
8245 */
8246 static int
8247compare_func_name(s1, s2)
8248 const void *s1;
8249 const void *s2;
8250{
8251 struct fst *p1 = (struct fst *)s1;
8252 struct fst *p2 = (struct fst *)s2;
8253
8254 return STRCMP(p1->f_name, p2->f_name);
8255}
8256
8257/*
8258 * Sort the function table by function name.
8259 * The sorting of the table above is ASCII dependant.
8260 * On machines using EBCDIC we have to sort it.
8261 */
8262 static void
8263sortFunctions()
8264{
8265 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8266
8267 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8268}
8269#endif
8270
8271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272/*
8273 * Find internal function in table above.
8274 * Return index, or -1 if not found
8275 */
8276 static int
8277find_internal_func(name)
8278 char_u *name; /* name of the function */
8279{
8280 int first = 0;
8281 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8282 int cmp;
8283 int x;
8284
8285 /*
8286 * Find the function name in the table. Binary search.
8287 */
8288 while (first <= last)
8289 {
8290 x = first + ((unsigned)(last - first) >> 1);
8291 cmp = STRCMP(name, functions[x].f_name);
8292 if (cmp < 0)
8293 last = x - 1;
8294 else if (cmp > 0)
8295 first = x + 1;
8296 else
8297 return x;
8298 }
8299 return -1;
8300}
8301
8302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8304 * name it contains, otherwise return "name".
8305 */
8306 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008307deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 char_u *name;
8309 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311{
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 int cc;
8314
8315 cc = name[*lenp];
8316 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008317 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 {
8323 *lenp = 0;
8324 return (char_u *)""; /* just in case */
8325 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008326 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008328 }
8329
8330 return name;
8331}
8332
8333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * Allocate a variable for the result of a function.
8335 * Return OK or FAIL.
8336 */
8337 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8339 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 char_u *name; /* name of the function */
8341 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u **arg; /* argument, pointing to the '(' */
8344 linenr_T firstline; /* first line of range */
8345 linenr_T lastline; /* last line of range */
8346 int *doesrange; /* return: function handled range */
8347 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 char_u *argp;
8351 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008352 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 int argcount = 0; /* number of arguments found */
8354
8355 /*
8356 * Get the arguments.
8357 */
8358 argp = *arg;
8359 while (argcount < MAX_FUNC_ARGS)
8360 {
8361 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8362 if (*argp == ')' || *argp == ',' || *argp == NUL)
8363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8365 {
8366 ret = FAIL;
8367 break;
8368 }
8369 ++argcount;
8370 if (*argp != ',')
8371 break;
8372 }
8373 if (*argp == ')')
8374 ++argp;
8375 else
8376 ret = FAIL;
8377
8378 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 {
8383 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008384 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008386 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 *arg = skipwhite(argp);
8393 return ret;
8394}
8395
8396
8397/*
8398 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008399 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008400 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 */
8402 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008403call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008404 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008405 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008409 typval_T *argvars; /* vars for arguments, must have "argcount"
8410 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 linenr_T firstline; /* first line of range */
8412 linenr_T lastline; /* last line of range */
8413 int *doesrange; /* return: function handled range */
8414 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#define ERROR_UNKNOWN 0
8419#define ERROR_TOOMANY 1
8420#define ERROR_TOOFEW 2
8421#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422#define ERROR_DICT 4
8423#define ERROR_NONE 5
8424#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 int error = ERROR_NONE;
8426 int i;
8427 int llen;
8428 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429#define FLEN_FIXED 40
8430 char_u fname_buf[FLEN_FIXED + 1];
8431 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008432 char_u *name;
8433
8434 /* Make a copy of the name, if it comes from a funcref variable it could
8435 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008436 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008437 if (name == NULL)
8438 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
8440 /*
8441 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8442 * Change <SNR>123_name() to K_SNR 123_name().
8443 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 llen = eval_fname_script(name);
8446 if (llen > 0)
8447 {
8448 fname_buf[0] = K_SPECIAL;
8449 fname_buf[1] = KS_EXTRA;
8450 fname_buf[2] = (int)KE_SNR;
8451 i = 3;
8452 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8453 {
8454 if (current_SID <= 0)
8455 error = ERROR_SCRIPT;
8456 else
8457 {
8458 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8459 i = (int)STRLEN(fname_buf);
8460 }
8461 }
8462 if (i + STRLEN(name + llen) < FLEN_FIXED)
8463 {
8464 STRCPY(fname_buf + i, name + llen);
8465 fname = fname_buf;
8466 }
8467 else
8468 {
8469 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8470 if (fname == NULL)
8471 error = ERROR_OTHER;
8472 else
8473 {
8474 mch_memmove(fname, fname_buf, (size_t)i);
8475 STRCPY(fname + i, name + llen);
8476 }
8477 }
8478 }
8479 else
8480 fname = name;
8481
8482 *doesrange = FALSE;
8483
8484
8485 /* execute the function if no errors detected and executing */
8486 if (evaluate && error == ERROR_NONE)
8487 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008488 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 error = ERROR_UNKNOWN;
8491
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008492 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 /*
8495 * User defined function.
8496 */
8497 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008500 /* Trigger FuncUndefined event, may load the function. */
8501 if (fp == NULL
8502 && apply_autocmds(EVENT_FUNCUNDEFINED,
8503 fname, fname, TRUE, NULL)
8504 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008506 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 fp = find_func(fname);
8508 }
8509#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008510 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008511 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008512 {
8513 /* loaded a package, search for the function again */
8514 fp = find_func(fname);
8515 }
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (fp != NULL)
8518 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008519 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008521 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008523 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008525 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 else
8528 {
8529 /*
8530 * Call the user function.
8531 * Save and restore search patterns, script variables and
8532 * redo buffer.
8533 */
8534 save_search_patterns();
8535 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008536 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008539 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8540 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8541 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008542 /* Function was unreferenced while being used, free it
8543 * now. */
8544 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 restoreRedobuff();
8546 restore_search_patterns();
8547 error = ERROR_NONE;
8548 }
8549 }
8550 }
8551 else
8552 {
8553 /*
8554 * Find the function name in the table, call its implementation.
8555 */
8556 i = find_internal_func(fname);
8557 if (i >= 0)
8558 {
8559 if (argcount < functions[i].f_min_argc)
8560 error = ERROR_TOOFEW;
8561 else if (argcount > functions[i].f_max_argc)
8562 error = ERROR_TOOMANY;
8563 else
8564 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 error = ERROR_NONE;
8568 }
8569 }
8570 }
8571 /*
8572 * The function call (or "FuncUndefined" autocommand sequence) might
8573 * have been aborted by an error, an interrupt, or an explicitly thrown
8574 * exception that has not been caught so far. This situation can be
8575 * tested for by calling aborting(). For an error in an internal
8576 * function or for the "E132" error in call_user_func(), however, the
8577 * throw point at which the "force_abort" flag (temporarily reset by
8578 * emsg()) is normally updated has not been reached yet. We need to
8579 * update that flag first to make aborting() reliable.
8580 */
8581 update_force_abort();
8582 }
8583 if (error == ERROR_NONE)
8584 ret = OK;
8585
8586 /*
8587 * Report an error unless the argument evaluation or function call has been
8588 * cancelled due to an aborting error, an interrupt, or an exception.
8589 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008590 if (!aborting())
8591 {
8592 switch (error)
8593 {
8594 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008595 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008596 break;
8597 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008598 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 break;
8600 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008601 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008602 name);
8603 break;
8604 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008605 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008606 name);
8607 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008609 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610 name);
8611 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008612 }
8613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (fname != name && fname != fname_buf)
8616 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008617 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 return ret;
8620}
8621
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008622/*
8623 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008624 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 */
8626 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008627emsg_funcname(ermsg, name)
8628 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008629 char_u *name;
8630{
8631 char_u *p;
8632
8633 if (*name == K_SPECIAL)
8634 p = concat_str((char_u *)"<SNR>", name + 3);
8635 else
8636 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008637 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008638 if (p != name)
8639 vim_free(p);
8640}
8641
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008642/*
8643 * Return TRUE for a non-zero Number and a non-empty String.
8644 */
8645 static int
8646non_zero_arg(argvars)
8647 typval_T *argvars;
8648{
8649 return ((argvars[0].v_type == VAR_NUMBER
8650 && argvars[0].vval.v_number != 0)
8651 || (argvars[0].v_type == VAR_STRING
8652 && argvars[0].vval.v_string != NULL
8653 && *argvars[0].vval.v_string != NUL));
8654}
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656/*********************************************
8657 * Implementation of the built-in functions
8658 */
8659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008661static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8662
8663/*
8664 * Get the float value of "argvars[0]" into "f".
8665 * Returns FAIL when the argument is not a Number or Float.
8666 */
8667 static int
8668get_float_arg(argvars, f)
8669 typval_T *argvars;
8670 float_T *f;
8671{
8672 if (argvars[0].v_type == VAR_FLOAT)
8673 {
8674 *f = argvars[0].vval.v_float;
8675 return OK;
8676 }
8677 if (argvars[0].v_type == VAR_NUMBER)
8678 {
8679 *f = (float_T)argvars[0].vval.v_number;
8680 return OK;
8681 }
8682 EMSG(_("E808: Number or Float required"));
8683 return FAIL;
8684}
8685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686/*
8687 * "abs(expr)" function
8688 */
8689 static void
8690f_abs(argvars, rettv)
8691 typval_T *argvars;
8692 typval_T *rettv;
8693{
8694 if (argvars[0].v_type == VAR_FLOAT)
8695 {
8696 rettv->v_type = VAR_FLOAT;
8697 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8698 }
8699 else
8700 {
8701 varnumber_T n;
8702 int error = FALSE;
8703
8704 n = get_tv_number_chk(&argvars[0], &error);
8705 if (error)
8706 rettv->vval.v_number = -1;
8707 else if (n > 0)
8708 rettv->vval.v_number = n;
8709 else
8710 rettv->vval.v_number = -n;
8711 }
8712}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008713
8714/*
8715 * "acos()" function
8716 */
8717 static void
8718f_acos(argvars, rettv)
8719 typval_T *argvars;
8720 typval_T *rettv;
8721{
8722 float_T f;
8723
8724 rettv->v_type = VAR_FLOAT;
8725 if (get_float_arg(argvars, &f) == OK)
8726 rettv->vval.v_float = acos(f);
8727 else
8728 rettv->vval.v_float = 0.0;
8729}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008730#endif
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 */
8735 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008743 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008745 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008746 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008747 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 }
8750 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751 EMSG(_(e_listreq));
8752}
8753
8754/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008755 * "and(expr, expr)" function
8756 */
8757 static void
8758f_and(argvars, rettv)
8759 typval_T *argvars;
8760 typval_T *rettv;
8761{
8762 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8763 & get_tv_number_chk(&argvars[1], NULL);
8764}
8765
8766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008767 * "append(lnum, string/list)" function
8768 */
8769 static void
8770f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773{
8774 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 list_T *l = NULL;
8777 listitem_T *li = NULL;
8778 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008779 long added = 0;
8780
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008781 /* When coming here from Insert mode, sync undo, so that this can be
8782 * undone separately from what was previously inserted. */
8783 if (u_sync_once == 2)
8784 {
8785 u_sync_once = 1; /* notify that u_sync() was called */
8786 u_sync(TRUE);
8787 }
8788
Bram Moolenaar0d660222005-01-07 21:51:51 +00008789 lnum = get_tv_lnum(argvars);
8790 if (lnum >= 0
8791 && lnum <= curbuf->b_ml.ml_line_count
8792 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008793 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008794 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796 l = argvars[1].vval.v_list;
8797 if (l == NULL)
8798 return;
8799 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801 for (;;)
8802 {
8803 if (l == NULL)
8804 tv = &argvars[1]; /* append a string */
8805 else if (li == NULL)
8806 break; /* end of list */
8807 else
8808 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008809 line = get_tv_string_chk(tv);
8810 if (line == NULL) /* type error */
8811 {
8812 rettv->vval.v_number = 1; /* Failed */
8813 break;
8814 }
8815 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008816 ++added;
8817 if (l == NULL)
8818 break;
8819 li = li->li_next;
8820 }
8821
8822 appended_lines_mark(lnum, added);
8823 if (curwin->w_cursor.lnum > lnum)
8824 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 else
8827 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
8831 * "argc()" function
8832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841/*
8842 * "argidx()" function
8843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "argv(nr)" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 int idx;
8861
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008862 if (argvars[0].v_type != VAR_UNKNOWN)
8863 {
8864 idx = get_tv_number_chk(&argvars[0], NULL);
8865 if (idx >= 0 && idx < ARGCOUNT)
8866 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8867 else
8868 rettv->vval.v_string = NULL;
8869 rettv->v_type = VAR_STRING;
8870 }
8871 else if (rettv_list_alloc(rettv) == OK)
8872 for (idx = 0; idx < ARGCOUNT; ++idx)
8873 list_append_string(rettv->vval.v_list,
8874 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875}
8876
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008877#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881 static void
8882f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008883 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008884 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008886 float_T f;
8887
8888 rettv->v_type = VAR_FLOAT;
8889 if (get_float_arg(argvars, &f) == OK)
8890 rettv->vval.v_float = asin(f);
8891 else
8892 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893}
8894
8895/*
8896 * "atan()" function
8897 */
8898 static void
8899f_atan(argvars, rettv)
8900 typval_T *argvars;
8901 typval_T *rettv;
8902{
8903 float_T f;
8904
8905 rettv->v_type = VAR_FLOAT;
8906 if (get_float_arg(argvars, &f) == OK)
8907 rettv->vval.v_float = atan(f);
8908 else
8909 rettv->vval.v_float = 0.0;
8910}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008911
8912/*
8913 * "atan2()" function
8914 */
8915 static void
8916f_atan2(argvars, rettv)
8917 typval_T *argvars;
8918 typval_T *rettv;
8919{
8920 float_T fx, fy;
8921
8922 rettv->v_type = VAR_FLOAT;
8923 if (get_float_arg(argvars, &fx) == OK
8924 && get_float_arg(&argvars[1], &fy) == OK)
8925 rettv->vval.v_float = atan2(fx, fy);
8926 else
8927 rettv->vval.v_float = 0.0;
8928}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929#endif
8930
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931/*
8932 * "browse(save, title, initdir, default)" function
8933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
8939#ifdef FEAT_BROWSE
8940 int save;
8941 char_u *title;
8942 char_u *initdir;
8943 char_u *defname;
8944 char_u buf[NUMBUFLEN];
8945 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 save = get_tv_number_chk(&argvars[0], &error);
8949 title = get_tv_string_chk(&argvars[1]);
8950 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8951 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008953 if (error || title == NULL || initdir == NULL || defname == NULL)
8954 rettv->vval.v_string = NULL;
8955 else
8956 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008957 do_browse(save ? BROWSE_SAVE : 0,
8958 title, defname, NULL, initdir, NULL, curbuf);
8959#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008961#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963}
8964
8965/*
8966 * "browsedir(title, initdir)" function
8967 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972{
8973#ifdef FEAT_BROWSE
8974 char_u *title;
8975 char_u *initdir;
8976 char_u buf[NUMBUFLEN];
8977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 title = get_tv_string_chk(&argvars[0]);
8979 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 if (title == NULL || initdir == NULL)
8982 rettv->vval.v_string = NULL;
8983 else
8984 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008985 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
Bram Moolenaar33570922005-01-25 22:26:29 +00008992static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*
8995 * Find a buffer by number or exact name.
8996 */
8997 static buf_T *
8998find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 if (avar->v_type == VAR_NUMBER)
9004 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009005 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009008 if (buf == NULL)
9009 {
9010 /* No full path name match, try a match with a URL or a "nofile"
9011 * buffer, these don't use the full path. */
9012 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9013 if (buf->b_fname != NULL
9014 && (path_with_url(buf->b_fname)
9015#ifdef FEAT_QUICKFIX
9016 || bt_nofile(buf)
9017#endif
9018 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009019 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009020 break;
9021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023 return buf;
9024}
9025
9026/*
9027 * "bufexists(expr)" function
9028 */
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035}
9036
9037/*
9038 * "buflisted(expr)" function
9039 */
9040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *argvars;
9043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 buf_T *buf;
9046
9047 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
9051/*
9052 * "bufloaded(expr)" function
9053 */
9054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 typval_T *argvars;
9057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 buf_T *buf;
9060
9061 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009065static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067/*
9068 * Get buffer by number or pattern.
9069 */
9070 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009071get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009072 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009073 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 int save_magic;
9077 char_u *save_cpo;
9078 buf_T *buf;
9079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 if (tv->v_type == VAR_NUMBER)
9081 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009082 if (tv->v_type != VAR_STRING)
9083 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 if (name == NULL || *name == NUL)
9085 return curbuf;
9086 if (name[0] == '$' && name[1] == NUL)
9087 return lastbuf;
9088
9089 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9090 save_magic = p_magic;
9091 p_magic = TRUE;
9092 save_cpo = p_cpo;
9093 p_cpo = (char_u *)"";
9094
9095 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009096 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
9098 p_magic = save_magic;
9099 p_cpo = save_cpo;
9100
9101 /* If not found, try expanding the name, like done for bufexists(). */
9102 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 return buf;
9106}
9107
9108/*
9109 * "bufname(expr)" function
9110 */
9111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116 buf_T *buf;
9117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009120 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 --emsg_off;
9127}
9128
9129/*
9130 * "bufnr(expr)" function
9131 */
9132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009138 int error = FALSE;
9139 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009143 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009144 --emsg_off;
9145
9146 /* If the buffer isn't found and the second argument is not zero create a
9147 * new buffer. */
9148 if (buf == NULL
9149 && argvars[1].v_type != VAR_UNKNOWN
9150 && get_tv_number_chk(&argvars[1], &error) != 0
9151 && !error
9152 && (name = get_tv_string_chk(&argvars[0])) != NULL
9153 && !error)
9154 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
9161
9162/*
9163 * "bufwinnr(nr)" function
9164 */
9165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170#ifdef FEAT_WINDOWS
9171 win_T *wp;
9172 int winnr = 0;
9173#endif
9174 buf_T *buf;
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009178 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#ifdef FEAT_WINDOWS
9180 for (wp = firstwin; wp; wp = wp->w_next)
9181 {
9182 ++winnr;
9183 if (wp->w_buffer == buf)
9184 break;
9185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#endif
9190 --emsg_off;
9191}
9192
9193/*
9194 * "byte2line(byte)" function
9195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#else
9204 long boff = 0;
9205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 (linenr_T)0, &boff);
9212#endif
9213}
9214
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009215 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009216byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009217 typval_T *argvars;
9218 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009219 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009220{
9221#ifdef FEAT_MBYTE
9222 char_u *t;
9223#endif
9224 char_u *str;
9225 long idx;
9226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 str = get_tv_string_chk(&argvars[0]);
9228 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009231 return;
9232
9233#ifdef FEAT_MBYTE
9234 t = str;
9235 for ( ; idx > 0; idx--)
9236 {
9237 if (*t == NUL) /* EOL reached */
9238 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009239 if (enc_utf8 && comp)
9240 t += utf_ptr2len(t);
9241 else
9242 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009243 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009244 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009246 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009248#endif
9249}
9250
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009251/*
9252 * "byteidx()" function
9253 */
9254 static void
9255f_byteidx(argvars, rettv)
9256 typval_T *argvars;
9257 typval_T *rettv;
9258{
9259 byteidx(argvars, rettv, FALSE);
9260}
9261
9262/*
9263 * "byteidxcomp()" function
9264 */
9265 static void
9266f_byteidxcomp(argvars, rettv)
9267 typval_T *argvars;
9268 typval_T *rettv;
9269{
9270 byteidx(argvars, rettv, TRUE);
9271}
9272
Bram Moolenaardb913952012-06-29 12:54:53 +02009273 int
9274func_call(name, args, selfdict, rettv)
9275 char_u *name;
9276 typval_T *args;
9277 dict_T *selfdict;
9278 typval_T *rettv;
9279{
9280 listitem_T *item;
9281 typval_T argv[MAX_FUNC_ARGS + 1];
9282 int argc = 0;
9283 int dummy;
9284 int r = 0;
9285
9286 for (item = args->vval.v_list->lv_first; item != NULL;
9287 item = item->li_next)
9288 {
9289 if (argc == MAX_FUNC_ARGS)
9290 {
9291 EMSG(_("E699: Too many arguments"));
9292 break;
9293 }
9294 /* Make a copy of each argument. This is needed to be able to set
9295 * v_lock to VAR_FIXED in the copy without changing the original list.
9296 */
9297 copy_tv(&item->li_tv, &argv[argc++]);
9298 }
9299
9300 if (item == NULL)
9301 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9302 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9303 &dummy, TRUE, selfdict);
9304
9305 /* Free the arguments. */
9306 while (argc > 0)
9307 clear_tv(&argv[--argc]);
9308
9309 return r;
9310}
9311
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009312/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009313 * "call(func, arglist)" function
9314 */
9315 static void
9316f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319{
9320 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009321 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009323 if (argvars[1].v_type != VAR_LIST)
9324 {
9325 EMSG(_(e_listreq));
9326 return;
9327 }
9328 if (argvars[1].vval.v_list == NULL)
9329 return;
9330
9331 if (argvars[0].v_type == VAR_FUNC)
9332 func = argvars[0].vval.v_string;
9333 else
9334 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 if (*func == NUL)
9336 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009337
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 if (argvars[2].v_type != VAR_UNKNOWN)
9339 {
9340 if (argvars[2].v_type != VAR_DICT)
9341 {
9342 EMSG(_(e_dictreq));
9343 return;
9344 }
9345 selfdict = argvars[2].vval.v_dict;
9346 }
9347
Bram Moolenaardb913952012-06-29 12:54:53 +02009348 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009349}
9350
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009351#ifdef FEAT_FLOAT
9352/*
9353 * "ceil({float})" function
9354 */
9355 static void
9356f_ceil(argvars, rettv)
9357 typval_T *argvars;
9358 typval_T *rettv;
9359{
9360 float_T f;
9361
9362 rettv->v_type = VAR_FLOAT;
9363 if (get_float_arg(argvars, &f) == OK)
9364 rettv->vval.v_float = ceil(f);
9365 else
9366 rettv->vval.v_float = 0.0;
9367}
9368#endif
9369
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009370/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009371 * "changenr()" function
9372 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009373 static void
9374f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009375 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009376 typval_T *rettv;
9377{
9378 rettv->vval.v_number = curbuf->b_u_seq_cur;
9379}
9380
9381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 * "char2nr(string)" function
9383 */
9384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 typval_T *argvars;
9387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
9389#ifdef FEAT_MBYTE
9390 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009391 {
9392 int utf8 = 0;
9393
9394 if (argvars[1].v_type != VAR_UNKNOWN)
9395 utf8 = get_tv_number_chk(&argvars[1], NULL);
9396
9397 if (utf8)
9398 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9399 else
9400 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 else
9403#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405}
9406
9407/*
9408 * "cindent(lnum)" function
9409 */
9410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415#ifdef FEAT_CINDENT
9416 pos_T pos;
9417 linenr_T lnum;
9418
9419 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9422 {
9423 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 curwin->w_cursor = pos;
9426 }
9427 else
9428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
9432/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009433 * "clearmatches()" function
9434 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009435 static void
9436f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009437 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009439{
9440#ifdef FEAT_SEARCH_EXTRA
9441 clear_matches(curwin);
9442#endif
9443}
9444
9445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 * "col(string)" function
9447 */
9448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 colnr_T col = 0;
9454 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009455 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009457 fp = var2fpos(&argvars[0], FALSE, &fnum);
9458 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 {
9460 if (fp->col == MAXCOL)
9461 {
9462 /* '> can be MAXCOL, get the length of the line then */
9463 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009464 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 else
9466 col = MAXCOL;
9467 }
9468 else
9469 {
9470 col = fp->col + 1;
9471#ifdef FEAT_VIRTUALEDIT
9472 /* col(".") when the cursor is on the NUL at the end of the line
9473 * because of "coladd" can be seen as an extra column. */
9474 if (virtual_active() && fp == &curwin->w_cursor)
9475 {
9476 char_u *p = ml_get_cursor();
9477
9478 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9479 curwin->w_virtcol - curwin->w_cursor.coladd))
9480 {
9481# ifdef FEAT_MBYTE
9482 int l;
9483
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009484 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 col += l;
9486# else
9487 if (*p != NUL && p[1] == NUL)
9488 ++col;
9489# endif
9490 }
9491 }
9492#endif
9493 }
9494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
Bram Moolenaar572cb562005-08-05 21:35:02 +00009498#if defined(FEAT_INS_EXPAND)
9499/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009500 * "complete()" function
9501 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009502 static void
9503f_complete(argvars, rettv)
9504 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009505 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009506{
9507 int startcol;
9508
9509 if ((State & INSERT) == 0)
9510 {
9511 EMSG(_("E785: complete() can only be used in Insert mode"));
9512 return;
9513 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009514
9515 /* Check for undo allowed here, because if something was already inserted
9516 * the line was already saved for undo and this check isn't done. */
9517 if (!undo_allowed())
9518 return;
9519
Bram Moolenaarade00832006-03-10 21:46:58 +00009520 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9521 {
9522 EMSG(_(e_invarg));
9523 return;
9524 }
9525
9526 startcol = get_tv_number_chk(&argvars[0], NULL);
9527 if (startcol <= 0)
9528 return;
9529
9530 set_completion(startcol - 1, argvars[1].vval.v_list);
9531}
9532
9533/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009534 * "complete_add()" function
9535 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009536 static void
9537f_complete_add(argvars, rettv)
9538 typval_T *argvars;
9539 typval_T *rettv;
9540{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009541 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009542}
9543
9544/*
9545 * "complete_check()" function
9546 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009547 static void
9548f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009549 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009550 typval_T *rettv;
9551{
9552 int saved = RedrawingDisabled;
9553
9554 RedrawingDisabled = 0;
9555 ins_compl_check_keys(0);
9556 rettv->vval.v_number = compl_interrupted;
9557 RedrawingDisabled = saved;
9558}
9559#endif
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561/*
9562 * "confirm(message, buttons[, default [, type]])" function
9563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009566 typval_T *argvars UNUSED;
9567 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9570 char_u *message;
9571 char_u *buttons = NULL;
9572 char_u buf[NUMBUFLEN];
9573 char_u buf2[NUMBUFLEN];
9574 int def = 1;
9575 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 char_u *typestr;
9577 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 message = get_tv_string_chk(&argvars[0]);
9580 if (message == NULL)
9581 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9585 if (buttons == NULL)
9586 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9593 if (typestr == NULL)
9594 error = TRUE;
9595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 switch (TOUPPER_ASC(*typestr))
9598 {
9599 case 'E': type = VIM_ERROR; break;
9600 case 'Q': type = VIM_QUESTION; break;
9601 case 'I': type = VIM_INFO; break;
9602 case 'W': type = VIM_WARNING; break;
9603 case 'G': type = VIM_GENERIC; break;
9604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 }
9606 }
9607 }
9608 }
9609
9610 if (buttons == NULL || *buttons == NUL)
9611 buttons = (char_u *)_("&Ok");
9612
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009613 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009615 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616#endif
9617}
9618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619/*
9620 * "copy()" function
9621 */
9622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *argvars;
9625 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009626{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009627 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009630#ifdef FEAT_FLOAT
9631/*
9632 * "cos()" function
9633 */
9634 static void
9635f_cos(argvars, rettv)
9636 typval_T *argvars;
9637 typval_T *rettv;
9638{
9639 float_T f;
9640
9641 rettv->v_type = VAR_FLOAT;
9642 if (get_float_arg(argvars, &f) == OK)
9643 rettv->vval.v_float = cos(f);
9644 else
9645 rettv->vval.v_float = 0.0;
9646}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009647
9648/*
9649 * "cosh()" function
9650 */
9651 static void
9652f_cosh(argvars, rettv)
9653 typval_T *argvars;
9654 typval_T *rettv;
9655{
9656 float_T f;
9657
9658 rettv->v_type = VAR_FLOAT;
9659 if (get_float_arg(argvars, &f) == OK)
9660 rettv->vval.v_float = cosh(f);
9661 else
9662 rettv->vval.v_float = 0.0;
9663}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009664#endif
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 * "count()" function
9668 */
9669 static void
9670f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *argvars;
9672 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 long n = 0;
9675 int ic = FALSE;
9676
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 listitem_T *li;
9680 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009682
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 if ((l = argvars[0].vval.v_list) != NULL)
9684 {
9685 li = l->lv_first;
9686 if (argvars[2].v_type != VAR_UNKNOWN)
9687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 int error = FALSE;
9689
9690 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009691 if (argvars[3].v_type != VAR_UNKNOWN)
9692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 idx = get_tv_number_chk(&argvars[3], &error);
9694 if (!error)
9695 {
9696 li = list_find(l, idx);
9697 if (li == NULL)
9698 EMSGN(_(e_listidx), idx);
9699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009701 if (error)
9702 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009703 }
9704
9705 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009706 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 ++n;
9708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 else if (argvars[0].v_type == VAR_DICT)
9711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 int todo;
9713 dict_T *d;
9714 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 if ((d = argvars[0].vval.v_dict) != NULL)
9717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 int error = FALSE;
9719
Bram Moolenaare9a41262005-01-15 22:18:47 +00009720 if (argvars[2].v_type != VAR_UNKNOWN)
9721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009723 if (argvars[3].v_type != VAR_UNKNOWN)
9724 EMSG(_(e_invarg));
9725 }
9726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009727 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009729 {
9730 if (!HASHITEM_EMPTY(hi))
9731 {
9732 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009733 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009734 ++n;
9735 }
9736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009737 }
9738 }
9739 else
9740 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009741 rettv->vval.v_number = n;
9742}
9743
9744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9746 *
9747 * Checks the existence of a cscope connection.
9748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009751 typval_T *argvars UNUSED;
9752 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754#ifdef FEAT_CSCOPE
9755 int num = 0;
9756 char_u *dbpath = NULL;
9757 char_u *prepend = NULL;
9758 char_u buf[NUMBUFLEN];
9759
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 if (argvars[0].v_type != VAR_UNKNOWN
9761 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 num = (int)get_tv_number(&argvars[0]);
9764 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 }
9768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#endif
9771}
9772
9773/*
9774 * "cursor(lnum, col)" function
9775 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 * Moves the cursor to the specified line and column.
9777 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785#ifdef FEAT_VIRTUALEDIT
9786 long coladd = 0;
9787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009789 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009790 if (argvars[1].v_type == VAR_UNKNOWN)
9791 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009792 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009793
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009794 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009795 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009796 line = pos.lnum;
9797 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009798#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009799 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009800#endif
9801 }
9802 else
9803 {
9804 line = get_tv_lnum(argvars);
9805 col = get_tv_number_chk(&argvars[1], NULL);
9806#ifdef FEAT_VIRTUALEDIT
9807 if (argvars[2].v_type != VAR_UNKNOWN)
9808 coladd = get_tv_number_chk(&argvars[2], NULL);
9809#endif
9810 }
9811 if (line < 0 || col < 0
9812#ifdef FEAT_VIRTUALEDIT
9813 || coladd < 0
9814#endif
9815 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 if (line > 0)
9818 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (col > 0)
9820 curwin->w_cursor.col = col - 1;
9821#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009822 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#endif
9824
9825 /* Make sure the cursor is in a valid position. */
9826 check_cursor();
9827#ifdef FEAT_MBYTE
9828 /* Correct cursor for multi-byte character. */
9829 if (has_mbyte)
9830 mb_adjust_cursor();
9831#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009832
9833 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009834 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
9837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009838 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 */
9840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009845 int noref = 0;
9846
9847 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009849 if (noref < 0 || noref > 1)
9850 EMSG(_(e_invarg));
9851 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009852 {
9853 current_copyID += COPYID_INC;
9854 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * "delete()" function
9860 */
9861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 typval_T *argvars;
9864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870}
9871
9872/*
9873 * "did_filetype()" function
9874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009877 typval_T *argvars UNUSED;
9878 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#endif
9883}
9884
9885/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009886 * "diff_filler()" function
9887 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009890 typval_T *argvars UNUSED;
9891 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892{
9893#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895#endif
9896}
9897
9898/*
9899 * "diff_hlID()" function
9900 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009903 typval_T *argvars UNUSED;
9904 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009905{
9906#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009908 static linenr_T prev_lnum = 0;
9909 static int changedtick = 0;
9910 static int fnum = 0;
9911 static int change_start = 0;
9912 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009913 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914 int filler_lines;
9915 int col;
9916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 if (lnum < 0) /* ignore type error in {lnum} arg */
9918 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009919 if (lnum != prev_lnum
9920 || changedtick != curbuf->b_changedtick
9921 || fnum != curbuf->b_fnum)
9922 {
9923 /* New line, buffer, change: need to get the values. */
9924 filler_lines = diff_check(curwin, lnum);
9925 if (filler_lines < 0)
9926 {
9927 if (filler_lines == -1)
9928 {
9929 change_start = MAXCOL;
9930 change_end = -1;
9931 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9932 hlID = HLF_ADD; /* added line */
9933 else
9934 hlID = HLF_CHD; /* changed line */
9935 }
9936 else
9937 hlID = HLF_ADD; /* added line */
9938 }
9939 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009940 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009941 prev_lnum = lnum;
9942 changedtick = curbuf->b_changedtick;
9943 fnum = curbuf->b_fnum;
9944 }
9945
9946 if (hlID == HLF_CHD || hlID == HLF_TXD)
9947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009949 if (col >= change_start && col <= change_end)
9950 hlID = HLF_TXD; /* changed text */
9951 else
9952 hlID = HLF_CHD; /* changed line */
9953 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009954 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009955#endif
9956}
9957
9958/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009959 * "empty({expr})" function
9960 */
9961 static void
9962f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009965{
9966 int n;
9967
9968 switch (argvars[0].v_type)
9969 {
9970 case VAR_STRING:
9971 case VAR_FUNC:
9972 n = argvars[0].vval.v_string == NULL
9973 || *argvars[0].vval.v_string == NUL;
9974 break;
9975 case VAR_NUMBER:
9976 n = argvars[0].vval.v_number == 0;
9977 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009978#ifdef FEAT_FLOAT
9979 case VAR_FLOAT:
9980 n = argvars[0].vval.v_float == 0.0;
9981 break;
9982#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009983 case VAR_LIST:
9984 n = argvars[0].vval.v_list == NULL
9985 || argvars[0].vval.v_list->lv_first == NULL;
9986 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 case VAR_DICT:
9988 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009991 default:
9992 EMSG2(_(e_intern2), "f_empty()");
9993 n = 0;
9994 }
9995
9996 rettv->vval.v_number = n;
9997}
9998
9999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 * "escape({string}, {chars})" function
10001 */
10002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 typval_T *argvars;
10005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 char_u buf[NUMBUFLEN];
10008
Bram Moolenaar758711c2005-02-02 23:11:38 +000010009 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10010 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012}
10013
10014/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010015 * "eval()" function
10016 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010017 static void
10018f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010019 typval_T *argvars;
10020 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010021{
10022 char_u *s;
10023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010024 s = get_tv_string_chk(&argvars[0]);
10025 if (s != NULL)
10026 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10029 {
10030 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010031 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010033 else if (*s != NUL)
10034 EMSG(_(e_trailing));
10035}
10036
10037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 * "eventhandler()" function
10039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "executable()" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010056 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10057}
10058
10059/*
10060 * "exepath()" function
10061 */
10062 static void
10063f_exepath(argvars, rettv)
10064 typval_T *argvars;
10065 typval_T *rettv;
10066{
10067 char_u *p = NULL;
10068
10069 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10070 rettv->v_type = VAR_STRING;
10071 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072}
10073
10074/*
10075 * "exists()" function
10076 */
10077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010079 typval_T *argvars;
10080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 char_u *p;
10083 char_u *name;
10084 int n = FALSE;
10085 int len = 0;
10086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (*p == '$') /* environment variable */
10089 {
10090 /* first try "normal" environment variables (fast) */
10091 if (mch_getenv(p + 1) != NULL)
10092 n = TRUE;
10093 else
10094 {
10095 /* try expanding things like $VIM and ${HOME} */
10096 p = expand_env_save(p);
10097 if (p != NULL && *p != '$')
10098 n = TRUE;
10099 vim_free(p);
10100 }
10101 }
10102 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010105 if (*skipwhite(p) != NUL)
10106 n = FALSE; /* trailing garbage */
10107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 else if (*p == '*') /* internal or user defined function */
10109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010110 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 else if (*p == ':')
10113 {
10114 n = cmd_exists(p + 1);
10115 }
10116 else if (*p == '#')
10117 {
10118#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010119 if (p[1] == '#')
10120 n = autocmd_supported(p + 2);
10121 else
10122 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123#endif
10124 }
10125 else /* internal variable */
10126 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010127 char_u *tofree;
10128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010130 /* get_name_len() takes care of expanding curly braces */
10131 name = p;
10132 len = get_name_len(&p, &tofree, TRUE, FALSE);
10133 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010135 if (tofree != NULL)
10136 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010137 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010138 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010140 /* handle d.key, l[idx], f(expr) */
10141 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10142 if (n)
10143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010146 if (*p != NUL)
10147 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010149 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
10151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153}
10154
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010155#ifdef FEAT_FLOAT
10156/*
10157 * "exp()" function
10158 */
10159 static void
10160f_exp(argvars, rettv)
10161 typval_T *argvars;
10162 typval_T *rettv;
10163{
10164 float_T f;
10165
10166 rettv->v_type = VAR_FLOAT;
10167 if (get_float_arg(argvars, &f) == OK)
10168 rettv->vval.v_float = exp(f);
10169 else
10170 rettv->vval.v_float = 0.0;
10171}
10172#endif
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
10175 * "expand()" function
10176 */
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 char_u *s;
10183 int len;
10184 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010185 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010188 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010191 if (argvars[1].v_type != VAR_UNKNOWN
10192 && argvars[2].v_type != VAR_UNKNOWN
10193 && get_tv_number_chk(&argvars[2], &error)
10194 && !error)
10195 {
10196 rettv->v_type = VAR_LIST;
10197 rettv->vval.v_list = NULL;
10198 }
10199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (*s == '%' || *s == '#' || *s == '<')
10202 {
10203 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010204 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010206 if (rettv->v_type == VAR_LIST)
10207 {
10208 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10209 list_append_string(rettv->vval.v_list, result, -1);
10210 else
10211 vim_free(result);
10212 }
10213 else
10214 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216 else
10217 {
10218 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010219 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 if (argvars[1].v_type != VAR_UNKNOWN
10221 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010222 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 if (!error)
10224 {
10225 ExpandInit(&xpc);
10226 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010227 if (p_wic)
10228 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010229 if (rettv->v_type == VAR_STRING)
10230 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10231 options, WILD_ALL);
10232 else if (rettv_list_alloc(rettv) != FAIL)
10233 {
10234 int i;
10235
10236 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10237 for (i = 0; i < xpc.xp_numfiles; i++)
10238 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10239 ExpandCleanup(&xpc);
10240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 }
10242 else
10243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 }
10245}
10246
10247/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010248 * Go over all entries in "d2" and add them to "d1".
10249 * When "action" is "error" then a duplicate key is an error.
10250 * When "action" is "force" then a duplicate key is overwritten.
10251 * Otherwise duplicate keys are ignored ("action" is "keep").
10252 */
10253 void
10254dict_extend(d1, d2, action)
10255 dict_T *d1;
10256 dict_T *d2;
10257 char_u *action;
10258{
10259 dictitem_T *di1;
10260 hashitem_T *hi2;
10261 int todo;
10262
10263 todo = (int)d2->dv_hashtab.ht_used;
10264 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10265 {
10266 if (!HASHITEM_EMPTY(hi2))
10267 {
10268 --todo;
10269 di1 = dict_find(d1, hi2->hi_key, -1);
10270 if (d1->dv_scope != 0)
10271 {
10272 /* Disallow replacing a builtin function in l: and g:.
10273 * Check the key to be valid when adding to any
10274 * scope. */
10275 if (d1->dv_scope == VAR_DEF_SCOPE
10276 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10277 && var_check_func_name(hi2->hi_key,
10278 di1 == NULL))
10279 break;
10280 if (!valid_varname(hi2->hi_key))
10281 break;
10282 }
10283 if (di1 == NULL)
10284 {
10285 di1 = dictitem_copy(HI2DI(hi2));
10286 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10287 dictitem_free(di1);
10288 }
10289 else if (*action == 'e')
10290 {
10291 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10292 break;
10293 }
10294 else if (*action == 'f' && HI2DI(hi2) != di1)
10295 {
10296 clear_tv(&di1->di_tv);
10297 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10298 }
10299 }
10300 }
10301}
10302
10303/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010304 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306 */
10307 static void
10308f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010309 typval_T *argvars;
10310 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010312 char *arg_errmsg = N_("extend() argument");
10313
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 list_T *l1, *l2;
10317 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320
Bram Moolenaare9a41262005-01-15 22:18:47 +000010321 l1 = argvars[0].vval.v_list;
10322 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010323 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010324 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010325 {
10326 if (argvars[2].v_type != VAR_UNKNOWN)
10327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 before = get_tv_number_chk(&argvars[2], &error);
10329 if (error)
10330 return; /* type error; errmsg already given */
10331
Bram Moolenaar758711c2005-02-02 23:11:38 +000010332 if (before == l1->lv_len)
10333 item = NULL;
10334 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010335 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010336 item = list_find(l1, before);
10337 if (item == NULL)
10338 {
10339 EMSGN(_(e_listidx), before);
10340 return;
10341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 }
10343 }
10344 else
10345 item = NULL;
10346 list_extend(l1, l2, item);
10347
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 copy_tv(&argvars[0], rettv);
10349 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10352 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010353 dict_T *d1, *d2;
10354 char_u *action;
10355 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356
10357 d1 = argvars[0].vval.v_dict;
10358 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010359 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010360 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 {
10362 /* Check the third argument. */
10363 if (argvars[2].v_type != VAR_UNKNOWN)
10364 {
10365 static char *(av[]) = {"keep", "force", "error"};
10366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 action = get_tv_string_chk(&argvars[2]);
10368 if (action == NULL)
10369 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010370 for (i = 0; i < 3; ++i)
10371 if (STRCMP(action, av[i]) == 0)
10372 break;
10373 if (i == 3)
10374 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010375 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010376 return;
10377 }
10378 }
10379 else
10380 action = (char_u *)"force";
10381
Bram Moolenaara9922d62013-05-30 13:01:18 +020010382 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 copy_tv(&argvars[0], rettv);
10385 }
10386 }
10387 else
10388 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010389}
10390
10391/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010392 * "feedkeys()" function
10393 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010394 static void
10395f_feedkeys(argvars, rettv)
10396 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010397 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010398{
10399 int remap = TRUE;
10400 char_u *keys, *flags;
10401 char_u nbuf[NUMBUFLEN];
10402 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010403 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010404
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010405 /* This is not allowed in the sandbox. If the commands would still be
10406 * executed in the sandbox it would be OK, but it probably happens later,
10407 * when "sandbox" is no longer set. */
10408 if (check_secure())
10409 return;
10410
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411 keys = get_tv_string(&argvars[0]);
10412 if (*keys != NUL)
10413 {
10414 if (argvars[1].v_type != VAR_UNKNOWN)
10415 {
10416 flags = get_tv_string_buf(&argvars[1], nbuf);
10417 for ( ; *flags != NUL; ++flags)
10418 {
10419 switch (*flags)
10420 {
10421 case 'n': remap = FALSE; break;
10422 case 'm': remap = TRUE; break;
10423 case 't': typed = TRUE; break;
10424 }
10425 }
10426 }
10427
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010428 /* Need to escape K_SPECIAL and CSI before putting the string in the
10429 * typeahead buffer. */
10430 keys_esc = vim_strsave_escape_csi(keys);
10431 if (keys_esc != NULL)
10432 {
10433 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010434 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010435 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010436 if (vgetc_busy)
10437 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010438 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010439 }
10440}
10441
10442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * "filereadable()" function
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010450 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 char_u *p;
10452 int n;
10453
Bram Moolenaarc236c162008-07-13 17:41:49 +000010454#ifndef O_NONBLOCK
10455# define O_NONBLOCK 0
10456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010458 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10459 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 {
10461 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010462 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 }
10464 else
10465 n = FALSE;
10466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010471 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 * rights to write into.
10473 */
10474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010479 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480}
10481
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010482static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010483
10484 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010485findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010486 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010488 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010489{
10490#ifdef FEAT_SEARCHPATH
10491 char_u *fname;
10492 char_u *fresult = NULL;
10493 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10494 char_u *p;
10495 char_u pathbuf[NUMBUFLEN];
10496 int count = 1;
10497 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010498 int error = FALSE;
10499#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010500
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010501 rettv->vval.v_string = NULL;
10502 rettv->v_type = VAR_STRING;
10503
10504#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010506
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010507 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10510 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010511 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 else
10513 {
10514 if (*p != NUL)
10515 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520 }
10521
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010522 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10523 error = TRUE;
10524
10525 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 do
10528 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010529 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010530 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 fresult = find_file_in_path_option(first ? fname : NULL,
10532 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010533 0, first, path,
10534 find_what,
10535 curbuf->b_ffname,
10536 find_what == FINDFILE_DIR
10537 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010539
10540 if (fresult != NULL && rettv->v_type == VAR_LIST)
10541 list_append_string(rettv->vval.v_list, fresult, -1);
10542
10543 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010545
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010546 if (rettv->v_type == VAR_STRING)
10547 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010548#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010549}
10550
Bram Moolenaar33570922005-01-25 22:26:29 +000010551static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10552static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553
10554/*
10555 * Implementation of map() and filter().
10556 */
10557 static void
10558filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 int map;
10562{
10563 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 listitem_T *li, *nli;
10566 list_T *l = NULL;
10567 dictitem_T *di;
10568 hashtab_T *ht;
10569 hashitem_T *hi;
10570 dict_T *d = NULL;
10571 typval_T save_val;
10572 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010574 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010575 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10576 char *arg_errmsg = (map ? N_("map() argument")
10577 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010578 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010579 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010582 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010583 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010584 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 return;
10586 }
10587 else if (argvars[0].v_type == VAR_DICT)
10588 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010589 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010590 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 return;
10592 }
10593 else
10594 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010595 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 return;
10597 }
10598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 expr = get_tv_string_buf_chk(&argvars[1], buf);
10600 /* On type errors, the preceding call has already displayed an error
10601 * message. Avoid a misleading error message for an empty string that
10602 * was not passed as argument. */
10603 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 prepare_vimvar(VV_VAL, &save_val);
10606 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010607
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010608 /* We reset "did_emsg" to be able to detect whether an error
10609 * occurred during evaluation of the expression. */
10610 save_did_emsg = did_emsg;
10611 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010612
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010613 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 vimvars[VV_KEY].vv_type = VAR_STRING;
10617
10618 ht = &d->dv_hashtab;
10619 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010620 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (!HASHITEM_EMPTY(hi))
10624 {
10625 --todo;
10626 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010627 if (tv_check_lock(di->di_tv.v_lock,
10628 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 break;
10630 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010631 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010632 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 break;
10634 if (!map && rem)
10635 dictitem_remove(d, di);
10636 clear_tv(&vimvars[VV_KEY].vv_tv);
10637 }
10638 }
10639 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 }
10641 else
10642 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010643 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 for (li = l->lv_first; li != NULL; li = nli)
10646 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010647 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010648 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010650 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010651 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010652 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010653 break;
10654 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010656 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010657 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010659
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010660 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010662
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010663 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010665
10666 copy_tv(&argvars[0], rettv);
10667}
10668
10669 static int
10670filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 char_u *expr;
10673 int map;
10674 int *remp;
10675{
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 s = expr;
10682 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010684 if (*s != NUL) /* check for trailing chars after expr */
10685 {
10686 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010687 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 }
10689 if (map)
10690 {
10691 /* map(): replace the list item value */
10692 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010693 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010694 *tv = rettv;
10695 }
10696 else
10697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 int error = FALSE;
10699
Bram Moolenaare9a41262005-01-15 22:18:47 +000010700 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010702 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 /* On type error, nothing has been removed; return FAIL to stop the
10704 * loop. The error message was given by get_tv_number_chk(). */
10705 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010706 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010707 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010708 retval = OK;
10709theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010711 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010712}
10713
10714/*
10715 * "filter()" function
10716 */
10717 static void
10718f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 typval_T *argvars;
10720 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010721{
10722 filter_map(argvars, rettv, FALSE);
10723}
10724
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010726 * "finddir({fname}[, {path}[, {count}]])" function
10727 */
10728 static void
10729f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010732{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010733 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010734}
10735
10736/*
10737 * "findfile({fname}[, {path}[, {count}]])" function
10738 */
10739 static void
10740f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *argvars;
10742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010743{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010744 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010745}
10746
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010747#ifdef FEAT_FLOAT
10748/*
10749 * "float2nr({float})" function
10750 */
10751 static void
10752f_float2nr(argvars, rettv)
10753 typval_T *argvars;
10754 typval_T *rettv;
10755{
10756 float_T f;
10757
10758 if (get_float_arg(argvars, &f) == OK)
10759 {
10760 if (f < -0x7fffffff)
10761 rettv->vval.v_number = -0x7fffffff;
10762 else if (f > 0x7fffffff)
10763 rettv->vval.v_number = 0x7fffffff;
10764 else
10765 rettv->vval.v_number = (varnumber_T)f;
10766 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010767}
10768
10769/*
10770 * "floor({float})" function
10771 */
10772 static void
10773f_floor(argvars, rettv)
10774 typval_T *argvars;
10775 typval_T *rettv;
10776{
10777 float_T f;
10778
10779 rettv->v_type = VAR_FLOAT;
10780 if (get_float_arg(argvars, &f) == OK)
10781 rettv->vval.v_float = floor(f);
10782 else
10783 rettv->vval.v_float = 0.0;
10784}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010785
10786/*
10787 * "fmod()" function
10788 */
10789 static void
10790f_fmod(argvars, rettv)
10791 typval_T *argvars;
10792 typval_T *rettv;
10793{
10794 float_T fx, fy;
10795
10796 rettv->v_type = VAR_FLOAT;
10797 if (get_float_arg(argvars, &fx) == OK
10798 && get_float_arg(&argvars[1], &fy) == OK)
10799 rettv->vval.v_float = fmod(fx, fy);
10800 else
10801 rettv->vval.v_float = 0.0;
10802}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010803#endif
10804
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010806 * "fnameescape({string})" function
10807 */
10808 static void
10809f_fnameescape(argvars, rettv)
10810 typval_T *argvars;
10811 typval_T *rettv;
10812{
10813 rettv->vval.v_string = vim_strsave_fnameescape(
10814 get_tv_string(&argvars[0]), FALSE);
10815 rettv->v_type = VAR_STRING;
10816}
10817
10818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 * "fnamemodify({fname}, {mods})" function
10820 */
10821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 char_u *fname;
10827 char_u *mods;
10828 int usedlen = 0;
10829 int len;
10830 char_u *fbuf = NULL;
10831 char_u buf[NUMBUFLEN];
10832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 fname = get_tv_string_chk(&argvars[0]);
10834 mods = get_tv_string_buf_chk(&argvars[1], buf);
10835 if (fname == NULL || mods == NULL)
10836 fname = NULL;
10837 else
10838 {
10839 len = (int)STRLEN(fname);
10840 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 vim_free(fbuf);
10849}
10850
Bram Moolenaar33570922005-01-25 22:26:29 +000010851static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
10853/*
10854 * "foldclosed()" function
10855 */
10856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010858 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010860 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861{
10862#ifdef FEAT_FOLDING
10863 linenr_T lnum;
10864 linenr_T first, last;
10865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10868 {
10869 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10870 {
10871 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 return;
10876 }
10877 }
10878#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880}
10881
10882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883 * "foldclosed()" function
10884 */
10885 static void
10886f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010889{
10890 foldclosed_both(argvars, rettv, FALSE);
10891}
10892
10893/*
10894 * "foldclosedend()" function
10895 */
10896 static void
10897f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010900{
10901 foldclosed_both(argvars, rettv, TRUE);
10902}
10903
10904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * "foldlevel()" function
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 typval_T *argvars UNUSED;
10910 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
10912#ifdef FEAT_FOLDING
10913 linenr_T lnum;
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919}
10920
10921/*
10922 * "foldtext()" function
10923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929#ifdef FEAT_FOLDING
10930 linenr_T lnum;
10931 char_u *s;
10932 char_u *r;
10933 int len;
10934 char *txt;
10935#endif
10936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->v_type = VAR_STRING;
10938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10941 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10942 <= curbuf->b_ml.ml_line_count
10943 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 {
10945 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10947 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 {
10949 if (!linewhite(lnum))
10950 break;
10951 ++lnum;
10952 }
10953
10954 /* Find interesting text in this line. */
10955 s = skipwhite(ml_get(lnum));
10956 /* skip C comment-start */
10957 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010960 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010962 {
10963 s = skipwhite(ml_get(lnum + 1));
10964 if (*s == '*')
10965 s = skipwhite(s + 1);
10966 }
10967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 txt = _("+-%s%3ld lines: ");
10969 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 + 20 /* for %3ld */
10972 + STRLEN(s))); /* concatenated */
10973 if (r != NULL)
10974 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10976 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10977 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 len = (int)STRLEN(r);
10979 STRCAT(r, s);
10980 /* remove 'foldmarker' and 'commentstring' */
10981 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 }
10984 }
10985#endif
10986}
10987
10988/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010989 * "foldtextresult(lnum)" function
10990 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010993 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010994 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010995{
10996#ifdef FEAT_FOLDING
10997 linenr_T lnum;
10998 char_u *text;
10999 char_u buf[51];
11000 foldinfo_T foldinfo;
11001 int fold_count;
11002#endif
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->v_type = VAR_STRING;
11005 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011006#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 /* treat illegal types and illegal string values for {lnum} the same */
11009 if (lnum < 0)
11010 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011011 fold_count = foldedCount(curwin, lnum, &foldinfo);
11012 if (fold_count > 0)
11013 {
11014 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11015 &foldinfo, buf);
11016 if (text == buf)
11017 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011019 }
11020#endif
11021}
11022
11023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 * "foreground()" function
11025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011028 typval_T *argvars UNUSED;
11029 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031#ifdef FEAT_GUI
11032 if (gui.in_use)
11033 gui_mch_set_foreground();
11034#else
11035# ifdef WIN32
11036 win32_set_foreground();
11037# endif
11038#endif
11039}
11040
11041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011042 * "function()" function
11043 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048{
11049 char_u *s;
11050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011052 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011054 /* Don't check an autoload name for existence here. */
11055 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011056 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057 else
11058 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011059 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011060 {
11061 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011062 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011064 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11065 * also be called from another script. Using trans_function_name()
11066 * would also work, but some plugins depend on the name being
11067 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011068 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011069 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011070 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011071 if (rettv->vval.v_string != NULL)
11072 {
11073 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011074 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011075 }
11076 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011077 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011078 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 }
11081}
11082
11083/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011084 * "garbagecollect()" function
11085 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011086 static void
11087f_garbagecollect(argvars, rettv)
11088 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011089 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011090{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011091 /* This is postponed until we are back at the toplevel, because we may be
11092 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11093 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011094
11095 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11096 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011097}
11098
11099/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100 * "get()" function
11101 */
11102 static void
11103f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 typval_T *argvars;
11105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106{
Bram Moolenaar33570922005-01-25 22:26:29 +000011107 listitem_T *li;
11108 list_T *l;
11109 dictitem_T *di;
11110 dict_T *d;
11111 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 int error = FALSE;
11118
11119 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11120 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 else if (argvars[0].v_type == VAR_DICT)
11125 {
11126 if ((d = argvars[0].vval.v_dict) != NULL)
11127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011128 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 if (di != NULL)
11130 tv = &di->di_tv;
11131 }
11132 }
11133 else
11134 EMSG2(_(e_listdictarg), "get()");
11135
11136 if (tv == NULL)
11137 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011138 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 copy_tv(&argvars[2], rettv);
11140 }
11141 else
11142 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143}
11144
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145static 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 +000011146
11147/*
11148 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149 * Return a range (from start to end) of lines in rettv from the specified
11150 * buffer.
11151 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011152 */
11153 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011155 buf_T *buf;
11156 linenr_T start;
11157 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011158 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011159 typval_T *rettv;
11160{
11161 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011162
Bram Moolenaar959a1432013-12-14 12:17:38 +010011163 rettv->v_type = VAR_STRING;
11164 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011165 if (retlist && rettv_list_alloc(rettv) == FAIL)
11166 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011167
11168 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11169 return;
11170
11171 if (!retlist)
11172 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011173 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11174 p = ml_get_buf(buf, start, FALSE);
11175 else
11176 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011177 rettv->vval.v_string = vim_strsave(p);
11178 }
11179 else
11180 {
11181 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011182 return;
11183
11184 if (start < 1)
11185 start = 1;
11186 if (end > buf->b_ml.ml_line_count)
11187 end = buf->b_ml.ml_line_count;
11188 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011189 if (list_append_string(rettv->vval.v_list,
11190 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011191 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011192 }
11193}
11194
11195/*
11196 * "getbufline()" function
11197 */
11198 static void
11199f_getbufline(argvars, rettv)
11200 typval_T *argvars;
11201 typval_T *rettv;
11202{
11203 linenr_T lnum;
11204 linenr_T end;
11205 buf_T *buf;
11206
11207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11208 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011209 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011210 --emsg_off;
11211
Bram Moolenaar661b1822005-07-28 22:36:45 +000011212 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011213 if (argvars[2].v_type == VAR_UNKNOWN)
11214 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011215 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011216 end = get_tv_lnum_buf(&argvars[2], buf);
11217
Bram Moolenaar342337a2005-07-21 21:11:17 +000011218 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011219}
11220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221/*
11222 * "getbufvar()" function
11223 */
11224 static void
11225f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *argvars;
11227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228{
11229 buf_T *buf;
11230 buf_T *save_curbuf;
11231 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011233 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11236 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011238 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011239
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011240 rettv->v_type = VAR_STRING;
11241 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242
11243 if (buf != NULL && varname != NULL)
11244 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011245 /* set curbuf to be our buf, temporarily */
11246 save_curbuf = curbuf;
11247 curbuf = buf;
11248
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011250 {
11251 if (get_option_tv(&varname, rettv, TRUE) == OK)
11252 done = TRUE;
11253 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011254 else if (STRCMP(varname, "changedtick") == 0)
11255 {
11256 rettv->v_type = VAR_NUMBER;
11257 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011258 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011260 else
11261 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011262 /* Look up the variable. */
11263 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11264 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11265 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011267 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011269 done = TRUE;
11270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011272
11273 /* restore previous notion of curbuf */
11274 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011275 }
11276
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011277 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11278 /* use the default value */
11279 copy_tv(&argvars[2], rettv);
11280
Bram Moolenaar0d660222005-01-07 21:51:51 +000011281 --emsg_off;
11282}
11283
11284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 * "getchar()" function
11286 */
11287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 typval_T *argvars;
11290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
11292 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011295 /* Position the cursor. Needed after a message that ends in a space. */
11296 windgoto(msg_row, msg_col);
11297
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 ++no_mapping;
11299 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011300 for (;;)
11301 {
11302 if (argvars[0].v_type == VAR_UNKNOWN)
11303 /* getchar(): blocking wait. */
11304 n = safe_vgetc();
11305 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11306 /* getchar(1): only check if char avail */
11307 n = vpeekc();
11308 else if (error || vpeekc() == NUL)
11309 /* illegal argument or getchar(0) and no char avail: return zero */
11310 n = 0;
11311 else
11312 /* getchar(0) and char avail: return char */
11313 n = safe_vgetc();
11314 if (n == K_IGNORE)
11315 continue;
11316 break;
11317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 --no_mapping;
11319 --allow_keys;
11320
Bram Moolenaar219b8702006-11-01 14:32:36 +000011321 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11322 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11323 vimvars[VV_MOUSE_COL].vv_nr = 0;
11324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 if (IS_SPECIAL(n) || mod_mask != 0)
11327 {
11328 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11329 int i = 0;
11330
11331 /* Turn a special key into three bytes, plus modifier. */
11332 if (mod_mask != 0)
11333 {
11334 temp[i++] = K_SPECIAL;
11335 temp[i++] = KS_MODIFIER;
11336 temp[i++] = mod_mask;
11337 }
11338 if (IS_SPECIAL(n))
11339 {
11340 temp[i++] = K_SPECIAL;
11341 temp[i++] = K_SECOND(n);
11342 temp[i++] = K_THIRD(n);
11343 }
11344#ifdef FEAT_MBYTE
11345 else if (has_mbyte)
11346 i += (*mb_char2bytes)(n, temp + i);
11347#endif
11348 else
11349 temp[i++] = n;
11350 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->v_type = VAR_STRING;
11352 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011353
11354#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011355 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011356 {
11357 int row = mouse_row;
11358 int col = mouse_col;
11359 win_T *win;
11360 linenr_T lnum;
11361# ifdef FEAT_WINDOWS
11362 win_T *wp;
11363# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011364 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011365
11366 if (row >= 0 && col >= 0)
11367 {
11368 /* Find the window at the mouse coordinates and compute the
11369 * text position. */
11370 win = mouse_find_win(&row, &col);
11371 (void)mouse_comp_pos(win, &row, &col, &lnum);
11372# ifdef FEAT_WINDOWS
11373 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011374 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011375# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011376 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011377 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11378 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11379 }
11380 }
11381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383}
11384
11385/*
11386 * "getcharmod()" function
11387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394}
11395
11396/*
11397 * "getcmdline()" function
11398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 rettv->v_type = VAR_STRING;
11405 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406}
11407
11408/*
11409 * "getcmdpos()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417}
11418
11419/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011420 * "getcmdtype()" function
11421 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011422 static void
11423f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011424 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011425 typval_T *rettv;
11426{
11427 rettv->v_type = VAR_STRING;
11428 rettv->vval.v_string = alloc(2);
11429 if (rettv->vval.v_string != NULL)
11430 {
11431 rettv->vval.v_string[0] = get_cmdline_type();
11432 rettv->vval.v_string[1] = NUL;
11433 }
11434}
11435
11436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 * "getcwd()" function
11438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011441 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011444 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011447 rettv->vval.v_string = NULL;
11448 cwd = alloc(MAXPATHL);
11449 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011451 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11452 {
11453 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011455 if (rettv->vval.v_string != NULL)
11456 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011458 }
11459 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 }
11461}
11462
11463/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011464 * "getfontname()" function
11465 */
11466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
11472 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011473#ifdef FEAT_GUI
11474 if (gui.in_use)
11475 {
11476 GuiFont font;
11477 char_u *name = NULL;
11478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011480 {
11481 /* Get the "Normal" font. Either the name saved by
11482 * hl_set_font_name() or from the font ID. */
11483 font = gui.norm_font;
11484 name = hl_get_font_name();
11485 }
11486 else
11487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011489 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11490 return;
11491 font = gui_mch_get_font(name, FALSE);
11492 if (font == NOFONT)
11493 return; /* Invalid font name, return empty string. */
11494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011496 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011497 gui_mch_free_font(font);
11498 }
11499#endif
11500}
11501
11502/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011503 * "getfperm({fname})" function
11504 */
11505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011507 typval_T *argvars;
11508 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011509{
11510 char_u *fname;
11511 struct stat st;
11512 char_u *perm = NULL;
11513 char_u flags[] = "rwx";
11514 int i;
11515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011519 if (mch_stat((char *)fname, &st) >= 0)
11520 {
11521 perm = vim_strsave((char_u *)"---------");
11522 if (perm != NULL)
11523 {
11524 for (i = 0; i < 9; i++)
11525 {
11526 if (st.st_mode & (1 << (8 - i)))
11527 perm[i] = flags[i % 3];
11528 }
11529 }
11530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011532}
11533
11534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 * "getfsize({fname})" function
11536 */
11537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011539 typval_T *argvars;
11540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542 char_u *fname;
11543 struct stat st;
11544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548
11549 if (mch_stat((char *)fname, &st) >= 0)
11550 {
11551 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011556
11557 /* non-perfect check for overflow */
11558 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11559 rettv->vval.v_number = -2;
11560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564}
11565
11566/*
11567 * "getftime({fname})" function
11568 */
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574 char_u *fname;
11575 struct stat st;
11576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
11579 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583}
11584
11585/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011586 * "getftype({fname})" function
11587 */
11588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011590 typval_T *argvars;
11591 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011592{
11593 char_u *fname;
11594 struct stat st;
11595 char_u *type = NULL;
11596 char *t;
11597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011601 if (mch_lstat((char *)fname, &st) >= 0)
11602 {
11603#ifdef S_ISREG
11604 if (S_ISREG(st.st_mode))
11605 t = "file";
11606 else if (S_ISDIR(st.st_mode))
11607 t = "dir";
11608# ifdef S_ISLNK
11609 else if (S_ISLNK(st.st_mode))
11610 t = "link";
11611# endif
11612# ifdef S_ISBLK
11613 else if (S_ISBLK(st.st_mode))
11614 t = "bdev";
11615# endif
11616# ifdef S_ISCHR
11617 else if (S_ISCHR(st.st_mode))
11618 t = "cdev";
11619# endif
11620# ifdef S_ISFIFO
11621 else if (S_ISFIFO(st.st_mode))
11622 t = "fifo";
11623# endif
11624# ifdef S_ISSOCK
11625 else if (S_ISSOCK(st.st_mode))
11626 t = "fifo";
11627# endif
11628 else
11629 t = "other";
11630#else
11631# ifdef S_IFMT
11632 switch (st.st_mode & S_IFMT)
11633 {
11634 case S_IFREG: t = "file"; break;
11635 case S_IFDIR: t = "dir"; break;
11636# ifdef S_IFLNK
11637 case S_IFLNK: t = "link"; break;
11638# endif
11639# ifdef S_IFBLK
11640 case S_IFBLK: t = "bdev"; break;
11641# endif
11642# ifdef S_IFCHR
11643 case S_IFCHR: t = "cdev"; break;
11644# endif
11645# ifdef S_IFIFO
11646 case S_IFIFO: t = "fifo"; break;
11647# endif
11648# ifdef S_IFSOCK
11649 case S_IFSOCK: t = "socket"; break;
11650# endif
11651 default: t = "other";
11652 }
11653# else
11654 if (mch_isdir(fname))
11655 t = "dir";
11656 else
11657 t = "file";
11658# endif
11659#endif
11660 type = vim_strsave((char_u *)t);
11661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011663}
11664
11665/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011666 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 */
11668 static void
11669f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011670 typval_T *argvars;
11671 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672{
11673 linenr_T lnum;
11674 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011675 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676
11677 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011678 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011679 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011680 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 retlist = FALSE;
11682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011686 retlist = TRUE;
11687 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011688
Bram Moolenaar342337a2005-07-21 21:11:17 +000011689 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690}
11691
11692/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011693 * "getmatches()" function
11694 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011695 static void
11696f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011697 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011698 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011699{
11700#ifdef FEAT_SEARCH_EXTRA
11701 dict_T *dict;
11702 matchitem_T *cur = curwin->w_match_head;
11703
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011704 if (rettv_list_alloc(rettv) == OK)
11705 {
11706 while (cur != NULL)
11707 {
11708 dict = dict_alloc();
11709 if (dict == NULL)
11710 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011711 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11712 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11713 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11714 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11715 list_append_dict(rettv->vval.v_list, dict);
11716 cur = cur->next;
11717 }
11718 }
11719#endif
11720}
11721
11722/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011723 * "getpid()" function
11724 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011725 static void
11726f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011727 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011728 typval_T *rettv;
11729{
11730 rettv->vval.v_number = mch_get_pid();
11731}
11732
11733/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011734 * "getpos(string)" function
11735 */
11736 static void
11737f_getpos(argvars, rettv)
11738 typval_T *argvars;
11739 typval_T *rettv;
11740{
11741 pos_T *fp;
11742 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011743 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011744
11745 if (rettv_list_alloc(rettv) == OK)
11746 {
11747 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011748 fp = var2fpos(&argvars[0], TRUE, &fnum);
11749 if (fnum != -1)
11750 list_append_number(l, (varnumber_T)fnum);
11751 else
11752 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011753 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11754 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011755 list_append_number(l, (fp != NULL)
11756 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011757 : (varnumber_T)0);
11758 list_append_number(l,
11759#ifdef FEAT_VIRTUALEDIT
11760 (fp != NULL) ? (varnumber_T)fp->coladd :
11761#endif
11762 (varnumber_T)0);
11763 }
11764 else
11765 rettv->vval.v_number = FALSE;
11766}
11767
11768/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011769 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011770 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011771 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011772f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011773 typval_T *argvars UNUSED;
11774 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011775{
11776#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011777 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011778#endif
11779
Bram Moolenaar2641f772005-03-25 21:58:17 +000011780#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011781 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011782 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011783 wp = NULL;
11784 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11785 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011787 if (wp == NULL)
11788 return;
11789 }
11790
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011791 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011792 }
11793#endif
11794}
11795
11796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 * "getreg()" function
11798 */
11799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 typval_T *argvars;
11802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803{
11804 char_u *strregname;
11805 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011806 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011807 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011810 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 strregname = get_tv_string_chk(&argvars[0]);
11813 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011817 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11818 return_list = get_tv_number_chk(&argvars[2], &error);
11819 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011822 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011823
11824 if (error)
11825 return;
11826
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 regname = (strregname == NULL ? '"' : *strregname);
11828 if (regname == 0)
11829 regname = '"';
11830
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011831 if (return_list)
11832 {
11833 rettv->v_type = VAR_LIST;
11834 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11835 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11836 }
11837 else
11838 {
11839 rettv->v_type = VAR_STRING;
11840 rettv->vval.v_string = get_reg_contents(regname,
11841 arg2 ? GREG_EXPR_SRC : 0);
11842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843}
11844
11845/*
11846 * "getregtype()" function
11847 */
11848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011850 typval_T *argvars;
11851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
11853 char_u *strregname;
11854 int regname;
11855 char_u buf[NUMBUFLEN + 2];
11856 long reglen = 0;
11857
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011858 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 {
11860 strregname = get_tv_string_chk(&argvars[0]);
11861 if (strregname == NULL) /* type error; errmsg already given */
11862 {
11863 rettv->v_type = VAR_STRING;
11864 rettv->vval.v_string = NULL;
11865 return;
11866 }
11867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
11869 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871
11872 regname = (strregname == NULL ? '"' : *strregname);
11873 if (regname == 0)
11874 regname = '"';
11875
11876 buf[0] = NUL;
11877 buf[1] = NUL;
11878 switch (get_reg_type(regname, &reglen))
11879 {
11880 case MLINE: buf[0] = 'V'; break;
11881 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 case MBLOCK:
11883 buf[0] = Ctrl_V;
11884 sprintf((char *)buf + 1, "%ld", reglen + 1);
11885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->v_type = VAR_STRING;
11888 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
11891/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011892 * "gettabvar()" function
11893 */
11894 static void
11895f_gettabvar(argvars, rettv)
11896 typval_T *argvars;
11897 typval_T *rettv;
11898{
11899 tabpage_T *tp;
11900 dictitem_T *v;
11901 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011902 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011903
11904 rettv->v_type = VAR_STRING;
11905 rettv->vval.v_string = NULL;
11906
11907 varname = get_tv_string_chk(&argvars[1]);
11908 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11909 if (tp != NULL && varname != NULL)
11910 {
11911 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011912 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011913 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011914 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011915 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011916 done = TRUE;
11917 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011918 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011919
11920 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011921 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011922}
11923
11924/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011925 * "gettabwinvar()" function
11926 */
11927 static void
11928f_gettabwinvar(argvars, rettv)
11929 typval_T *argvars;
11930 typval_T *rettv;
11931{
11932 getwinvar(argvars, rettv, 1);
11933}
11934
11935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 * "getwinposx()" function
11937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011940 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944#ifdef FEAT_GUI
11945 if (gui.in_use)
11946 {
11947 int x, y;
11948
11949 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 }
11952#endif
11953}
11954
11955/*
11956 * "getwinposy()" function
11957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964#ifdef FEAT_GUI
11965 if (gui.in_use)
11966 {
11967 int x, y;
11968
11969 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 }
11972#endif
11973}
11974
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011975/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011976 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011977 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011978 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011979find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011980 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011981 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011982{
11983#ifdef FEAT_WINDOWS
11984 win_T *wp;
11985#endif
11986 int nr;
11987
11988 nr = get_tv_number_chk(vp, NULL);
11989
11990#ifdef FEAT_WINDOWS
11991 if (nr < 0)
11992 return NULL;
11993 if (nr == 0)
11994 return curwin;
11995
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011996 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11997 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011998 if (--nr <= 0)
11999 break;
12000 return wp;
12001#else
12002 if (nr == 0 || nr == 1)
12003 return curwin;
12004 return NULL;
12005#endif
12006}
12007
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008/*
12009 * "getwinvar()" function
12010 */
12011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *argvars;
12014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012016 getwinvar(argvars, rettv, 0);
12017}
12018
12019/*
12020 * getwinvar() and gettabwinvar()
12021 */
12022 static void
12023getwinvar(argvars, rettv, off)
12024 typval_T *argvars;
12025 typval_T *rettv;
12026 int off; /* 1 for gettabwinvar() */
12027{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 win_T *win, *oldcurwin;
12029 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012030 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012031 tabpage_T *tp = NULL;
12032 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012033 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012035#ifdef FEAT_WINDOWS
12036 if (off == 1)
12037 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12038 else
12039 tp = curtab;
12040#endif
12041 win = find_win_by_nr(&argvars[off], tp);
12042 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012045 rettv->v_type = VAR_STRING;
12046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
12048 if (win != NULL && varname != NULL)
12049 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012050 /* Set curwin to be our win, temporarily. Also set the tabpage,
12051 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012052 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012053
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012055 {
12056 if (get_option_tv(&varname, rettv, 1) == OK)
12057 done = TRUE;
12058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
12060 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012061 /* Look up the variable. */
12062 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12063 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012065 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012067 done = TRUE;
12068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012070
12071 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012072 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 }
12074
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012075 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12076 /* use the default return value */
12077 copy_tv(&argvars[off + 2], rettv);
12078
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 --emsg_off;
12080}
12081
12082/*
12083 * "glob()" function
12084 */
12085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012090 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012092 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012094 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012095 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012097 if (argvars[1].v_type != VAR_UNKNOWN)
12098 {
12099 if (get_tv_number_chk(&argvars[1], &error))
12100 options |= WILD_KEEP_ALL;
12101 if (argvars[2].v_type != VAR_UNKNOWN
12102 && get_tv_number_chk(&argvars[2], &error))
12103 {
12104 rettv->v_type = VAR_LIST;
12105 rettv->vval.v_list = NULL;
12106 }
12107 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012108 if (!error)
12109 {
12110 ExpandInit(&xpc);
12111 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012112 if (p_wic)
12113 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012114 if (rettv->v_type == VAR_STRING)
12115 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012116 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012117 else if (rettv_list_alloc(rettv) != FAIL)
12118 {
12119 int i;
12120
12121 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12122 NULL, options, WILD_ALL_KEEP);
12123 for (i = 0; i < xpc.xp_numfiles; i++)
12124 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12125
12126 ExpandCleanup(&xpc);
12127 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012128 }
12129 else
12130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131}
12132
12133/*
12134 * "globpath()" function
12135 */
12136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 typval_T *argvars;
12139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012141 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012144 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012146 /* When the optional second argument is non-zero, don't remove matches
12147 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12148 if (argvars[2].v_type != VAR_UNKNOWN
12149 && get_tv_number_chk(&argvars[2], &error))
12150 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012152 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 rettv->vval.v_string = NULL;
12154 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012155 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12156 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157}
12158
12159/*
12160 * "has()" function
12161 */
12162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012164 typval_T *argvars;
12165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166{
12167 int i;
12168 char_u *name;
12169 int n = FALSE;
12170 static char *(has_list[]) =
12171 {
12172#ifdef AMIGA
12173 "amiga",
12174# ifdef FEAT_ARP
12175 "arp",
12176# endif
12177#endif
12178#ifdef __BEOS__
12179 "beos",
12180#endif
12181#ifdef MSDOS
12182# ifdef DJGPP
12183 "dos32",
12184# else
12185 "dos16",
12186# endif
12187#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012188#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 "mac",
12190#endif
12191#if defined(MACOS_X_UNIX)
12192 "macunix",
12193#endif
12194#ifdef OS2
12195 "os2",
12196#endif
12197#ifdef __QNX__
12198 "qnx",
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef UNIX
12201 "unix",
12202#endif
12203#ifdef VMS
12204 "vms",
12205#endif
12206#ifdef WIN16
12207 "win16",
12208#endif
12209#ifdef WIN32
12210 "win32",
12211#endif
12212#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12213 "win32unix",
12214#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012215#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 "win64",
12217#endif
12218#ifdef EBCDIC
12219 "ebcdic",
12220#endif
12221#ifndef CASE_INSENSITIVE_FILENAME
12222 "fname_case",
12223#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012224#ifdef HAVE_ACL
12225 "acl",
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#ifdef FEAT_ARABIC
12228 "arabic",
12229#endif
12230#ifdef FEAT_AUTOCMD
12231 "autocmd",
12232#endif
12233#ifdef FEAT_BEVAL
12234 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012235# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12236 "balloon_multiline",
12237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#endif
12239#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12240 "builtin_terms",
12241# ifdef ALL_BUILTIN_TCAPS
12242 "all_builtin_terms",
12243# endif
12244#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012245#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12246 || defined(FEAT_GUI_W32) \
12247 || defined(FEAT_GUI_MOTIF))
12248 "browsefilter",
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250#ifdef FEAT_BYTEOFF
12251 "byte_offset",
12252#endif
12253#ifdef FEAT_CINDENT
12254 "cindent",
12255#endif
12256#ifdef FEAT_CLIENTSERVER
12257 "clientserver",
12258#endif
12259#ifdef FEAT_CLIPBOARD
12260 "clipboard",
12261#endif
12262#ifdef FEAT_CMDL_COMPL
12263 "cmdline_compl",
12264#endif
12265#ifdef FEAT_CMDHIST
12266 "cmdline_hist",
12267#endif
12268#ifdef FEAT_COMMENTS
12269 "comments",
12270#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012271#ifdef FEAT_CONCEAL
12272 "conceal",
12273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274#ifdef FEAT_CRYPT
12275 "cryptv",
12276#endif
12277#ifdef FEAT_CSCOPE
12278 "cscope",
12279#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012280#ifdef FEAT_CURSORBIND
12281 "cursorbind",
12282#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012283#ifdef CURSOR_SHAPE
12284 "cursorshape",
12285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286#ifdef DEBUG
12287 "debug",
12288#endif
12289#ifdef FEAT_CON_DIALOG
12290 "dialog_con",
12291#endif
12292#ifdef FEAT_GUI_DIALOG
12293 "dialog_gui",
12294#endif
12295#ifdef FEAT_DIFF
12296 "diff",
12297#endif
12298#ifdef FEAT_DIGRAPHS
12299 "digraphs",
12300#endif
12301#ifdef FEAT_DND
12302 "dnd",
12303#endif
12304#ifdef FEAT_EMACS_TAGS
12305 "emacs_tags",
12306#endif
12307 "eval", /* always present, of course! */
12308#ifdef FEAT_EX_EXTRA
12309 "ex_extra",
12310#endif
12311#ifdef FEAT_SEARCH_EXTRA
12312 "extra_search",
12313#endif
12314#ifdef FEAT_FKMAP
12315 "farsi",
12316#endif
12317#ifdef FEAT_SEARCHPATH
12318 "file_in_path",
12319#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012320#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012321 "filterpipe",
12322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#ifdef FEAT_FIND_ID
12324 "find_in_path",
12325#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012326#ifdef FEAT_FLOAT
12327 "float",
12328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_FOLDING
12330 "folding",
12331#endif
12332#ifdef FEAT_FOOTER
12333 "footer",
12334#endif
12335#if !defined(USE_SYSTEM) && defined(UNIX)
12336 "fork",
12337#endif
12338#ifdef FEAT_GETTEXT
12339 "gettext",
12340#endif
12341#ifdef FEAT_GUI
12342 "gui",
12343#endif
12344#ifdef FEAT_GUI_ATHENA
12345# ifdef FEAT_GUI_NEXTAW
12346 "gui_neXtaw",
12347# else
12348 "gui_athena",
12349# endif
12350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_GUI_GTK
12352 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012355#ifdef FEAT_GUI_GNOME
12356 "gui_gnome",
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_GUI_MAC
12359 "gui_mac",
12360#endif
12361#ifdef FEAT_GUI_MOTIF
12362 "gui_motif",
12363#endif
12364#ifdef FEAT_GUI_PHOTON
12365 "gui_photon",
12366#endif
12367#ifdef FEAT_GUI_W16
12368 "gui_win16",
12369#endif
12370#ifdef FEAT_GUI_W32
12371 "gui_win32",
12372#endif
12373#ifdef FEAT_HANGULIN
12374 "hangul_input",
12375#endif
12376#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12377 "iconv",
12378#endif
12379#ifdef FEAT_INS_EXPAND
12380 "insert_expand",
12381#endif
12382#ifdef FEAT_JUMPLIST
12383 "jumplist",
12384#endif
12385#ifdef FEAT_KEYMAP
12386 "keymap",
12387#endif
12388#ifdef FEAT_LANGMAP
12389 "langmap",
12390#endif
12391#ifdef FEAT_LIBCALL
12392 "libcall",
12393#endif
12394#ifdef FEAT_LINEBREAK
12395 "linebreak",
12396#endif
12397#ifdef FEAT_LISP
12398 "lispindent",
12399#endif
12400#ifdef FEAT_LISTCMDS
12401 "listcmds",
12402#endif
12403#ifdef FEAT_LOCALMAP
12404 "localmap",
12405#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012406#ifdef FEAT_LUA
12407# ifndef DYNAMIC_LUA
12408 "lua",
12409# endif
12410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#ifdef FEAT_MENU
12412 "menu",
12413#endif
12414#ifdef FEAT_SESSION
12415 "mksession",
12416#endif
12417#ifdef FEAT_MODIFY_FNAME
12418 "modify_fname",
12419#endif
12420#ifdef FEAT_MOUSE
12421 "mouse",
12422#endif
12423#ifdef FEAT_MOUSESHAPE
12424 "mouseshape",
12425#endif
12426#if defined(UNIX) || defined(VMS)
12427# ifdef FEAT_MOUSE_DEC
12428 "mouse_dec",
12429# endif
12430# ifdef FEAT_MOUSE_GPM
12431 "mouse_gpm",
12432# endif
12433# ifdef FEAT_MOUSE_JSB
12434 "mouse_jsbterm",
12435# endif
12436# ifdef FEAT_MOUSE_NET
12437 "mouse_netterm",
12438# endif
12439# ifdef FEAT_MOUSE_PTERM
12440 "mouse_pterm",
12441# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012442# ifdef FEAT_MOUSE_SGR
12443 "mouse_sgr",
12444# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012445# ifdef FEAT_SYSMOUSE
12446 "mouse_sysmouse",
12447# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012448# ifdef FEAT_MOUSE_URXVT
12449 "mouse_urxvt",
12450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451# ifdef FEAT_MOUSE_XTERM
12452 "mouse_xterm",
12453# endif
12454#endif
12455#ifdef FEAT_MBYTE
12456 "multi_byte",
12457#endif
12458#ifdef FEAT_MBYTE_IME
12459 "multi_byte_ime",
12460#endif
12461#ifdef FEAT_MULTI_LANG
12462 "multi_lang",
12463#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012464#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012465#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012466 "mzscheme",
12467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469#ifdef FEAT_OLE
12470 "ole",
12471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472#ifdef FEAT_PATH_EXTRA
12473 "path_extra",
12474#endif
12475#ifdef FEAT_PERL
12476#ifndef DYNAMIC_PERL
12477 "perl",
12478#endif
12479#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012480#ifdef FEAT_PERSISTENT_UNDO
12481 "persistent_undo",
12482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483#ifdef FEAT_PYTHON
12484#ifndef DYNAMIC_PYTHON
12485 "python",
12486#endif
12487#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012488#ifdef FEAT_PYTHON3
12489#ifndef DYNAMIC_PYTHON3
12490 "python3",
12491#endif
12492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493#ifdef FEAT_POSTSCRIPT
12494 "postscript",
12495#endif
12496#ifdef FEAT_PRINTER
12497 "printer",
12498#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012499#ifdef FEAT_PROFILE
12500 "profile",
12501#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012502#ifdef FEAT_RELTIME
12503 "reltime",
12504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505#ifdef FEAT_QUICKFIX
12506 "quickfix",
12507#endif
12508#ifdef FEAT_RIGHTLEFT
12509 "rightleft",
12510#endif
12511#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12512 "ruby",
12513#endif
12514#ifdef FEAT_SCROLLBIND
12515 "scrollbind",
12516#endif
12517#ifdef FEAT_CMDL_INFO
12518 "showcmd",
12519 "cmdline_info",
12520#endif
12521#ifdef FEAT_SIGNS
12522 "signs",
12523#endif
12524#ifdef FEAT_SMARTINDENT
12525 "smartindent",
12526#endif
12527#ifdef FEAT_SNIFF
12528 "sniff",
12529#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012530#ifdef STARTUPTIME
12531 "startuptime",
12532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533#ifdef FEAT_STL_OPT
12534 "statusline",
12535#endif
12536#ifdef FEAT_SUN_WORKSHOP
12537 "sun_workshop",
12538#endif
12539#ifdef FEAT_NETBEANS_INTG
12540 "netbeans_intg",
12541#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012542#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012543 "spell",
12544#endif
12545#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 "syntax",
12547#endif
12548#if defined(USE_SYSTEM) || !defined(UNIX)
12549 "system",
12550#endif
12551#ifdef FEAT_TAG_BINS
12552 "tag_binary",
12553#endif
12554#ifdef FEAT_TAG_OLDSTATIC
12555 "tag_old_static",
12556#endif
12557#ifdef FEAT_TAG_ANYWHITE
12558 "tag_any_white",
12559#endif
12560#ifdef FEAT_TCL
12561# ifndef DYNAMIC_TCL
12562 "tcl",
12563# endif
12564#endif
12565#ifdef TERMINFO
12566 "terminfo",
12567#endif
12568#ifdef FEAT_TERMRESPONSE
12569 "termresponse",
12570#endif
12571#ifdef FEAT_TEXTOBJ
12572 "textobjects",
12573#endif
12574#ifdef HAVE_TGETENT
12575 "tgetent",
12576#endif
12577#ifdef FEAT_TITLE
12578 "title",
12579#endif
12580#ifdef FEAT_TOOLBAR
12581 "toolbar",
12582#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012583#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12584 "unnamedplus",
12585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586#ifdef FEAT_USR_CMDS
12587 "user-commands", /* was accidentally included in 5.4 */
12588 "user_commands",
12589#endif
12590#ifdef FEAT_VIMINFO
12591 "viminfo",
12592#endif
12593#ifdef FEAT_VERTSPLIT
12594 "vertsplit",
12595#endif
12596#ifdef FEAT_VIRTUALEDIT
12597 "virtualedit",
12598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600#ifdef FEAT_VISUALEXTRA
12601 "visualextra",
12602#endif
12603#ifdef FEAT_VREPLACE
12604 "vreplace",
12605#endif
12606#ifdef FEAT_WILDIGN
12607 "wildignore",
12608#endif
12609#ifdef FEAT_WILDMENU
12610 "wildmenu",
12611#endif
12612#ifdef FEAT_WINDOWS
12613 "windows",
12614#endif
12615#ifdef FEAT_WAK
12616 "winaltkeys",
12617#endif
12618#ifdef FEAT_WRITEBACKUP
12619 "writebackup",
12620#endif
12621#ifdef FEAT_XIM
12622 "xim",
12623#endif
12624#ifdef FEAT_XFONTSET
12625 "xfontset",
12626#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012627#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012628 "xpm",
12629 "xpm_w32", /* for backward compatibility */
12630#else
12631# if defined(HAVE_XPM)
12632 "xpm",
12633# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635#ifdef USE_XSMP
12636 "xsmp",
12637#endif
12638#ifdef USE_XSMP_INTERACT
12639 "xsmp_interact",
12640#endif
12641#ifdef FEAT_XCLIPBOARD
12642 "xterm_clipboard",
12643#endif
12644#ifdef FEAT_XTERM_SAVE
12645 "xterm_save",
12646#endif
12647#if defined(UNIX) && defined(FEAT_X11)
12648 "X11",
12649#endif
12650 NULL
12651 };
12652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 for (i = 0; has_list[i] != NULL; ++i)
12655 if (STRICMP(name, has_list[i]) == 0)
12656 {
12657 n = TRUE;
12658 break;
12659 }
12660
12661 if (n == FALSE)
12662 {
12663 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012664 {
12665 if (name[5] == '-'
12666 && STRLEN(name) > 11
12667 && vim_isdigit(name[6])
12668 && vim_isdigit(name[8])
12669 && vim_isdigit(name[10]))
12670 {
12671 int major = atoi((char *)name + 6);
12672 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012673
12674 /* Expect "patch-9.9.01234". */
12675 n = (major < VIM_VERSION_MAJOR
12676 || (major == VIM_VERSION_MAJOR
12677 && (minor < VIM_VERSION_MINOR
12678 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012679 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012680 }
12681 else
12682 n = has_patch(atoi((char *)name + 5));
12683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 else if (STRICMP(name, "vim_starting") == 0)
12685 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012686#ifdef FEAT_MBYTE
12687 else if (STRICMP(name, "multi_byte_encoding") == 0)
12688 n = has_mbyte;
12689#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012690#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12691 else if (STRICMP(name, "balloon_multiline") == 0)
12692 n = multiline_balloon_available();
12693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694#ifdef DYNAMIC_TCL
12695 else if (STRICMP(name, "tcl") == 0)
12696 n = tcl_enabled(FALSE);
12697#endif
12698#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12699 else if (STRICMP(name, "iconv") == 0)
12700 n = iconv_enabled(FALSE);
12701#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012702#ifdef DYNAMIC_LUA
12703 else if (STRICMP(name, "lua") == 0)
12704 n = lua_enabled(FALSE);
12705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012706#ifdef DYNAMIC_MZSCHEME
12707 else if (STRICMP(name, "mzscheme") == 0)
12708 n = mzscheme_enabled(FALSE);
12709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#ifdef DYNAMIC_RUBY
12711 else if (STRICMP(name, "ruby") == 0)
12712 n = ruby_enabled(FALSE);
12713#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012714#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715#ifdef DYNAMIC_PYTHON
12716 else if (STRICMP(name, "python") == 0)
12717 n = python_enabled(FALSE);
12718#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012719#endif
12720#ifdef FEAT_PYTHON3
12721#ifdef DYNAMIC_PYTHON3
12722 else if (STRICMP(name, "python3") == 0)
12723 n = python3_enabled(FALSE);
12724#endif
12725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#ifdef DYNAMIC_PERL
12727 else if (STRICMP(name, "perl") == 0)
12728 n = perl_enabled(FALSE);
12729#endif
12730#ifdef FEAT_GUI
12731 else if (STRICMP(name, "gui_running") == 0)
12732 n = (gui.in_use || gui.starting);
12733# ifdef FEAT_GUI_W32
12734 else if (STRICMP(name, "gui_win32s") == 0)
12735 n = gui_is_win32s();
12736# endif
12737# ifdef FEAT_BROWSE
12738 else if (STRICMP(name, "browse") == 0)
12739 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12740# endif
12741#endif
12742#ifdef FEAT_SYN_HL
12743 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012744 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745#endif
12746#if defined(WIN3264)
12747 else if (STRICMP(name, "win95") == 0)
12748 n = mch_windows95();
12749#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012750#ifdef FEAT_NETBEANS_INTG
12751 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012752 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 }
12755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012760 * "has_key()" function
12761 */
12762 static void
12763f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012766{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012767 if (argvars[0].v_type != VAR_DICT)
12768 {
12769 EMSG(_(e_dictreq));
12770 return;
12771 }
12772 if (argvars[0].vval.v_dict == NULL)
12773 return;
12774
12775 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012776 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012777}
12778
12779/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012780 * "haslocaldir()" function
12781 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012782 static void
12783f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012784 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012785 typval_T *rettv;
12786{
12787 rettv->vval.v_number = (curwin->w_localdir != NULL);
12788}
12789
12790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 * "hasmapto()" function
12792 */
12793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012795 typval_T *argvars;
12796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797{
12798 char_u *name;
12799 char_u *mode;
12800 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012801 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012804 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 mode = (char_u *)"nvo";
12806 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012809 if (argvars[2].v_type != VAR_UNKNOWN)
12810 abbr = get_tv_number(&argvars[2]);
12811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812
Bram Moolenaar2c932302006-03-18 21:42:09 +000012813 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
12820 * "histadd()" function
12821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827#ifdef FEAT_CMDHIST
12828 int histype;
12829 char_u *str;
12830 char_u buf[NUMBUFLEN];
12831#endif
12832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 if (check_restricted() || check_secure())
12835 return;
12836#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12838 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 if (histype >= 0)
12840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 if (*str != NUL)
12843 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012844 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 return;
12848 }
12849 }
12850#endif
12851}
12852
12853/*
12854 * "histdel()" function
12855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012858 typval_T *argvars UNUSED;
12859 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860{
12861#ifdef FEAT_CMDHIST
12862 int n;
12863 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012864 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12867 if (str == NULL)
12868 n = 0;
12869 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012872 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 else
12877 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 get_tv_string_buf(&argvars[1], buf));
12880 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881#endif
12882}
12883
12884/*
12885 * "histget()" function
12886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
12892#ifdef FEAT_CMDHIST
12893 int type;
12894 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12898 if (str == NULL)
12899 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 {
12902 type = get_histtype(str);
12903 if (argvars[1].v_type == VAR_UNKNOWN)
12904 idx = get_history_idx(type);
12905 else
12906 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12907 /* -1 on type error */
12908 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914}
12915
12916/*
12917 * "histnr()" function
12918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923{
12924 int i;
12925
12926#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012927 char_u *history = get_tv_string_chk(&argvars[0]);
12928
12929 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 if (i >= HIST_CMD && i < HIST_COUNT)
12931 i = get_history_idx(i);
12932 else
12933#endif
12934 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 * "highlightID(name)" function
12940 */
12941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *argvars;
12944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
12949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950 * "highlight_exists()" function
12951 */
12952 static void
12953f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012954 typval_T *argvars;
12955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956{
12957 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12958}
12959
12960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 * "hostname()" function
12962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967{
12968 char_u hostname[256];
12969
12970 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 rettv->v_type = VAR_STRING;
12972 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973}
12974
12975/*
12976 * iconv() function
12977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982{
12983#ifdef FEAT_MBYTE
12984 char_u buf1[NUMBUFLEN];
12985 char_u buf2[NUMBUFLEN];
12986 char_u *from, *to, *str;
12987 vimconv_T vimconv;
12988#endif
12989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990 rettv->v_type = VAR_STRING;
12991 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992
12993#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012994 str = get_tv_string(&argvars[0]);
12995 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12996 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 vimconv.vc_type = CONV_NONE;
12998 convert_setup(&vimconv, from, to);
12999
13000 /* If the encodings are equal, no conversion needed. */
13001 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013004 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005
13006 convert_setup(&vimconv, NULL, NULL);
13007 vim_free(from);
13008 vim_free(to);
13009#endif
13010}
13011
13012/*
13013 * "indent()" function
13014 */
13015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013016f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013017 typval_T *argvars;
13018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019{
13020 linenr_T lnum;
13021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013029/*
13030 * "index()" function
13031 */
13032 static void
13033f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013036{
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 list_T *l;
13038 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013039 long idx = 0;
13040 int ic = FALSE;
13041
13042 rettv->vval.v_number = -1;
13043 if (argvars[0].v_type != VAR_LIST)
13044 {
13045 EMSG(_(e_listreq));
13046 return;
13047 }
13048 l = argvars[0].vval.v_list;
13049 if (l != NULL)
13050 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013051 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 int error = FALSE;
13055
Bram Moolenaar758711c2005-02-02 23:11:38 +000013056 /* Start at specified item. Use the cached index that list_find()
13057 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013059 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 ic = get_tv_number_chk(&argvars[3], &error);
13062 if (error)
13063 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013064 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013065
Bram Moolenaar758711c2005-02-02 23:11:38 +000013066 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013067 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013068 {
13069 rettv->vval.v_number = idx;
13070 break;
13071 }
13072 }
13073}
13074
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075static int inputsecret_flag = 0;
13076
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013077static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13078
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013080 * This function is used by f_input() and f_inputdialog() functions. The third
13081 * argument to f_input() specifies the type of completion to use at the
13082 * prompt. The third argument to f_inputdialog() specifies the value to return
13083 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 */
13085 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013086get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013089 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 char_u *p = NULL;
13093 int c;
13094 char_u buf[NUMBUFLEN];
13095 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013097 int xp_type = EXPAND_NOTHING;
13098 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
13103#ifdef NO_CONSOLE_INPUT
13104 /* While starting up, there is no place to enter text. */
13105 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107#endif
13108
13109 cmd_silent = FALSE; /* Want to see the prompt. */
13110 if (prompt != NULL)
13111 {
13112 /* Only the part of the message after the last NL is considered as
13113 * prompt for the command line */
13114 p = vim_strrchr(prompt, '\n');
13115 if (p == NULL)
13116 p = prompt;
13117 else
13118 {
13119 ++p;
13120 c = *p;
13121 *p = NUL;
13122 msg_start();
13123 msg_clr_eos();
13124 msg_puts_attr(prompt, echo_attr);
13125 msg_didout = FALSE;
13126 msg_starthere();
13127 *p = c;
13128 }
13129 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131 if (argvars[1].v_type != VAR_UNKNOWN)
13132 {
13133 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13134 if (defstr != NULL)
13135 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013137 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013138 {
13139 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013140 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013141 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013142
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013143 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013144 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013145
Bram Moolenaar4463f292005-09-25 22:20:24 +000013146 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13147 if (xp_name == NULL)
13148 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013150 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013151
Bram Moolenaar4463f292005-09-25 22:20:24 +000013152 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13153 &xp_arg) == FAIL)
13154 return;
13155 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013156 }
13157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013158 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013159 {
13160# ifdef FEAT_EX_EXTRA
13161 int save_ex_normal_busy = ex_normal_busy;
13162 ex_normal_busy = 0;
13163# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013165 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13166 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013167# ifdef FEAT_EX_EXTRA
13168 ex_normal_busy = save_ex_normal_busy;
13169# endif
13170 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013171 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013172 && argvars[1].v_type != VAR_UNKNOWN
13173 && argvars[2].v_type != VAR_UNKNOWN)
13174 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13175 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013176
13177 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 /* since the user typed this, no need to wait for return */
13180 need_wait_return = FALSE;
13181 msg_didout = FALSE;
13182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 cmd_silent = cmd_silent_save;
13184}
13185
13186/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013187 * "input()" function
13188 * Also handles inputsecret() when inputsecret is set.
13189 */
13190 static void
13191f_input(argvars, rettv)
13192 typval_T *argvars;
13193 typval_T *rettv;
13194{
13195 get_user_input(argvars, rettv, FALSE);
13196}
13197
13198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 * "inputdialog()" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206#if defined(FEAT_GUI_TEXTDIALOG)
13207 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13208 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13209 {
13210 char_u *message;
13211 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013214 message = get_tv_string_chk(&argvars[0]);
13215 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013216 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013217 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 else
13219 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 if (message != NULL && defstr != NULL
13221 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013222 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 else
13225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 if (message != NULL && defstr != NULL
13227 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_string = vim_strsave(
13230 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
13236 else
13237#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013238 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239}
13240
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013241/*
13242 * "inputlist()" function
13243 */
13244 static void
13245f_inputlist(argvars, rettv)
13246 typval_T *argvars;
13247 typval_T *rettv;
13248{
13249 listitem_T *li;
13250 int selected;
13251 int mouse_used;
13252
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013253#ifdef NO_CONSOLE_INPUT
13254 /* While starting up, there is no place to enter text. */
13255 if (no_console_input())
13256 return;
13257#endif
13258 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13259 {
13260 EMSG2(_(e_listarg), "inputlist()");
13261 return;
13262 }
13263
13264 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013265 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013266 lines_left = Rows; /* avoid more prompt */
13267 msg_scroll = TRUE;
13268 msg_clr_eos();
13269
13270 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13271 {
13272 msg_puts(get_tv_string(&li->li_tv));
13273 msg_putchar('\n');
13274 }
13275
13276 /* Ask for choice. */
13277 selected = prompt_for_number(&mouse_used);
13278 if (mouse_used)
13279 selected -= lines_left;
13280
13281 rettv->vval.v_number = selected;
13282}
13283
13284
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13286
13287/*
13288 * "inputrestore()" function
13289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
13295 if (ga_userinput.ga_len > 0)
13296 {
13297 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13299 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013300 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 }
13302 else if (p_verbose > 1)
13303 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013304 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 }
13307}
13308
13309/*
13310 * "inputsave()" function
13311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013317 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 if (ga_grow(&ga_userinput, 1) == OK)
13319 {
13320 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13321 + ga_userinput.ga_len);
13322 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013323 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 }
13325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327}
13328
13329/*
13330 * "inputsecret()" function
13331 */
13332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336{
13337 ++cmdline_star;
13338 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 --cmdline_star;
13341 --inputsecret_flag;
13342}
13343
13344/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013345 * "insert()" function
13346 */
13347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 typval_T *argvars;
13350 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013351{
13352 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 listitem_T *item;
13354 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356
13357 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013359 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013360 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361 {
13362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013363 before = get_tv_number_chk(&argvars[2], &error);
13364 if (error)
13365 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013366
Bram Moolenaar758711c2005-02-02 23:11:38 +000013367 if (before == l->lv_len)
13368 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013369 else
13370 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013371 item = list_find(l, before);
13372 if (item == NULL)
13373 {
13374 EMSGN(_(e_listidx), before);
13375 l = NULL;
13376 }
13377 }
13378 if (l != NULL)
13379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013380 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013381 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013382 }
13383 }
13384}
13385
13386/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013387 * "invert(expr)" function
13388 */
13389 static void
13390f_invert(argvars, rettv)
13391 typval_T *argvars;
13392 typval_T *rettv;
13393{
13394 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13395}
13396
13397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 * "isdirectory()" function
13399 */
13400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 typval_T *argvars;
13403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406}
13407
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013408/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013409 * "islocked()" function
13410 */
13411 static void
13412f_islocked(argvars, rettv)
13413 typval_T *argvars;
13414 typval_T *rettv;
13415{
13416 lval_T lv;
13417 char_u *end;
13418 dictitem_T *di;
13419
13420 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013421 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13422 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013423 if (end != NULL && lv.ll_name != NULL)
13424 {
13425 if (*end != NUL)
13426 EMSG(_(e_trailing));
13427 else
13428 {
13429 if (lv.ll_tv == NULL)
13430 {
13431 if (check_changedtick(lv.ll_name))
13432 rettv->vval.v_number = 1; /* always locked */
13433 else
13434 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013435 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013436 if (di != NULL)
13437 {
13438 /* Consider a variable locked when:
13439 * 1. the variable itself is locked
13440 * 2. the value of the variable is locked.
13441 * 3. the List or Dict value is locked.
13442 */
13443 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13444 || tv_islocked(&di->di_tv));
13445 }
13446 }
13447 }
13448 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013449 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013450 else if (lv.ll_newkey != NULL)
13451 EMSG2(_(e_dictkey), lv.ll_newkey);
13452 else if (lv.ll_list != NULL)
13453 /* List item. */
13454 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13455 else
13456 /* Dictionary item. */
13457 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13458 }
13459 }
13460
13461 clear_lval(&lv);
13462}
13463
Bram Moolenaar33570922005-01-25 22:26:29 +000013464static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013465
13466/*
13467 * Turn a dict into a list:
13468 * "what" == 0: list of keys
13469 * "what" == 1: list of values
13470 * "what" == 2: list of items
13471 */
13472 static void
13473dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013476 int what;
13477{
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 list_T *l2;
13479 dictitem_T *di;
13480 hashitem_T *hi;
13481 listitem_T *li;
13482 listitem_T *li2;
13483 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013484 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013485
Bram Moolenaar8c711452005-01-14 21:53:12 +000013486 if (argvars[0].v_type != VAR_DICT)
13487 {
13488 EMSG(_(e_dictreq));
13489 return;
13490 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013491 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492 return;
13493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013494 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013495 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013496
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013497 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013500 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013502 --todo;
13503 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013504
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013505 li = listitem_alloc();
13506 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013507 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013508 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013509
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013510 if (what == 0)
13511 {
13512 /* keys() */
13513 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013514 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013515 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13516 }
13517 else if (what == 1)
13518 {
13519 /* values() */
13520 copy_tv(&di->di_tv, &li->li_tv);
13521 }
13522 else
13523 {
13524 /* items() */
13525 l2 = list_alloc();
13526 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013527 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013528 li->li_tv.vval.v_list = l2;
13529 if (l2 == NULL)
13530 break;
13531 ++l2->lv_refcount;
13532
13533 li2 = listitem_alloc();
13534 if (li2 == NULL)
13535 break;
13536 list_append(l2, li2);
13537 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013538 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013539 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13540
13541 li2 = listitem_alloc();
13542 if (li2 == NULL)
13543 break;
13544 list_append(l2, li2);
13545 copy_tv(&di->di_tv, &li2->li_tv);
13546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013547 }
13548 }
13549}
13550
13551/*
13552 * "items(dict)" function
13553 */
13554 static void
13555f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013558{
13559 dict_list(argvars, rettv, 2);
13560}
13561
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 * "join()" function
13564 */
13565 static void
13566f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569{
13570 garray_T ga;
13571 char_u *sep;
13572
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573 if (argvars[0].v_type != VAR_LIST)
13574 {
13575 EMSG(_(e_listreq));
13576 return;
13577 }
13578 if (argvars[0].vval.v_list == NULL)
13579 return;
13580 if (argvars[1].v_type == VAR_UNKNOWN)
13581 sep = (char_u *)" ";
13582 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013584
13585 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586
13587 if (sep != NULL)
13588 {
13589 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013590 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591 ga_append(&ga, NUL);
13592 rettv->vval.v_string = (char_u *)ga.ga_data;
13593 }
13594 else
13595 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013596}
13597
13598/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013599 * "keys()" function
13600 */
13601 static void
13602f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *argvars;
13604 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013605{
13606 dict_list(argvars, rettv, 0);
13607}
13608
13609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 * "last_buffer_nr()" function.
13611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617 int n = 0;
13618 buf_T *buf;
13619
13620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13621 if (n < buf->b_fnum)
13622 n = buf->b_fnum;
13623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625}
13626
13627/*
13628 * "len()" function
13629 */
13630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634{
13635 switch (argvars[0].v_type)
13636 {
13637 case VAR_STRING:
13638 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_number = (varnumber_T)STRLEN(
13640 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 break;
13642 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013645 case VAR_DICT:
13646 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013648 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013649 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013650 break;
13651 }
13652}
13653
Bram Moolenaar33570922005-01-25 22:26:29 +000013654static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655
13656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 int type;
13661{
13662#ifdef FEAT_LIBCALL
13663 char_u *string_in;
13664 char_u **string_result;
13665 int nr_result;
13666#endif
13667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013669 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013671
13672 if (check_restricted() || check_secure())
13673 return;
13674
13675#ifdef FEAT_LIBCALL
13676 /* The first two args must be strings, otherwise its meaningless */
13677 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013679 string_in = NULL;
13680 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013681 string_in = argvars[2].vval.v_string;
13682 if (type == VAR_NUMBER)
13683 string_result = NULL;
13684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013686 if (mch_libcall(argvars[0].vval.v_string,
13687 argvars[1].vval.v_string,
13688 string_in,
13689 argvars[2].vval.v_number,
13690 string_result,
13691 &nr_result) == OK
13692 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 }
13695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696}
13697
13698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699 * "libcall()" function
13700 */
13701 static void
13702f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *argvars;
13704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013705{
13706 libcall_common(argvars, rettv, VAR_STRING);
13707}
13708
13709/*
13710 * "libcallnr()" function
13711 */
13712 static void
13713f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *argvars;
13715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013716{
13717 libcall_common(argvars, rettv, VAR_NUMBER);
13718}
13719
13720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 * "line(string)" function
13722 */
13723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *argvars;
13726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727{
13728 linenr_T lnum = 0;
13729 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013730 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013732 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 if (fp != NULL)
13734 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736}
13737
13738/*
13739 * "line2byte(lnum)" function
13740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
13746#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#else
13749 linenr_T lnum;
13750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13756 if (rettv->vval.v_number >= 0)
13757 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#endif
13759}
13760
13761/*
13762 * "lispindent(lnum)" function
13763 */
13764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768{
13769#ifdef FEAT_LISP
13770 pos_T pos;
13771 linenr_T lnum;
13772
13773 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13776 {
13777 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 curwin->w_cursor = pos;
13780 }
13781 else
13782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784}
13785
13786/*
13787 * "localtime()" function
13788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795}
13796
Bram Moolenaar33570922005-01-25 22:26:29 +000013797static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798
13799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 int exact;
13804{
13805 char_u *keys;
13806 char_u *which;
13807 char_u buf[NUMBUFLEN];
13808 char_u *keys_buf = NULL;
13809 char_u *rhs;
13810 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013811 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013812 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013813 mapblock_T *mp;
13814 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
13816 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 rettv->v_type = VAR_STRING;
13818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 if (*keys == NUL)
13822 return;
13823
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013828 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013829 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013830 if (argvars[3].v_type != VAR_UNKNOWN)
13831 get_dict = get_tv_number(&argvars[3]);
13832 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 else
13835 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 if (which == NULL)
13837 return;
13838
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 mode = get_map_mode(&which, 0);
13840
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013841 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013842 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013844
13845 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013847 /* Return a string. */
13848 if (rhs != NULL)
13849 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaarbd743252010-10-20 21:23:33 +020013851 }
13852 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13853 {
13854 /* Return a dictionary. */
13855 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13856 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13857 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
Bram Moolenaarbd743252010-10-20 21:23:33 +020013859 dict_add_nr_str(dict, "lhs", 0L, lhs);
13860 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13861 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13862 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13863 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13864 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13865 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013866 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013867 dict_add_nr_str(dict, "mode", 0L, mapmode);
13868
13869 vim_free(lhs);
13870 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 }
13872}
13873
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013874#ifdef FEAT_FLOAT
13875/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013876 * "log()" function
13877 */
13878 static void
13879f_log(argvars, rettv)
13880 typval_T *argvars;
13881 typval_T *rettv;
13882{
13883 float_T f;
13884
13885 rettv->v_type = VAR_FLOAT;
13886 if (get_float_arg(argvars, &f) == OK)
13887 rettv->vval.v_float = log(f);
13888 else
13889 rettv->vval.v_float = 0.0;
13890}
13891
13892/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013893 * "log10()" function
13894 */
13895 static void
13896f_log10(argvars, rettv)
13897 typval_T *argvars;
13898 typval_T *rettv;
13899{
13900 float_T f;
13901
13902 rettv->v_type = VAR_FLOAT;
13903 if (get_float_arg(argvars, &f) == OK)
13904 rettv->vval.v_float = log10(f);
13905 else
13906 rettv->vval.v_float = 0.0;
13907}
13908#endif
13909
Bram Moolenaar1dced572012-04-05 16:54:08 +020013910#ifdef FEAT_LUA
13911/*
13912 * "luaeval()" function
13913 */
13914 static void
13915f_luaeval(argvars, rettv)
13916 typval_T *argvars;
13917 typval_T *rettv;
13918{
13919 char_u *str;
13920 char_u buf[NUMBUFLEN];
13921
13922 str = get_tv_string_buf(&argvars[0], buf);
13923 do_luaeval(str, argvars + 1, rettv);
13924}
13925#endif
13926
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 * "map()" function
13929 */
13930 static void
13931f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013934{
13935 filter_map(argvars, rettv, TRUE);
13936}
13937
13938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 */
13941 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013943 typval_T *argvars;
13944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013946 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947}
13948
13949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 */
13952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013954 typval_T *argvars;
13955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013957 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958}
13959
Bram Moolenaar33570922005-01-25 22:26:29 +000013960static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
13962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013963find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 int type;
13967{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013969 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 char_u *pat;
13972 regmatch_T regmatch;
13973 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013974 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 char_u *save_cpo;
13976 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013977 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013978 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013979 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013980 list_T *l = NULL;
13981 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013982 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013983 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984
13985 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13986 save_cpo = p_cpo;
13987 p_cpo = (char_u *)"";
13988
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013989 rettv->vval.v_number = -1;
13990 if (type == 3)
13991 {
13992 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013993 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013994 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013995 }
13996 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 rettv->v_type = VAR_STRING;
13999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 if (argvars[0].v_type == VAR_LIST)
14003 {
14004 if ((l = argvars[0].vval.v_list) == NULL)
14005 goto theend;
14006 li = l->lv_first;
14007 }
14008 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014009 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014011 len = (long)STRLEN(str);
14012 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14015 if (pat == NULL)
14016 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020 int error = FALSE;
14021
14022 start = get_tv_number_chk(&argvars[2], &error);
14023 if (error)
14024 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025 if (l != NULL)
14026 {
14027 li = list_find(l, start);
14028 if (li == NULL)
14029 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014030 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014031 }
14032 else
14033 {
14034 if (start < 0)
14035 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014036 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014037 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014038 /* When "count" argument is there ignore matches before "start",
14039 * otherwise skip part of the string. Differs when pattern is "^"
14040 * or "\<". */
14041 if (argvars[3].v_type != VAR_UNKNOWN)
14042 startcol = start;
14043 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014044 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014045 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014046 len -= start;
14047 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014048 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014049
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 nth = get_tv_number_chk(&argvars[3], &error);
14052 if (error)
14053 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 }
14055
14056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14057 if (regmatch.regprog != NULL)
14058 {
14059 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014060
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014061 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014062 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014063 if (l != NULL)
14064 {
14065 if (li == NULL)
14066 {
14067 match = FALSE;
14068 break;
14069 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014070 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014071 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014072 if (str == NULL)
14073 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014074 }
14075
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014076 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014079 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080 if (l == NULL && !match)
14081 break;
14082
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014083 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084 if (l != NULL)
14085 {
14086 li = li->li_next;
14087 ++idx;
14088 }
14089 else
14090 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014091#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014092 startcol = (colnr_T)(regmatch.startp[0]
14093 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014094#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014095 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014096#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014097 if (startcol > (colnr_T)len
14098 || str + startcol <= regmatch.startp[0])
14099 {
14100 match = FALSE;
14101 break;
14102 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014103 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014104 }
14105
14106 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014108 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014109 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014110 int i;
14111
14112 /* return list with matched string and submatches */
14113 for (i = 0; i < NSUBEXP; ++i)
14114 {
14115 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014116 {
14117 if (list_append_string(rettv->vval.v_list,
14118 (char_u *)"", 0) == FAIL)
14119 break;
14120 }
14121 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014122 regmatch.startp[i],
14123 (int)(regmatch.endp[i] - regmatch.startp[i]))
14124 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014125 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014126 }
14127 }
14128 else if (type == 2)
14129 {
14130 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014131 if (l != NULL)
14132 copy_tv(&li->li_tv, rettv);
14133 else
14134 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014136 }
14137 else if (l != NULL)
14138 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 else
14140 {
14141 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 (varnumber_T)(regmatch.startp[0] - str);
14144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014147 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 }
14149 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014150 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 }
14152
14153theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014154 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 p_cpo = save_cpo;
14156}
14157
14158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159 * "match()" function
14160 */
14161 static void
14162f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 typval_T *argvars;
14164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165{
14166 find_some_match(argvars, rettv, 1);
14167}
14168
14169/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014170 * "matchadd()" function
14171 */
14172 static void
14173f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014174 typval_T *argvars UNUSED;
14175 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014176{
14177#ifdef FEAT_SEARCH_EXTRA
14178 char_u buf[NUMBUFLEN];
14179 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14180 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14181 int prio = 10; /* default priority */
14182 int id = -1;
14183 int error = FALSE;
14184
14185 rettv->vval.v_number = -1;
14186
14187 if (grp == NULL || pat == NULL)
14188 return;
14189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014191 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014192 if (argvars[3].v_type != VAR_UNKNOWN)
14193 id = get_tv_number_chk(&argvars[3], &error);
14194 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014195 if (error == TRUE)
14196 return;
14197 if (id >= 1 && id <= 3)
14198 {
14199 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14200 return;
14201 }
14202
14203 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14204#endif
14205}
14206
14207/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014208 * "matcharg()" function
14209 */
14210 static void
14211f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014212 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014213 typval_T *rettv;
14214{
14215 if (rettv_list_alloc(rettv) == OK)
14216 {
14217#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014218 int id = get_tv_number(&argvars[0]);
14219 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014220
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014221 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014222 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014223 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14224 {
14225 list_append_string(rettv->vval.v_list,
14226 syn_id2name(m->hlg_id), -1);
14227 list_append_string(rettv->vval.v_list, m->pattern, -1);
14228 }
14229 else
14230 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014231 list_append_string(rettv->vval.v_list, NULL, -1);
14232 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014233 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014234 }
14235#endif
14236 }
14237}
14238
14239/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014240 * "matchdelete()" function
14241 */
14242 static void
14243f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014244 typval_T *argvars UNUSED;
14245 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014246{
14247#ifdef FEAT_SEARCH_EXTRA
14248 rettv->vval.v_number = match_delete(curwin,
14249 (int)get_tv_number(&argvars[0]), TRUE);
14250#endif
14251}
14252
14253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254 * "matchend()" function
14255 */
14256 static void
14257f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 typval_T *argvars;
14259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014260{
14261 find_some_match(argvars, rettv, 0);
14262}
14263
14264/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014265 * "matchlist()" function
14266 */
14267 static void
14268f_matchlist(argvars, rettv)
14269 typval_T *argvars;
14270 typval_T *rettv;
14271{
14272 find_some_match(argvars, rettv, 3);
14273}
14274
14275/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276 * "matchstr()" function
14277 */
14278 static void
14279f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014282{
14283 find_some_match(argvars, rettv, 2);
14284}
14285
Bram Moolenaar33570922005-01-25 22:26:29 +000014286static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014287
14288 static void
14289max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014290 typval_T *argvars;
14291 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014292 int domax;
14293{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014294 long n = 0;
14295 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014297
14298 if (argvars[0].v_type == VAR_LIST)
14299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 list_T *l;
14301 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014302
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014303 l = argvars[0].vval.v_list;
14304 if (l != NULL)
14305 {
14306 li = l->lv_first;
14307 if (li != NULL)
14308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014310 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014311 {
14312 li = li->li_next;
14313 if (li == NULL)
14314 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014316 if (domax ? i > n : i < n)
14317 n = i;
14318 }
14319 }
14320 }
14321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014322 else if (argvars[0].v_type == VAR_DICT)
14323 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014324 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014325 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014326 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014327 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328
14329 d = argvars[0].vval.v_dict;
14330 if (d != NULL)
14331 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014332 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014334 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014335 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014339 if (first)
14340 {
14341 n = i;
14342 first = FALSE;
14343 }
14344 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014345 n = i;
14346 }
14347 }
14348 }
14349 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014350 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014351 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014353}
14354
14355/*
14356 * "max()" function
14357 */
14358 static void
14359f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014362{
14363 max_min(argvars, rettv, TRUE);
14364}
14365
14366/*
14367 * "min()" function
14368 */
14369 static void
14370f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014373{
14374 max_min(argvars, rettv, FALSE);
14375}
14376
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014377static int mkdir_recurse __ARGS((char_u *dir, int prot));
14378
14379/*
14380 * Create the directory in which "dir" is located, and higher levels when
14381 * needed.
14382 */
14383 static int
14384mkdir_recurse(dir, prot)
14385 char_u *dir;
14386 int prot;
14387{
14388 char_u *p;
14389 char_u *updir;
14390 int r = FAIL;
14391
14392 /* Get end of directory name in "dir".
14393 * We're done when it's "/" or "c:/". */
14394 p = gettail_sep(dir);
14395 if (p <= get_past_head(dir))
14396 return OK;
14397
14398 /* If the directory exists we're done. Otherwise: create it.*/
14399 updir = vim_strnsave(dir, (int)(p - dir));
14400 if (updir == NULL)
14401 return FAIL;
14402 if (mch_isdir(updir))
14403 r = OK;
14404 else if (mkdir_recurse(updir, prot) == OK)
14405 r = vim_mkdir_emsg(updir, prot);
14406 vim_free(updir);
14407 return r;
14408}
14409
14410#ifdef vim_mkdir
14411/*
14412 * "mkdir()" function
14413 */
14414 static void
14415f_mkdir(argvars, rettv)
14416 typval_T *argvars;
14417 typval_T *rettv;
14418{
14419 char_u *dir;
14420 char_u buf[NUMBUFLEN];
14421 int prot = 0755;
14422
14423 rettv->vval.v_number = FAIL;
14424 if (check_restricted() || check_secure())
14425 return;
14426
14427 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014428 if (*dir == NUL)
14429 rettv->vval.v_number = FAIL;
14430 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014431 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014432 if (*gettail(dir) == NUL)
14433 /* remove trailing slashes */
14434 *gettail_sep(dir) = NUL;
14435
14436 if (argvars[1].v_type != VAR_UNKNOWN)
14437 {
14438 if (argvars[2].v_type != VAR_UNKNOWN)
14439 prot = get_tv_number_chk(&argvars[2], NULL);
14440 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14441 mkdir_recurse(dir, prot);
14442 }
14443 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014444 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014445}
14446#endif
14447
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 * "mode()" function
14450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014453 typval_T *argvars;
14454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014456 char_u buf[3];
14457
14458 buf[1] = NUL;
14459 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 if (VIsual_active)
14462 {
14463 if (VIsual_select)
14464 buf[0] = VIsual_mode + 's' - 'v';
14465 else
14466 buf[0] = VIsual_mode;
14467 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014468 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014469 || State == CONFIRM)
14470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014472 if (State == ASKMORE)
14473 buf[1] = 'm';
14474 else if (State == CONFIRM)
14475 buf[1] = '?';
14476 }
14477 else if (State == EXTERNCMD)
14478 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 else if (State & INSERT)
14480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014481#ifdef FEAT_VREPLACE
14482 if (State & VREPLACE_FLAG)
14483 {
14484 buf[0] = 'R';
14485 buf[1] = 'v';
14486 }
14487 else
14488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (State & REPLACE_FLAG)
14490 buf[0] = 'R';
14491 else
14492 buf[0] = 'i';
14493 }
14494 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014497 if (exmode_active)
14498 buf[1] = 'v';
14499 }
14500 else if (exmode_active)
14501 {
14502 buf[0] = 'c';
14503 buf[1] = 'e';
14504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014508 if (finish_op)
14509 buf[1] = 'o';
14510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014512 /* Clear out the minor mode when the argument is not a non-zero number or
14513 * non-empty string. */
14514 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014515 buf[1] = NUL;
14516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->vval.v_string = vim_strsave(buf);
14518 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
Bram Moolenaar429fa852013-04-15 12:27:36 +020014521#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014522/*
14523 * "mzeval()" function
14524 */
14525 static void
14526f_mzeval(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530 char_u *str;
14531 char_u buf[NUMBUFLEN];
14532
14533 str = get_tv_string_buf(&argvars[0], buf);
14534 do_mzeval(str, rettv);
14535}
Bram Moolenaar75676462013-01-30 14:55:42 +010014536
14537 void
14538mzscheme_call_vim(name, args, rettv)
14539 char_u *name;
14540 typval_T *args;
14541 typval_T *rettv;
14542{
14543 typval_T argvars[3];
14544
14545 argvars[0].v_type = VAR_STRING;
14546 argvars[0].vval.v_string = name;
14547 copy_tv(args, &argvars[1]);
14548 argvars[2].v_type = VAR_UNKNOWN;
14549 f_call(argvars, rettv);
14550 clear_tv(&argvars[1]);
14551}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014552#endif
14553
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555 * "nextnonblank()" function
14556 */
14557 static void
14558f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 typval_T *argvars;
14560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561{
14562 linenr_T lnum;
14563
14564 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 {
14568 lnum = 0;
14569 break;
14570 }
14571 if (*skipwhite(ml_get(lnum)) != NUL)
14572 break;
14573 }
14574 rettv->vval.v_number = lnum;
14575}
14576
14577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 * "nr2char()" function
14579 */
14580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 typval_T *argvars;
14583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
14585 char_u buf[NUMBUFLEN];
14586
14587#ifdef FEAT_MBYTE
14588 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014589 {
14590 int utf8 = 0;
14591
14592 if (argvars[1].v_type != VAR_UNKNOWN)
14593 utf8 = get_tv_number_chk(&argvars[1], NULL);
14594 if (utf8)
14595 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14596 else
14597 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 else
14600#endif
14601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 buf[1] = NUL;
14604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607}
14608
14609/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014610 * "or(expr, expr)" function
14611 */
14612 static void
14613f_or(argvars, rettv)
14614 typval_T *argvars;
14615 typval_T *rettv;
14616{
14617 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14618 | get_tv_number_chk(&argvars[1], NULL);
14619}
14620
14621/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014622 * "pathshorten()" function
14623 */
14624 static void
14625f_pathshorten(argvars, rettv)
14626 typval_T *argvars;
14627 typval_T *rettv;
14628{
14629 char_u *p;
14630
14631 rettv->v_type = VAR_STRING;
14632 p = get_tv_string_chk(&argvars[0]);
14633 if (p == NULL)
14634 rettv->vval.v_string = NULL;
14635 else
14636 {
14637 p = vim_strsave(p);
14638 rettv->vval.v_string = p;
14639 if (p != NULL)
14640 shorten_dir(p);
14641 }
14642}
14643
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014644#ifdef FEAT_FLOAT
14645/*
14646 * "pow()" function
14647 */
14648 static void
14649f_pow(argvars, rettv)
14650 typval_T *argvars;
14651 typval_T *rettv;
14652{
14653 float_T fx, fy;
14654
14655 rettv->v_type = VAR_FLOAT;
14656 if (get_float_arg(argvars, &fx) == OK
14657 && get_float_arg(&argvars[1], &fy) == OK)
14658 rettv->vval.v_float = pow(fx, fy);
14659 else
14660 rettv->vval.v_float = 0.0;
14661}
14662#endif
14663
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 * "prevnonblank()" function
14666 */
14667 static void
14668f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 typval_T *argvars;
14670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671{
14672 linenr_T lnum;
14673
14674 lnum = get_tv_lnum(argvars);
14675 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14676 lnum = 0;
14677 else
14678 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14679 --lnum;
14680 rettv->vval.v_number = lnum;
14681}
14682
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014683#ifdef HAVE_STDARG_H
14684/* This dummy va_list is here because:
14685 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14686 * - locally in the function results in a "used before set" warning
14687 * - using va_start() to initialize it gives "function with fixed args" error */
14688static va_list ap;
14689#endif
14690
Bram Moolenaar8c711452005-01-14 21:53:12 +000014691/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014692 * "printf()" function
14693 */
14694 static void
14695f_printf(argvars, rettv)
14696 typval_T *argvars;
14697 typval_T *rettv;
14698{
14699 rettv->v_type = VAR_STRING;
14700 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014701#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014702 {
14703 char_u buf[NUMBUFLEN];
14704 int len;
14705 char_u *s;
14706 int saved_did_emsg = did_emsg;
14707 char *fmt;
14708
14709 /* Get the required length, allocate the buffer and do it for real. */
14710 did_emsg = FALSE;
14711 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014712 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014713 if (!did_emsg)
14714 {
14715 s = alloc(len + 1);
14716 if (s != NULL)
14717 {
14718 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014719 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014720 }
14721 }
14722 did_emsg |= saved_did_emsg;
14723 }
14724#endif
14725}
14726
14727/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014728 * "pumvisible()" function
14729 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014730 static void
14731f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014732 typval_T *argvars UNUSED;
14733 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014734{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014735#ifdef FEAT_INS_EXPAND
14736 if (pum_visible())
14737 rettv->vval.v_number = 1;
14738#endif
14739}
14740
Bram Moolenaardb913952012-06-29 12:54:53 +020014741#ifdef FEAT_PYTHON3
14742/*
14743 * "py3eval()" function
14744 */
14745 static void
14746f_py3eval(argvars, rettv)
14747 typval_T *argvars;
14748 typval_T *rettv;
14749{
14750 char_u *str;
14751 char_u buf[NUMBUFLEN];
14752
14753 str = get_tv_string_buf(&argvars[0], buf);
14754 do_py3eval(str, rettv);
14755}
14756#endif
14757
14758#ifdef FEAT_PYTHON
14759/*
14760 * "pyeval()" function
14761 */
14762 static void
14763f_pyeval(argvars, rettv)
14764 typval_T *argvars;
14765 typval_T *rettv;
14766{
14767 char_u *str;
14768 char_u buf[NUMBUFLEN];
14769
14770 str = get_tv_string_buf(&argvars[0], buf);
14771 do_pyeval(str, rettv);
14772}
14773#endif
14774
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014775/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014776 * "range()" function
14777 */
14778 static void
14779f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *argvars;
14781 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014782{
14783 long start;
14784 long end;
14785 long stride = 1;
14786 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014790 if (argvars[1].v_type == VAR_UNKNOWN)
14791 {
14792 end = start - 1;
14793 start = 0;
14794 }
14795 else
14796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 }
14801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 if (error)
14803 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014805 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014806 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014807 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014808 else
14809 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014810 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014812 if (list_append_number(rettv->vval.v_list,
14813 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014814 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014815 }
14816}
14817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014818/*
14819 * "readfile()" function
14820 */
14821 static void
14822f_readfile(argvars, rettv)
14823 typval_T *argvars;
14824 typval_T *rettv;
14825{
14826 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014828 char_u *fname;
14829 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14831 int io_size = sizeof(buf);
14832 int readlen; /* size of last fread() */
14833 char_u *prev = NULL; /* previously read bytes, if any */
14834 long prevlen = 0; /* length of data in prev */
14835 long prevsize = 0; /* size of prev buffer */
14836 long maxline = MAXLNUM;
14837 long cnt = 0;
14838 char_u *p; /* position in buf */
14839 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014841 if (argvars[1].v_type != VAR_UNKNOWN)
14842 {
14843 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14844 binary = TRUE;
14845 if (argvars[2].v_type != VAR_UNKNOWN)
14846 maxline = get_tv_number(&argvars[2]);
14847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014848
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014849 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014850 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851
14852 /* Always open the file in binary mode, library functions have a mind of
14853 * their own about CR-LF conversion. */
14854 fname = get_tv_string(&argvars[0]);
14855 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14856 {
14857 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14858 return;
14859 }
14860
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014861 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014863 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014864
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014865 /* This for loop processes what was read, but is also entered at end
14866 * of file so that either:
14867 * - an incomplete line gets written
14868 * - a "binary" file gets an empty line at the end if it ends in a
14869 * newline. */
14870 for (p = buf, start = buf;
14871 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14872 ++p)
14873 {
14874 if (*p == '\n' || readlen <= 0)
14875 {
14876 listitem_T *li;
14877 char_u *s = NULL;
14878 long_u len = p - start;
14879
14880 /* Finished a line. Remove CRs before NL. */
14881 if (readlen > 0 && !binary)
14882 {
14883 while (len > 0 && start[len - 1] == '\r')
14884 --len;
14885 /* removal may cross back to the "prev" string */
14886 if (len == 0)
14887 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14888 --prevlen;
14889 }
14890 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014891 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014892 else
14893 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014894 /* Change "prev" buffer to be the right size. This way
14895 * the bytes are only copied once, and very long lines are
14896 * allocated only once. */
14897 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014899 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 prev = NULL; /* the list will own the string */
14902 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014903 }
14904 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014905 if (s == NULL)
14906 {
14907 do_outofmem_msg((long_u) prevlen + len + 1);
14908 failed = TRUE;
14909 break;
14910 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014911
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014912 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014913 {
14914 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014915 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014916 break;
14917 }
14918 li->li_tv.v_type = VAR_STRING;
14919 li->li_tv.v_lock = 0;
14920 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014921 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014923 start = p + 1; /* step over newline */
14924 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014925 break;
14926 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 else if (*p == NUL)
14928 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014929#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14931 * when finding the BF and check the previous two bytes. */
14932 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014933 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14935 * + 1, these may be in the "prev" string. */
14936 char_u back1 = p >= buf + 1 ? p[-1]
14937 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14938 char_u back2 = p >= buf + 2 ? p[-2]
14939 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14940 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014941
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014942 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014943 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014944 char_u *dest = p - 2;
14945
14946 /* Usually a BOM is at the beginning of a file, and so at
14947 * the beginning of a line; then we can just step over it.
14948 */
14949 if (start == dest)
14950 start = p + 1;
14951 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014952 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014953 /* have to shuffle buf to close gap */
14954 int adjust_prevlen = 0;
14955
14956 if (dest < buf)
14957 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014958 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014959 dest = buf;
14960 }
14961 if (readlen > p - buf + 1)
14962 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14963 readlen -= 3 - adjust_prevlen;
14964 prevlen -= adjust_prevlen;
14965 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014966 }
14967 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014968 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014969#endif
14970 } /* for */
14971
14972 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14973 break;
14974 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976 /* There's part of a line in buf, store it in "prev". */
14977 if (p - start + prevlen >= prevsize)
14978 {
14979 /* need bigger "prev" buffer */
14980 char_u *newprev;
14981
14982 /* A common use case is ordinary text files and "prev" gets a
14983 * fragment of a line, so the first allocation is made
14984 * small, to avoid repeatedly 'allocing' large and
14985 * 'reallocing' small. */
14986 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014987 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014988 else
14989 {
14990 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014991 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014992 prevsize = grow50pc > growmin ? grow50pc : growmin;
14993 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014994 newprev = prev == NULL ? alloc(prevsize)
14995 : vim_realloc(prev, prevsize);
14996 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014997 {
14998 do_outofmem_msg((long_u)prevsize);
14999 failed = TRUE;
15000 break;
15001 }
15002 prev = newprev;
15003 }
15004 /* Add the line part to end of "prev". */
15005 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015006 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015007 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015008 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015010 /*
15011 * For a negative line count use only the lines at the end of the file,
15012 * free the rest.
15013 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015014 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015015 while (cnt > -maxline)
15016 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015017 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015018 --cnt;
15019 }
15020
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015021 if (failed)
15022 {
15023 list_free(rettv->vval.v_list, TRUE);
15024 /* readfile doc says an empty list is returned on error */
15025 rettv->vval.v_list = list_alloc();
15026 }
15027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015028 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015029 fclose(fd);
15030}
15031
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015032#if defined(FEAT_RELTIME)
15033static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15034
15035/*
15036 * Convert a List to proftime_T.
15037 * Return FAIL when there is something wrong.
15038 */
15039 static int
15040list2proftime(arg, tm)
15041 typval_T *arg;
15042 proftime_T *tm;
15043{
15044 long n1, n2;
15045 int error = FALSE;
15046
15047 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15048 || arg->vval.v_list->lv_len != 2)
15049 return FAIL;
15050 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15051 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15052# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015053 tm->HighPart = n1;
15054 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015055# else
15056 tm->tv_sec = n1;
15057 tm->tv_usec = n2;
15058# endif
15059 return error ? FAIL : OK;
15060}
15061#endif /* FEAT_RELTIME */
15062
15063/*
15064 * "reltime()" function
15065 */
15066 static void
15067f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015068 typval_T *argvars UNUSED;
15069 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015070{
15071#ifdef FEAT_RELTIME
15072 proftime_T res;
15073 proftime_T start;
15074
15075 if (argvars[0].v_type == VAR_UNKNOWN)
15076 {
15077 /* No arguments: get current time. */
15078 profile_start(&res);
15079 }
15080 else if (argvars[1].v_type == VAR_UNKNOWN)
15081 {
15082 if (list2proftime(&argvars[0], &res) == FAIL)
15083 return;
15084 profile_end(&res);
15085 }
15086 else
15087 {
15088 /* Two arguments: compute the difference. */
15089 if (list2proftime(&argvars[0], &start) == FAIL
15090 || list2proftime(&argvars[1], &res) == FAIL)
15091 return;
15092 profile_sub(&res, &start);
15093 }
15094
15095 if (rettv_list_alloc(rettv) == OK)
15096 {
15097 long n1, n2;
15098
15099# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015100 n1 = res.HighPart;
15101 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015102# else
15103 n1 = res.tv_sec;
15104 n2 = res.tv_usec;
15105# endif
15106 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15107 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15108 }
15109#endif
15110}
15111
15112/*
15113 * "reltimestr()" function
15114 */
15115 static void
15116f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015117 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015118 typval_T *rettv;
15119{
15120#ifdef FEAT_RELTIME
15121 proftime_T tm;
15122#endif
15123
15124 rettv->v_type = VAR_STRING;
15125 rettv->vval.v_string = NULL;
15126#ifdef FEAT_RELTIME
15127 if (list2proftime(&argvars[0], &tm) == OK)
15128 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15129#endif
15130}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015131
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15133static void make_connection __ARGS((void));
15134static int check_connection __ARGS((void));
15135
15136 static void
15137make_connection()
15138{
15139 if (X_DISPLAY == NULL
15140# ifdef FEAT_GUI
15141 && !gui.in_use
15142# endif
15143 )
15144 {
15145 x_force_connect = TRUE;
15146 setup_term_clip();
15147 x_force_connect = FALSE;
15148 }
15149}
15150
15151 static int
15152check_connection()
15153{
15154 make_connection();
15155 if (X_DISPLAY == NULL)
15156 {
15157 EMSG(_("E240: No connection to Vim server"));
15158 return FAIL;
15159 }
15160 return OK;
15161}
15162#endif
15163
15164#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015165static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166
15167 static void
15168remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *argvars;
15170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 int expr;
15172{
15173 char_u *server_name;
15174 char_u *keys;
15175 char_u *r = NULL;
15176 char_u buf[NUMBUFLEN];
15177# ifdef WIN32
15178 HWND w;
15179# else
15180 Window w;
15181# endif
15182
15183 if (check_restricted() || check_secure())
15184 return;
15185
15186# ifdef FEAT_X11
15187 if (check_connection() == FAIL)
15188 return;
15189# endif
15190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015191 server_name = get_tv_string_chk(&argvars[0]);
15192 if (server_name == NULL)
15193 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194 keys = get_tv_string_buf(&argvars[1], buf);
15195# ifdef WIN32
15196 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15197# else
15198 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15199 < 0)
15200# endif
15201 {
15202 if (r != NULL)
15203 EMSG(r); /* sending worked but evaluation failed */
15204 else
15205 EMSG2(_("E241: Unable to send to %s"), server_name);
15206 return;
15207 }
15208
15209 rettv->vval.v_string = r;
15210
15211 if (argvars[2].v_type != VAR_UNKNOWN)
15212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015214 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015217 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 v.di_tv.v_type = VAR_STRING;
15219 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 idvar = get_tv_string_chk(&argvars[2]);
15221 if (idvar != NULL)
15222 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015223 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 }
15225}
15226#endif
15227
15228/*
15229 * "remote_expr()" function
15230 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 static void
15232f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015233 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015235{
15236 rettv->v_type = VAR_STRING;
15237 rettv->vval.v_string = NULL;
15238#ifdef FEAT_CLIENTSERVER
15239 remote_common(argvars, rettv, TRUE);
15240#endif
15241}
15242
15243/*
15244 * "remote_foreground()" function
15245 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246 static void
15247f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015248 typval_T *argvars UNUSED;
15249 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251#ifdef FEAT_CLIENTSERVER
15252# ifdef WIN32
15253 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 {
15255 char_u *server_name = get_tv_string_chk(&argvars[0]);
15256
15257 if (server_name != NULL)
15258 serverForeground(server_name);
15259 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260# else
15261 /* Send a foreground() expression to the server. */
15262 argvars[1].v_type = VAR_STRING;
15263 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15264 argvars[2].v_type = VAR_UNKNOWN;
15265 remote_common(argvars, rettv, TRUE);
15266 vim_free(argvars[1].vval.v_string);
15267# endif
15268#endif
15269}
15270
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271 static void
15272f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015273 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275{
15276#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278 char_u *s = NULL;
15279# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015280 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283
15284 if (check_restricted() || check_secure())
15285 {
15286 rettv->vval.v_number = -1;
15287 return;
15288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 serverid = get_tv_string_chk(&argvars[0]);
15290 if (serverid == NULL)
15291 {
15292 rettv->vval.v_number = -1;
15293 return; /* type error; errmsg already given */
15294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015296 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 if (n == 0)
15298 rettv->vval.v_number = -1;
15299 else
15300 {
15301 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15302 rettv->vval.v_number = (s != NULL);
15303 }
15304# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305 if (check_connection() == FAIL)
15306 return;
15307
15308 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015310# endif
15311
15312 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 char_u *retvar;
15315
Bram Moolenaar33570922005-01-25 22:26:29 +000015316 v.di_tv.v_type = VAR_STRING;
15317 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 retvar = get_tv_string_chk(&argvars[1]);
15319 if (retvar != NULL)
15320 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015321 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 }
15323#else
15324 rettv->vval.v_number = -1;
15325#endif
15326}
15327
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 static void
15329f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015330 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332{
15333 char_u *r = NULL;
15334
15335#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015336 char_u *serverid = get_tv_string_chk(&argvars[0]);
15337
15338 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 {
15340# ifdef WIN32
15341 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015342 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015344 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015345 if (n != 0)
15346 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15347 if (r == NULL)
15348# else
15349 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015350 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351# endif
15352 EMSG(_("E277: Unable to read a server reply"));
15353 }
15354#endif
15355 rettv->v_type = VAR_STRING;
15356 rettv->vval.v_string = r;
15357}
15358
15359/*
15360 * "remote_send()" function
15361 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 static void
15363f_remote_send(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 rettv->v_type = VAR_STRING;
15368 rettv->vval.v_string = NULL;
15369#ifdef FEAT_CLIENTSERVER
15370 remote_common(argvars, rettv, FALSE);
15371#endif
15372}
15373
15374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015375 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015376 */
15377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015379 typval_T *argvars;
15380 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015381{
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 list_T *l;
15383 listitem_T *item, *item2;
15384 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015385 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015386 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015387 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 dict_T *d;
15389 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015390 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015391
Bram Moolenaar8c711452005-01-14 21:53:12 +000015392 if (argvars[0].v_type == VAR_DICT)
15393 {
15394 if (argvars[2].v_type != VAR_UNKNOWN)
15395 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015396 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015397 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015399 key = get_tv_string_chk(&argvars[1]);
15400 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015402 di = dict_find(d, key, -1);
15403 if (di == NULL)
15404 EMSG2(_(e_dictkey), key);
15405 else
15406 {
15407 *rettv = di->di_tv;
15408 init_tv(&di->di_tv);
15409 dictitem_remove(d, di);
15410 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015412 }
15413 }
15414 else if (argvars[0].v_type != VAR_LIST)
15415 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015416 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015417 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 int error = FALSE;
15420
15421 idx = get_tv_number_chk(&argvars[1], &error);
15422 if (error)
15423 ; /* type error: do nothing, errmsg already given */
15424 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015425 EMSGN(_(e_listidx), idx);
15426 else
15427 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015428 if (argvars[2].v_type == VAR_UNKNOWN)
15429 {
15430 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015431 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015432 *rettv = item->li_tv;
15433 vim_free(item);
15434 }
15435 else
15436 {
15437 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 end = get_tv_number_chk(&argvars[2], &error);
15439 if (error)
15440 ; /* type error: do nothing */
15441 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015442 EMSGN(_(e_listidx), end);
15443 else
15444 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015445 int cnt = 0;
15446
15447 for (li = item; li != NULL; li = li->li_next)
15448 {
15449 ++cnt;
15450 if (li == item2)
15451 break;
15452 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015453 if (li == NULL) /* didn't find "item2" after "item" */
15454 EMSG(_(e_invrange));
15455 else
15456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015457 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015458 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015460 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015461 l->lv_first = item;
15462 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015463 item->li_prev = NULL;
15464 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015465 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015466 }
15467 }
15468 }
15469 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015470 }
15471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472}
15473
15474/*
15475 * "rename({from}, {to})" function
15476 */
15477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 typval_T *argvars;
15480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481{
15482 char_u buf[NUMBUFLEN];
15483
15484 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15488 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489}
15490
15491/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015492 * "repeat()" function
15493 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015494 static void
15495f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015496 typval_T *argvars;
15497 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015498{
15499 char_u *p;
15500 int n;
15501 int slen;
15502 int len;
15503 char_u *r;
15504 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015505
15506 n = get_tv_number(&argvars[1]);
15507 if (argvars[0].v_type == VAR_LIST)
15508 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015509 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015510 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015511 if (list_extend(rettv->vval.v_list,
15512 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015513 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015514 }
15515 else
15516 {
15517 p = get_tv_string(&argvars[0]);
15518 rettv->v_type = VAR_STRING;
15519 rettv->vval.v_string = NULL;
15520
15521 slen = (int)STRLEN(p);
15522 len = slen * n;
15523 if (len <= 0)
15524 return;
15525
15526 r = alloc(len + 1);
15527 if (r != NULL)
15528 {
15529 for (i = 0; i < n; i++)
15530 mch_memmove(r + i * slen, p, (size_t)slen);
15531 r[len] = NUL;
15532 }
15533
15534 rettv->vval.v_string = r;
15535 }
15536}
15537
15538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 * "resolve()" function
15540 */
15541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015543 typval_T *argvars;
15544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
15546 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015547#ifdef HAVE_READLINK
15548 char_u *buf = NULL;
15549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552#ifdef FEAT_SHORTCUT
15553 {
15554 char_u *v = NULL;
15555
15556 v = mch_resolve_shortcut(p);
15557 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 }
15562#else
15563# ifdef HAVE_READLINK
15564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 char_u *cpy;
15566 int len;
15567 char_u *remain = NULL;
15568 char_u *q;
15569 int is_relative_to_current = FALSE;
15570 int has_trailing_pathsep = FALSE;
15571 int limit = 100;
15572
15573 p = vim_strsave(p);
15574
15575 if (p[0] == '.' && (vim_ispathsep(p[1])
15576 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15577 is_relative_to_current = TRUE;
15578
15579 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015580 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015583 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585
15586 q = getnextcomp(p);
15587 if (*q != NUL)
15588 {
15589 /* Separate the first path component in "p", and keep the
15590 * remainder (beginning with the path separator). */
15591 remain = vim_strsave(q - 1);
15592 q[-1] = NUL;
15593 }
15594
Bram Moolenaard9462e32011-04-11 21:35:11 +020015595 buf = alloc(MAXPATHL + 1);
15596 if (buf == NULL)
15597 goto fail;
15598
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 for (;;)
15600 {
15601 for (;;)
15602 {
15603 len = readlink((char *)p, (char *)buf, MAXPATHL);
15604 if (len <= 0)
15605 break;
15606 buf[len] = NUL;
15607
15608 if (limit-- == 0)
15609 {
15610 vim_free(p);
15611 vim_free(remain);
15612 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 goto fail;
15615 }
15616
15617 /* Ensure that the result will have a trailing path separator
15618 * if the argument has one. */
15619 if (remain == NULL && has_trailing_pathsep)
15620 add_pathsep(buf);
15621
15622 /* Separate the first path component in the link value and
15623 * concatenate the remainders. */
15624 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15625 if (*q != NUL)
15626 {
15627 if (remain == NULL)
15628 remain = vim_strsave(q - 1);
15629 else
15630 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015631 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 if (cpy != NULL)
15633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 vim_free(remain);
15635 remain = cpy;
15636 }
15637 }
15638 q[-1] = NUL;
15639 }
15640
15641 q = gettail(p);
15642 if (q > p && *q == NUL)
15643 {
15644 /* Ignore trailing path separator. */
15645 q[-1] = NUL;
15646 q = gettail(p);
15647 }
15648 if (q > p && !mch_isFullName(buf))
15649 {
15650 /* symlink is relative to directory of argument */
15651 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15652 if (cpy != NULL)
15653 {
15654 STRCPY(cpy, p);
15655 STRCPY(gettail(cpy), buf);
15656 vim_free(p);
15657 p = cpy;
15658 }
15659 }
15660 else
15661 {
15662 vim_free(p);
15663 p = vim_strsave(buf);
15664 }
15665 }
15666
15667 if (remain == NULL)
15668 break;
15669
15670 /* Append the first path component of "remain" to "p". */
15671 q = getnextcomp(remain + 1);
15672 len = q - remain - (*q != NUL);
15673 cpy = vim_strnsave(p, STRLEN(p) + len);
15674 if (cpy != NULL)
15675 {
15676 STRNCAT(cpy, remain, len);
15677 vim_free(p);
15678 p = cpy;
15679 }
15680 /* Shorten "remain". */
15681 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015682 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 else
15684 {
15685 vim_free(remain);
15686 remain = NULL;
15687 }
15688 }
15689
15690 /* If the result is a relative path name, make it explicitly relative to
15691 * the current directory if and only if the argument had this form. */
15692 if (!vim_ispathsep(*p))
15693 {
15694 if (is_relative_to_current
15695 && *p != NUL
15696 && !(p[0] == '.'
15697 && (p[1] == NUL
15698 || vim_ispathsep(p[1])
15699 || (p[1] == '.'
15700 && (p[2] == NUL
15701 || vim_ispathsep(p[2]))))))
15702 {
15703 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015704 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 if (cpy != NULL)
15706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 vim_free(p);
15708 p = cpy;
15709 }
15710 }
15711 else if (!is_relative_to_current)
15712 {
15713 /* Strip leading "./". */
15714 q = p;
15715 while (q[0] == '.' && vim_ispathsep(q[1]))
15716 q += 2;
15717 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015718 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 }
15720 }
15721
15722 /* Ensure that the result will have no trailing path separator
15723 * if the argument had none. But keep "/" or "//". */
15724 if (!has_trailing_pathsep)
15725 {
15726 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015727 if (after_pathsep(p, q))
15728 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 }
15730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 }
15733# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015734 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735# endif
15736#endif
15737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
15740#ifdef HAVE_READLINK
15741fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015742 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015744 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745}
15746
15747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 */
15750 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 typval_T *argvars;
15753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754{
Bram Moolenaar33570922005-01-25 22:26:29 +000015755 list_T *l;
15756 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758 if (argvars[0].v_type != VAR_LIST)
15759 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015760 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015761 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 {
15763 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015764 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015765 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 while (li != NULL)
15767 {
15768 ni = li->li_prev;
15769 list_append(l, li);
15770 li = ni;
15771 }
15772 rettv->vval.v_list = l;
15773 rettv->v_type = VAR_LIST;
15774 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015775 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777}
15778
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015779#define SP_NOMOVE 0x01 /* don't move cursor */
15780#define SP_REPEAT 0x02 /* repeat to find outer pair */
15781#define SP_RETCOUNT 0x04 /* return matchcount */
15782#define SP_SETPCMARK 0x08 /* set previous context mark */
15783#define SP_START 0x10 /* accept match at start position */
15784#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15785#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015786
Bram Moolenaar33570922005-01-25 22:26:29 +000015787static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788
15789/*
15790 * Get flags for a search function.
15791 * Possibly sets "p_ws".
15792 * Returns BACKWARD, FORWARD or zero (for an error).
15793 */
15794 static int
15795get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 int *flagsp;
15798{
15799 int dir = FORWARD;
15800 char_u *flags;
15801 char_u nbuf[NUMBUFLEN];
15802 int mask;
15803
15804 if (varp->v_type != VAR_UNKNOWN)
15805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 flags = get_tv_string_buf_chk(varp, nbuf);
15807 if (flags == NULL)
15808 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809 while (*flags != NUL)
15810 {
15811 switch (*flags)
15812 {
15813 case 'b': dir = BACKWARD; break;
15814 case 'w': p_ws = TRUE; break;
15815 case 'W': p_ws = FALSE; break;
15816 default: mask = 0;
15817 if (flagsp != NULL)
15818 switch (*flags)
15819 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015820 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015821 case 'e': mask = SP_END; break;
15822 case 'm': mask = SP_RETCOUNT; break;
15823 case 'n': mask = SP_NOMOVE; break;
15824 case 'p': mask = SP_SUBPAT; break;
15825 case 'r': mask = SP_REPEAT; break;
15826 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 }
15828 if (mask == 0)
15829 {
15830 EMSG2(_(e_invarg2), flags);
15831 dir = 0;
15832 }
15833 else
15834 *flagsp |= mask;
15835 }
15836 if (dir == 0)
15837 break;
15838 ++flags;
15839 }
15840 }
15841 return dir;
15842}
15843
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015848search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015850 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015851 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015853 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 char_u *pat;
15855 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015856 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 int save_p_ws = p_ws;
15858 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015859 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015860 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015861 proftime_T tm;
15862#ifdef FEAT_RELTIME
15863 long time_limit = 0;
15864#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015865 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015866 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015869 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015870 if (dir == 0)
15871 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015872 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015873 if (flags & SP_START)
15874 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015875 if (flags & SP_END)
15876 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877
Bram Moolenaar76929292008-01-06 19:07:36 +000015878 /* Optional arguments: line number to stop searching and timeout. */
15879 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015880 {
15881 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15882 if (lnum_stop < 0)
15883 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015884#ifdef FEAT_RELTIME
15885 if (argvars[3].v_type != VAR_UNKNOWN)
15886 {
15887 time_limit = get_tv_number_chk(&argvars[3], NULL);
15888 if (time_limit < 0)
15889 goto theend;
15890 }
15891#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015892 }
15893
Bram Moolenaar76929292008-01-06 19:07:36 +000015894#ifdef FEAT_RELTIME
15895 /* Set the time limit, if there is one. */
15896 profile_setlimit(time_limit, &tm);
15897#endif
15898
Bram Moolenaar231334e2005-07-25 20:46:57 +000015899 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015900 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015901 * Check to make sure only those flags are set.
15902 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15903 * flags cannot be set. Check for that condition also.
15904 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015905 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015906 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015909 goto theend;
15910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015912 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015913 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015914 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015915 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015917 if (flags & SP_SUBPAT)
15918 retval = subpatnum;
15919 else
15920 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015921 if (flags & SP_SETPCMARK)
15922 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015924 if (match_pos != NULL)
15925 {
15926 /* Store the match cursor position */
15927 match_pos->lnum = pos.lnum;
15928 match_pos->col = pos.col + 1;
15929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 /* "/$" will put the cursor after the end of the line, may need to
15931 * correct that here */
15932 check_cursor();
15933 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015934
15935 /* If 'n' flag is used: restore cursor position. */
15936 if (flags & SP_NOMOVE)
15937 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015938 else
15939 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015940theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015942
15943 return retval;
15944}
15945
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015946#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015947
15948/*
15949 * round() is not in C90, use ceil() or floor() instead.
15950 */
15951 float_T
15952vim_round(f)
15953 float_T f;
15954{
15955 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15956}
15957
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015958/*
15959 * "round({float})" function
15960 */
15961 static void
15962f_round(argvars, rettv)
15963 typval_T *argvars;
15964 typval_T *rettv;
15965{
15966 float_T f;
15967
15968 rettv->v_type = VAR_FLOAT;
15969 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015970 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015971 else
15972 rettv->vval.v_float = 0.0;
15973}
15974#endif
15975
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015976/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015977 * "screenattr()" function
15978 */
15979 static void
15980f_screenattr(argvars, rettv)
15981 typval_T *argvars UNUSED;
15982 typval_T *rettv;
15983{
15984 int row;
15985 int col;
15986 int c;
15987
15988 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15989 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15990 if (row < 0 || row >= screen_Rows
15991 || col < 0 || col >= screen_Columns)
15992 c = -1;
15993 else
15994 c = ScreenAttrs[LineOffset[row] + col];
15995 rettv->vval.v_number = c;
15996}
15997
15998/*
15999 * "screenchar()" function
16000 */
16001 static void
16002f_screenchar(argvars, rettv)
16003 typval_T *argvars UNUSED;
16004 typval_T *rettv;
16005{
16006 int row;
16007 int col;
16008 int off;
16009 int c;
16010
16011 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16012 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16013 if (row < 0 || row >= screen_Rows
16014 || col < 0 || col >= screen_Columns)
16015 c = -1;
16016 else
16017 {
16018 off = LineOffset[row] + col;
16019#ifdef FEAT_MBYTE
16020 if (enc_utf8 && ScreenLinesUC[off] != 0)
16021 c = ScreenLinesUC[off];
16022 else
16023#endif
16024 c = ScreenLines[off];
16025 }
16026 rettv->vval.v_number = c;
16027}
16028
16029/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016030 * "screencol()" function
16031 *
16032 * First column is 1 to be consistent with virtcol().
16033 */
16034 static void
16035f_screencol(argvars, rettv)
16036 typval_T *argvars UNUSED;
16037 typval_T *rettv;
16038{
16039 rettv->vval.v_number = screen_screencol() + 1;
16040}
16041
16042/*
16043 * "screenrow()" function
16044 */
16045 static void
16046f_screenrow(argvars, rettv)
16047 typval_T *argvars UNUSED;
16048 typval_T *rettv;
16049{
16050 rettv->vval.v_number = screen_screenrow() + 1;
16051}
16052
16053/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016054 * "search()" function
16055 */
16056 static void
16057f_search(argvars, rettv)
16058 typval_T *argvars;
16059 typval_T *rettv;
16060{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016061 int flags = 0;
16062
16063 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064}
16065
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016067 * "searchdecl()" function
16068 */
16069 static void
16070f_searchdecl(argvars, rettv)
16071 typval_T *argvars;
16072 typval_T *rettv;
16073{
16074 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016075 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016076 int error = FALSE;
16077 char_u *name;
16078
16079 rettv->vval.v_number = 1; /* default: FAIL */
16080
16081 name = get_tv_string_chk(&argvars[0]);
16082 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016083 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016084 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016085 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16086 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16087 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016088 if (!error && name != NULL)
16089 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016090 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016091}
16092
16093/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016094 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016096 static int
16097searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016099 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100{
16101 char_u *spat, *mpat, *epat;
16102 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 int dir;
16105 int flags = 0;
16106 char_u nbuf1[NUMBUFLEN];
16107 char_u nbuf2[NUMBUFLEN];
16108 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016109 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016110 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016111 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016114 spat = get_tv_string_chk(&argvars[0]);
16115 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16116 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16117 if (spat == NULL || mpat == NULL || epat == NULL)
16118 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 /* Handle the optional fourth argument: flags */
16121 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016122 if (dir == 0)
16123 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016124
16125 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016126 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16127 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016128 if ((flags & (SP_END | SP_SUBPAT)) != 0
16129 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016130 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016131 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016132 goto theend;
16133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016135 /* Using 'r' implies 'W', otherwise it doesn't work. */
16136 if (flags & SP_REPEAT)
16137 p_ws = FALSE;
16138
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016140 if (argvars[3].v_type == VAR_UNKNOWN
16141 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 skip = (char_u *)"";
16143 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016146 if (argvars[5].v_type != VAR_UNKNOWN)
16147 {
16148 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16149 if (lnum_stop < 0)
16150 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016151#ifdef FEAT_RELTIME
16152 if (argvars[6].v_type != VAR_UNKNOWN)
16153 {
16154 time_limit = get_tv_number_chk(&argvars[6], NULL);
16155 if (time_limit < 0)
16156 goto theend;
16157 }
16158#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 }
16160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016161 if (skip == NULL)
16162 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016164 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016165 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016166
16167theend:
16168 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016169
16170 return retval;
16171}
16172
16173/*
16174 * "searchpair()" function
16175 */
16176 static void
16177f_searchpair(argvars, rettv)
16178 typval_T *argvars;
16179 typval_T *rettv;
16180{
16181 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16182}
16183
16184/*
16185 * "searchpairpos()" function
16186 */
16187 static void
16188f_searchpairpos(argvars, rettv)
16189 typval_T *argvars;
16190 typval_T *rettv;
16191{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016192 pos_T match_pos;
16193 int lnum = 0;
16194 int col = 0;
16195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016197 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198
16199 if (searchpair_cmn(argvars, &match_pos) > 0)
16200 {
16201 lnum = match_pos.lnum;
16202 col = match_pos.col;
16203 }
16204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016205 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16206 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016207}
16208
16209/*
16210 * Search for a start/middle/end thing.
16211 * Used by searchpair(), see its documentation for the details.
16212 * Returns 0 or -1 for no match,
16213 */
16214 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016215do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16216 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016217 char_u *spat; /* start pattern */
16218 char_u *mpat; /* middle pattern */
16219 char_u *epat; /* end pattern */
16220 int dir; /* BACKWARD or FORWARD */
16221 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016222 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016223 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016224 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016225 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016226{
16227 char_u *save_cpo;
16228 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16229 long retval = 0;
16230 pos_T pos;
16231 pos_T firstpos;
16232 pos_T foundpos;
16233 pos_T save_cursor;
16234 pos_T save_pos;
16235 int n;
16236 int r;
16237 int nest = 1;
16238 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016239 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016240 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016241
16242 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16243 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016244 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016245
Bram Moolenaar76929292008-01-06 19:07:36 +000016246#ifdef FEAT_RELTIME
16247 /* Set the time limit, if there is one. */
16248 profile_setlimit(time_limit, &tm);
16249#endif
16250
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016251 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16252 * start/middle/end (pat3, for the top pair). */
16253 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16254 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16255 if (pat2 == NULL || pat3 == NULL)
16256 goto theend;
16257 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16258 if (*mpat == NUL)
16259 STRCPY(pat3, pat2);
16260 else
16261 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16262 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016263 if (flags & SP_START)
16264 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016265
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 save_cursor = curwin->w_cursor;
16267 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016268 clearpos(&firstpos);
16269 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 pat = pat3;
16271 for (;;)
16272 {
16273 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016274 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16276 /* didn't find it or found the first match again: FAIL */
16277 break;
16278
16279 if (firstpos.lnum == 0)
16280 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016281 if (equalpos(pos, foundpos))
16282 {
16283 /* Found the same position again. Can happen with a pattern that
16284 * has "\zs" at the end and searching backwards. Advance one
16285 * character and try again. */
16286 if (dir == BACKWARD)
16287 decl(&pos);
16288 else
16289 incl(&pos);
16290 }
16291 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016293 /* clear the start flag to avoid getting stuck here */
16294 options &= ~SEARCH_START;
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 /* If the skip pattern matches, ignore this match. */
16297 if (*skip != NUL)
16298 {
16299 save_pos = curwin->w_cursor;
16300 curwin->w_cursor = pos;
16301 r = eval_to_bool(skip, &err, NULL, FALSE);
16302 curwin->w_cursor = save_pos;
16303 if (err)
16304 {
16305 /* Evaluating {skip} caused an error, break here. */
16306 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016307 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 break;
16309 }
16310 if (r)
16311 continue;
16312 }
16313
16314 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16315 {
16316 /* Found end when searching backwards or start when searching
16317 * forward: nested pair. */
16318 ++nest;
16319 pat = pat2; /* nested, don't search for middle */
16320 }
16321 else
16322 {
16323 /* Found end when searching forward or start when searching
16324 * backward: end of (nested) pair; or found middle in outer pair. */
16325 if (--nest == 1)
16326 pat = pat3; /* outer level, search for middle */
16327 }
16328
16329 if (nest == 0)
16330 {
16331 /* Found the match: return matchcount or line number. */
16332 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016333 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016335 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016336 if (flags & SP_SETPCMARK)
16337 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 curwin->w_cursor = pos;
16339 if (!(flags & SP_REPEAT))
16340 break;
16341 nest = 1; /* search for next unmatched */
16342 }
16343 }
16344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345 if (match_pos != NULL)
16346 {
16347 /* Store the match cursor position */
16348 match_pos->lnum = curwin->w_cursor.lnum;
16349 match_pos->col = curwin->w_cursor.col + 1;
16350 }
16351
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016353 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 curwin->w_cursor = save_cursor;
16355
16356theend:
16357 vim_free(pat2);
16358 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016359 if (p_cpo == empty_option)
16360 p_cpo = save_cpo;
16361 else
16362 /* Darn, evaluating the {skip} expression changed the value. */
16363 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016364
16365 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366}
16367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016368/*
16369 * "searchpos()" function
16370 */
16371 static void
16372f_searchpos(argvars, rettv)
16373 typval_T *argvars;
16374 typval_T *rettv;
16375{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016376 pos_T match_pos;
16377 int lnum = 0;
16378 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016379 int n;
16380 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016381
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016382 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016384
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016385 n = search_cmn(argvars, &match_pos, &flags);
16386 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387 {
16388 lnum = match_pos.lnum;
16389 col = match_pos.col;
16390 }
16391
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016392 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16393 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016394 if (flags & SP_SUBPAT)
16395 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016396}
16397
16398
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 static void
16400f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404#ifdef FEAT_CLIENTSERVER
16405 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 char_u *server = get_tv_string_chk(&argvars[0]);
16407 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016410 if (server == NULL || reply == NULL)
16411 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 if (check_restricted() || check_secure())
16413 return;
16414# ifdef FEAT_X11
16415 if (check_connection() == FAIL)
16416 return;
16417# endif
16418
16419 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 EMSG(_("E258: Unable to send to client"));
16422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424 rettv->vval.v_number = 0;
16425#else
16426 rettv->vval.v_number = -1;
16427#endif
16428}
16429
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 static void
16431f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016432 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434{
16435 char_u *r = NULL;
16436
16437#ifdef FEAT_CLIENTSERVER
16438# ifdef WIN32
16439 r = serverGetVimNames();
16440# else
16441 make_connection();
16442 if (X_DISPLAY != NULL)
16443 r = serverGetVimNames(X_DISPLAY);
16444# endif
16445#endif
16446 rettv->v_type = VAR_STRING;
16447 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448}
16449
16450/*
16451 * "setbufvar()" function
16452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016456 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457{
16458 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 char_u nbuf[NUMBUFLEN];
16463
16464 if (check_restricted() || check_secure())
16465 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016466 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16467 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016468 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 varp = &argvars[2];
16470
16471 if (buf != NULL && varname != NULL && varp != NULL)
16472 {
16473 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
16476 if (*varname == '&')
16477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016478 long numval;
16479 char_u *strval;
16480 int error = FALSE;
16481
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 numval = get_tv_number_chk(varp, &error);
16484 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (!error && strval != NULL)
16486 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487 }
16488 else
16489 {
16490 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16491 if (bufvarname != NULL)
16492 {
16493 STRCPY(bufvarname, "b:");
16494 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016495 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 vim_free(bufvarname);
16497 }
16498 }
16499
16500 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503}
16504
16505/*
16506 * "setcmdpos()" function
16507 */
16508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016509f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016510 typval_T *argvars;
16511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 int pos = (int)get_tv_number(&argvars[0]) - 1;
16514
16515 if (pos >= 0)
16516 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517}
16518
16519/*
16520 * "setline()" function
16521 */
16522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016523f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *argvars;
16525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526{
16527 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016528 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016529 list_T *l = NULL;
16530 listitem_T *li = NULL;
16531 long added = 0;
16532 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016534 lnum = get_tv_lnum(&argvars[0]);
16535 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016537 l = argvars[1].vval.v_list;
16538 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016542
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016543 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016544 for (;;)
16545 {
16546 if (l != NULL)
16547 {
16548 /* list argument, get next string */
16549 if (li == NULL)
16550 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016552 li = li->li_next;
16553 }
16554
16555 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016558
16559 /* When coming here from Insert mode, sync undo, so that this can be
16560 * undone separately from what was previously inserted. */
16561 if (u_sync_once == 2)
16562 {
16563 u_sync_once = 1; /* notify that u_sync() was called */
16564 u_sync(TRUE);
16565 }
16566
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016567 if (lnum <= curbuf->b_ml.ml_line_count)
16568 {
16569 /* existing line, replace it */
16570 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16571 {
16572 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016573 if (lnum == curwin->w_cursor.lnum)
16574 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016575 rettv->vval.v_number = 0; /* OK */
16576 }
16577 }
16578 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16579 {
16580 /* lnum is one past the last line, append the line */
16581 ++added;
16582 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16583 rettv->vval.v_number = 0; /* OK */
16584 }
16585
16586 if (l == NULL) /* only one string argument */
16587 break;
16588 ++lnum;
16589 }
16590
16591 if (added > 0)
16592 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593}
16594
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016595static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16596
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016599 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016600 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016601set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016602 win_T *wp UNUSED;
16603 typval_T *list_arg UNUSED;
16604 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016605 typval_T *rettv;
16606{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016607#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016608 char_u *act;
16609 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016610#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016611
Bram Moolenaar2641f772005-03-25 21:58:17 +000016612 rettv->vval.v_number = -1;
16613
16614#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016615 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016616 EMSG(_(e_listreq));
16617 else
16618 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016619 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016620
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016621 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016622 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016623 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016624 if (act == NULL)
16625 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016626 if (*act == 'a' || *act == 'r')
16627 action = *act;
16628 }
16629
Bram Moolenaar81484f42012-12-05 15:16:47 +010016630 if (l != NULL && set_errorlist(wp, l, action,
16631 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016632 rettv->vval.v_number = 0;
16633 }
16634#endif
16635}
16636
16637/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016638 * "setloclist()" function
16639 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016640 static void
16641f_setloclist(argvars, rettv)
16642 typval_T *argvars;
16643 typval_T *rettv;
16644{
16645 win_T *win;
16646
16647 rettv->vval.v_number = -1;
16648
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016649 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016650 if (win != NULL)
16651 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16652}
16653
16654/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016655 * "setmatches()" function
16656 */
16657 static void
16658f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016659 typval_T *argvars UNUSED;
16660 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016661{
16662#ifdef FEAT_SEARCH_EXTRA
16663 list_T *l;
16664 listitem_T *li;
16665 dict_T *d;
16666
16667 rettv->vval.v_number = -1;
16668 if (argvars[0].v_type != VAR_LIST)
16669 {
16670 EMSG(_(e_listreq));
16671 return;
16672 }
16673 if ((l = argvars[0].vval.v_list) != NULL)
16674 {
16675
16676 /* To some extent make sure that we are dealing with a list from
16677 * "getmatches()". */
16678 li = l->lv_first;
16679 while (li != NULL)
16680 {
16681 if (li->li_tv.v_type != VAR_DICT
16682 || (d = li->li_tv.vval.v_dict) == NULL)
16683 {
16684 EMSG(_(e_invarg));
16685 return;
16686 }
16687 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16688 && dict_find(d, (char_u *)"pattern", -1) != NULL
16689 && dict_find(d, (char_u *)"priority", -1) != NULL
16690 && dict_find(d, (char_u *)"id", -1) != NULL))
16691 {
16692 EMSG(_(e_invarg));
16693 return;
16694 }
16695 li = li->li_next;
16696 }
16697
16698 clear_matches(curwin);
16699 li = l->lv_first;
16700 while (li != NULL)
16701 {
16702 d = li->li_tv.vval.v_dict;
16703 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16704 get_dict_string(d, (char_u *)"pattern", FALSE),
16705 (int)get_dict_number(d, (char_u *)"priority"),
16706 (int)get_dict_number(d, (char_u *)"id"));
16707 li = li->li_next;
16708 }
16709 rettv->vval.v_number = 0;
16710 }
16711#endif
16712}
16713
16714/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016715 * "setpos()" function
16716 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016717 static void
16718f_setpos(argvars, rettv)
16719 typval_T *argvars;
16720 typval_T *rettv;
16721{
16722 pos_T pos;
16723 int fnum;
16724 char_u *name;
16725
Bram Moolenaar08250432008-02-13 11:42:46 +000016726 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016727 name = get_tv_string_chk(argvars);
16728 if (name != NULL)
16729 {
16730 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16731 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016732 if (--pos.col < 0)
16733 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016734 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016735 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016736 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016737 if (fnum == curbuf->b_fnum)
16738 {
16739 curwin->w_cursor = pos;
16740 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016741 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016742 }
16743 else
16744 EMSG(_(e_invarg));
16745 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016746 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16747 {
16748 /* set mark */
16749 if (setmark_pos(name[1], &pos, fnum) == OK)
16750 rettv->vval.v_number = 0;
16751 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016752 else
16753 EMSG(_(e_invarg));
16754 }
16755 }
16756}
16757
16758/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016759 * "setqflist()" function
16760 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016761 static void
16762f_setqflist(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
16766 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16767}
16768
16769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 * "setreg()" function
16771 */
16772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 typval_T *argvars;
16775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776{
16777 int regname;
16778 char_u *strregname;
16779 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 int append;
16782 char_u yank_type;
16783 long block_len;
16784
16785 block_len = -1;
16786 yank_type = MAUTO;
16787 append = FALSE;
16788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016790 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016792 if (strregname == NULL)
16793 return; /* type error; errmsg already given */
16794 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 if (regname == 0 || regname == '@')
16796 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016800 stropt = get_tv_string_chk(&argvars[2]);
16801 if (stropt == NULL)
16802 return; /* type error */
16803 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 switch (*stropt)
16805 {
16806 case 'a': case 'A': /* append */
16807 append = TRUE;
16808 break;
16809 case 'v': case 'c': /* character-wise selection */
16810 yank_type = MCHAR;
16811 break;
16812 case 'V': case 'l': /* line-wise selection */
16813 yank_type = MLINE;
16814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 case 'b': case Ctrl_V: /* block-wise selection */
16816 yank_type = MBLOCK;
16817 if (VIM_ISDIGIT(stropt[1]))
16818 {
16819 ++stropt;
16820 block_len = getdigits(&stropt) - 1;
16821 --stropt;
16822 }
16823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 }
16825 }
16826
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016827 if (argvars[1].v_type == VAR_LIST)
16828 {
16829 char_u **lstval;
16830 char_u **curval;
16831 int len = argvars[1].vval.v_list->lv_len;
16832 listitem_T *li;
16833
16834 lstval = (char_u **)alloc(sizeof(char_u *) * (len + 1));
16835 if (lstval == NULL)
16836 return;
16837 curval = lstval;
16838
16839 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16840 li = li->li_next)
16841 {
16842 /* TODO: this may use a static buffer several times. */
16843 strval = get_tv_string_chk(&li->li_tv);
16844 if (strval == NULL)
16845 {
16846 vim_free(lstval);
16847 return;
16848 }
16849 *curval++ = strval;
16850 }
16851 *curval++ = NULL;
16852
16853 write_reg_contents_lst(regname, lstval, -1,
16854 append, yank_type, block_len);
16855 vim_free(lstval);
16856 }
16857 else
16858 {
16859 strval = get_tv_string_chk(&argvars[1]);
16860 if (strval == NULL)
16861 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016862 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016865 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866}
16867
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016868/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016869 * "settabvar()" function
16870 */
16871 static void
16872f_settabvar(argvars, rettv)
16873 typval_T *argvars;
16874 typval_T *rettv;
16875{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016876#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016877 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016878 tabpage_T *tp;
16879#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016880 char_u *varname, *tabvarname;
16881 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016882
16883 rettv->vval.v_number = 0;
16884
16885 if (check_restricted() || check_secure())
16886 return;
16887
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016888#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016889 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016890#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016891 varname = get_tv_string_chk(&argvars[1]);
16892 varp = &argvars[2];
16893
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016894 if (varname != NULL && varp != NULL
16895#ifdef FEAT_WINDOWS
16896 && tp != NULL
16897#endif
16898 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016899 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016900#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016901 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016902 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016903#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016904
16905 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16906 if (tabvarname != NULL)
16907 {
16908 STRCPY(tabvarname, "t:");
16909 STRCPY(tabvarname + 2, varname);
16910 set_var(tabvarname, varp, TRUE);
16911 vim_free(tabvarname);
16912 }
16913
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016914#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016915 /* Restore current tabpage */
16916 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016917 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016918#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016919 }
16920}
16921
16922/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016923 * "settabwinvar()" function
16924 */
16925 static void
16926f_settabwinvar(argvars, rettv)
16927 typval_T *argvars;
16928 typval_T *rettv;
16929{
16930 setwinvar(argvars, rettv, 1);
16931}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
16933/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016934 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016937f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 typval_T *argvars;
16939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016941 setwinvar(argvars, rettv, 0);
16942}
16943
16944/*
16945 * "setwinvar()" and "settabwinvar()" functions
16946 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016947
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016948 static void
16949setwinvar(argvars, rettv, off)
16950 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016951 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016952 int off;
16953{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 win_T *win;
16955#ifdef FEAT_WINDOWS
16956 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016957 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958#endif
16959 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016962 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963
16964 if (check_restricted() || check_secure())
16965 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016966
16967#ifdef FEAT_WINDOWS
16968 if (off == 1)
16969 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16970 else
16971 tp = curtab;
16972#endif
16973 win = find_win_by_nr(&argvars[off], tp);
16974 varname = get_tv_string_chk(&argvars[off + 1]);
16975 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977 if (win != NULL && varname != NULL && varp != NULL)
16978 {
16979#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016980 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016981 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982#endif
16983
16984 if (*varname == '&')
16985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 long numval;
16987 char_u *strval;
16988 int error = FALSE;
16989
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016991 numval = get_tv_number_chk(varp, &error);
16992 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 if (!error && strval != NULL)
16994 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 }
16996 else
16997 {
16998 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16999 if (winvarname != NULL)
17000 {
17001 STRCPY(winvarname, "w:");
17002 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017003 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 vim_free(winvarname);
17005 }
17006 }
17007
17008#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017009 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010#endif
17011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012}
17013
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017014#ifdef FEAT_CRYPT
17015/*
17016 * "sha256({string})" function
17017 */
17018 static void
17019f_sha256(argvars, rettv)
17020 typval_T *argvars;
17021 typval_T *rettv;
17022{
17023 char_u *p;
17024
17025 p = get_tv_string(&argvars[0]);
17026 rettv->vval.v_string = vim_strsave(
17027 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17028 rettv->v_type = VAR_STRING;
17029}
17030#endif /* FEAT_CRYPT */
17031
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017033 * "shellescape({string})" function
17034 */
17035 static void
17036f_shellescape(argvars, rettv)
17037 typval_T *argvars;
17038 typval_T *rettv;
17039{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017040 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017041 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017042 rettv->v_type = VAR_STRING;
17043}
17044
17045/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017046 * shiftwidth() function
17047 */
17048 static void
17049f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017050 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017051 typval_T *rettv;
17052{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017053 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017054}
17055
17056/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 */
17059 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 typval_T *argvars;
17062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 p = get_tv_string(&argvars[0]);
17067 rettv->vval.v_string = vim_strsave(p);
17068 simplify_filename(rettv->vval.v_string); /* simplify in place */
17069 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070}
17071
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017072#ifdef FEAT_FLOAT
17073/*
17074 * "sin()" function
17075 */
17076 static void
17077f_sin(argvars, rettv)
17078 typval_T *argvars;
17079 typval_T *rettv;
17080{
17081 float_T f;
17082
17083 rettv->v_type = VAR_FLOAT;
17084 if (get_float_arg(argvars, &f) == OK)
17085 rettv->vval.v_float = sin(f);
17086 else
17087 rettv->vval.v_float = 0.0;
17088}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017089
17090/*
17091 * "sinh()" function
17092 */
17093 static void
17094f_sinh(argvars, rettv)
17095 typval_T *argvars;
17096 typval_T *rettv;
17097{
17098 float_T f;
17099
17100 rettv->v_type = VAR_FLOAT;
17101 if (get_float_arg(argvars, &f) == OK)
17102 rettv->vval.v_float = sinh(f);
17103 else
17104 rettv->vval.v_float = 0.0;
17105}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017106#endif
17107
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108static int
17109#ifdef __BORLANDC__
17110 _RTLENTRYF
17111#endif
17112 item_compare __ARGS((const void *s1, const void *s2));
17113static int
17114#ifdef __BORLANDC__
17115 _RTLENTRYF
17116#endif
17117 item_compare2 __ARGS((const void *s1, const void *s2));
17118
17119static int item_compare_ic;
17120static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017121static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017123static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124#define ITEM_COMPARE_FAIL 999
17125
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017127 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 static int
17130#ifdef __BORLANDC__
17131_RTLENTRYF
17132#endif
17133item_compare(s1, s2)
17134 const void *s1;
17135 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 char_u *p1, *p2;
17138 char_u *tofree1, *tofree2;
17139 int res;
17140 char_u numbuf1[NUMBUFLEN];
17141 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017143 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17144 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017145 if (p1 == NULL)
17146 p1 = (char_u *)"";
17147 if (p2 == NULL)
17148 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 if (item_compare_ic)
17150 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 res = STRCMP(p1, p2);
17153 vim_free(tofree1);
17154 vim_free(tofree2);
17155 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156}
17157
17158 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159#ifdef __BORLANDC__
17160_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162item_compare2(s1, s2)
17163 const void *s1;
17164 const void *s2;
17165{
17166 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017168 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017171 /* shortcut after failure in previous call; compare all items equal */
17172 if (item_compare_func_err)
17173 return 0;
17174
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017175 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17176 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17178 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179
17180 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017181 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017182 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17183 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 clear_tv(&argv[0]);
17185 clear_tv(&argv[1]);
17186
17187 if (res == FAIL)
17188 res = ITEM_COMPARE_FAIL;
17189 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017190 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17191 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017192 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 clear_tv(&rettv);
17194 return res;
17195}
17196
17197/*
17198 * "sort({list})" function
17199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017201do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 typval_T *argvars;
17203 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017204 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205{
Bram Moolenaar33570922005-01-25 22:26:29 +000017206 list_T *l;
17207 listitem_T *li;
17208 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017209 long len;
17210 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017213 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 else
17215 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017216 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017217 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017218 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219 return;
17220 rettv->vval.v_list = l;
17221 rettv->v_type = VAR_LIST;
17222 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224 len = list_len(l);
17225 if (len <= 1)
17226 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 item_compare_ic = FALSE;
17229 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017230 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231 if (argvars[1].v_type != VAR_UNKNOWN)
17232 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017233 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017236 else
17237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017238 int error = FALSE;
17239
17240 i = get_tv_number_chk(&argvars[1], &error);
17241 if (error)
17242 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243 if (i == 1)
17244 item_compare_ic = TRUE;
17245 else
17246 item_compare_func = get_tv_string(&argvars[1]);
17247 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017248
17249 if (argvars[2].v_type != VAR_UNKNOWN)
17250 {
17251 /* optional third argument: {dict} */
17252 if (argvars[2].v_type != VAR_DICT)
17253 {
17254 EMSG(_(e_dictreq));
17255 return;
17256 }
17257 item_compare_selfdict = argvars[2].vval.v_dict;
17258 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017262 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017263 if (ptrs == NULL)
17264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265
Bram Moolenaar327aa022014-03-25 18:24:23 +010017266 i = 0;
17267 if (sort)
17268 {
17269 /* sort(): ptrs will be the list to sort */
17270 for (li = l->lv_first; li != NULL; li = li->li_next)
17271 ptrs[i++] = li;
17272
17273 item_compare_func_err = FALSE;
17274 /* test the compare function */
17275 if (item_compare_func != NULL
17276 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017277 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017278 EMSG(_("E702: Sort compare function failed"));
17279 else
17280 {
17281 /* Sort the array with item pointers. */
17282 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17283 item_compare_func == NULL ? item_compare : item_compare2);
17284
17285 if (!item_compare_func_err)
17286 {
17287 /* Clear the List and append the items in sorted order. */
17288 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17289 l->lv_len = 0;
17290 for (i = 0; i < len; ++i)
17291 list_append(l, ptrs[i]);
17292 }
17293 }
17294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017297 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17298
17299 /* f_uniq(): ptrs will be a stack of items to remove */
17300 item_compare_func_err = FALSE;
17301 item_compare_func_ptr = item_compare_func
17302 ? item_compare2 : item_compare;
17303
17304 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17305 li = li->li_next)
17306 {
17307 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17308 == 0)
17309 ptrs[i++] = li;
17310 if (item_compare_func_err)
17311 {
17312 EMSG(_("E882: Uniq compare function failed"));
17313 break;
17314 }
17315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017317 if (!item_compare_func_err)
17318 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017319 while (--i >= 0)
17320 {
17321 li = ptrs[i]->li_next;
17322 ptrs[i]->li_next = li->li_next;
17323 if (li->li_next != NULL)
17324 li->li_next->li_prev = ptrs[i];
17325 else
17326 l->lv_last = ptrs[i];
17327 list_fix_watch(l, li);
17328 listitem_free(li);
17329 l->lv_len--;
17330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017332 }
17333
17334 vim_free(ptrs);
17335 }
17336}
17337
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017338/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017339 * "sort({list})" function
17340 */
17341 static void
17342f_sort(argvars, rettv)
17343 typval_T *argvars;
17344 typval_T *rettv;
17345{
17346 do_sort_uniq(argvars, rettv, TRUE);
17347}
17348
17349/*
17350 * "uniq({list})" function
17351 */
17352 static void
17353f_uniq(argvars, rettv)
17354 typval_T *argvars;
17355 typval_T *rettv;
17356{
17357 do_sort_uniq(argvars, rettv, FALSE);
17358}
17359
17360/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017361 * "soundfold({word})" function
17362 */
17363 static void
17364f_soundfold(argvars, rettv)
17365 typval_T *argvars;
17366 typval_T *rettv;
17367{
17368 char_u *s;
17369
17370 rettv->v_type = VAR_STRING;
17371 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017372#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017373 rettv->vval.v_string = eval_soundfold(s);
17374#else
17375 rettv->vval.v_string = vim_strsave(s);
17376#endif
17377}
17378
17379/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017380 * "spellbadword()" function
17381 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017382 static void
17383f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017384 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017385 typval_T *rettv;
17386{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017387 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017388 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017389 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017390
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017391 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017392 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017393
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017394#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017395 if (argvars[0].v_type == VAR_UNKNOWN)
17396 {
17397 /* Find the start and length of the badly spelled word. */
17398 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17399 if (len != 0)
17400 word = ml_get_cursor();
17401 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017402 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017403 {
17404 char_u *str = get_tv_string_chk(&argvars[0]);
17405 int capcol = -1;
17406
17407 if (str != NULL)
17408 {
17409 /* Check the argument for spelling. */
17410 while (*str != NUL)
17411 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017412 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017413 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017414 {
17415 word = str;
17416 break;
17417 }
17418 str += len;
17419 }
17420 }
17421 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017422#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017423
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017424 list_append_string(rettv->vval.v_list, word, len);
17425 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017426 attr == HLF_SPB ? "bad" :
17427 attr == HLF_SPR ? "rare" :
17428 attr == HLF_SPL ? "local" :
17429 attr == HLF_SPC ? "caps" :
17430 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017431}
17432
17433/*
17434 * "spellsuggest()" function
17435 */
17436 static void
17437f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017438 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017439 typval_T *rettv;
17440{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017441#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017442 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017443 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017444 int maxcount;
17445 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017446 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017447 listitem_T *li;
17448 int need_capital = FALSE;
17449#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017450
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017451 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017452 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017453
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017454#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017455 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017456 {
17457 str = get_tv_string(&argvars[0]);
17458 if (argvars[1].v_type != VAR_UNKNOWN)
17459 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017460 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017461 if (maxcount <= 0)
17462 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017463 if (argvars[2].v_type != VAR_UNKNOWN)
17464 {
17465 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17466 if (typeerr)
17467 return;
17468 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017469 }
17470 else
17471 maxcount = 25;
17472
Bram Moolenaar4770d092006-01-12 23:22:24 +000017473 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474
17475 for (i = 0; i < ga.ga_len; ++i)
17476 {
17477 str = ((char_u **)ga.ga_data)[i];
17478
17479 li = listitem_alloc();
17480 if (li == NULL)
17481 vim_free(str);
17482 else
17483 {
17484 li->li_tv.v_type = VAR_STRING;
17485 li->li_tv.v_lock = 0;
17486 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017487 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017488 }
17489 }
17490 ga_clear(&ga);
17491 }
17492#endif
17493}
17494
Bram Moolenaar0d660222005-01-07 21:51:51 +000017495 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017496f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 typval_T *argvars;
17498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017499{
17500 char_u *str;
17501 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017502 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017503 regmatch_T regmatch;
17504 char_u patbuf[NUMBUFLEN];
17505 char_u *save_cpo;
17506 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017507 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017508 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017509 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510
17511 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17512 save_cpo = p_cpo;
17513 p_cpo = (char_u *)"";
17514
17515 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017516 if (argvars[1].v_type != VAR_UNKNOWN)
17517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017518 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17519 if (pat == NULL)
17520 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017521 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017522 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017523 }
17524 if (pat == NULL || *pat == NUL)
17525 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 if (typeerr)
17530 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531
Bram Moolenaar0d660222005-01-07 21:51:51 +000017532 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17533 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017536 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017537 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017538 if (*str == NUL)
17539 match = FALSE; /* empty item at the end */
17540 else
17541 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542 if (match)
17543 end = regmatch.startp[0];
17544 else
17545 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017546 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17547 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017549 if (list_append_string(rettv->vval.v_list, str,
17550 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017552 }
17553 if (!match)
17554 break;
17555 /* Advance to just after the match. */
17556 if (regmatch.endp[0] > str)
17557 col = 0;
17558 else
17559 {
17560 /* Don't get stuck at the same match. */
17561#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017562 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563#else
17564 col = 1;
17565#endif
17566 }
17567 str = regmatch.endp[0];
17568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569
Bram Moolenaar473de612013-06-08 18:19:48 +020017570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574}
17575
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017576#ifdef FEAT_FLOAT
17577/*
17578 * "sqrt()" function
17579 */
17580 static void
17581f_sqrt(argvars, rettv)
17582 typval_T *argvars;
17583 typval_T *rettv;
17584{
17585 float_T f;
17586
17587 rettv->v_type = VAR_FLOAT;
17588 if (get_float_arg(argvars, &f) == OK)
17589 rettv->vval.v_float = sqrt(f);
17590 else
17591 rettv->vval.v_float = 0.0;
17592}
17593
17594/*
17595 * "str2float()" function
17596 */
17597 static void
17598f_str2float(argvars, rettv)
17599 typval_T *argvars;
17600 typval_T *rettv;
17601{
17602 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17603
17604 if (*p == '+')
17605 p = skipwhite(p + 1);
17606 (void)string2float(p, &rettv->vval.v_float);
17607 rettv->v_type = VAR_FLOAT;
17608}
17609#endif
17610
Bram Moolenaar2c932302006-03-18 21:42:09 +000017611/*
17612 * "str2nr()" function
17613 */
17614 static void
17615f_str2nr(argvars, rettv)
17616 typval_T *argvars;
17617 typval_T *rettv;
17618{
17619 int base = 10;
17620 char_u *p;
17621 long n;
17622
17623 if (argvars[1].v_type != VAR_UNKNOWN)
17624 {
17625 base = get_tv_number(&argvars[1]);
17626 if (base != 8 && base != 10 && base != 16)
17627 {
17628 EMSG(_(e_invarg));
17629 return;
17630 }
17631 }
17632
17633 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017634 if (*p == '+')
17635 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017636 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17637 rettv->vval.v_number = n;
17638}
17639
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640#ifdef HAVE_STRFTIME
17641/*
17642 * "strftime({format}[, {time}])" function
17643 */
17644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017645f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017646 typval_T *argvars;
17647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648{
17649 char_u result_buf[256];
17650 struct tm *curtime;
17651 time_t seconds;
17652 char_u *p;
17653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017656 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 seconds = time(NULL);
17659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 curtime = localtime(&seconds);
17662 /* MSVC returns NULL for an invalid value of seconds. */
17663 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 else
17666 {
17667# ifdef FEAT_MBYTE
17668 vimconv_T conv;
17669 char_u *enc;
17670
17671 conv.vc_type = CONV_NONE;
17672 enc = enc_locale();
17673 convert_setup(&conv, p_enc, enc);
17674 if (conv.vc_type != CONV_NONE)
17675 p = string_convert(&conv, p, NULL);
17676# endif
17677 if (p != NULL)
17678 (void)strftime((char *)result_buf, sizeof(result_buf),
17679 (char *)p, curtime);
17680 else
17681 result_buf[0] = NUL;
17682
17683# ifdef FEAT_MBYTE
17684 if (conv.vc_type != CONV_NONE)
17685 vim_free(p);
17686 convert_setup(&conv, enc, p_enc);
17687 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 else
17690# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017691 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692
17693# ifdef FEAT_MBYTE
17694 /* Release conversion descriptors */
17695 convert_setup(&conv, NULL, NULL);
17696 vim_free(enc);
17697# endif
17698 }
17699}
17700#endif
17701
17702/*
17703 * "stridx()" function
17704 */
17705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 typval_T *argvars;
17708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709{
17710 char_u buf[NUMBUFLEN];
17711 char_u *needle;
17712 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 needle = get_tv_string_chk(&argvars[1]);
17718 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 if (needle == NULL || haystack == NULL)
17721 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 if (argvars[2].v_type != VAR_UNKNOWN)
17724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017725 int error = FALSE;
17726
17727 start_idx = get_tv_number_chk(&argvars[2], &error);
17728 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017730 if (start_idx >= 0)
17731 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 }
17733
17734 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17735 if (pos != NULL)
17736 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737}
17738
17739/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017740 * "string()" function
17741 */
17742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 typval_T *argvars;
17745 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746{
17747 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017748 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017751 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017753 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755}
17756
17757/*
17758 * "strlen()" function
17759 */
17760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 typval_T *argvars;
17763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765 rettv->vval.v_number = (varnumber_T)(STRLEN(
17766 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767}
17768
17769/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017770 * "strchars()" function
17771 */
17772 static void
17773f_strchars(argvars, rettv)
17774 typval_T *argvars;
17775 typval_T *rettv;
17776{
17777 char_u *s = get_tv_string(&argvars[0]);
17778#ifdef FEAT_MBYTE
17779 varnumber_T len = 0;
17780
17781 while (*s != NUL)
17782 {
17783 mb_cptr2char_adv(&s);
17784 ++len;
17785 }
17786 rettv->vval.v_number = len;
17787#else
17788 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17789#endif
17790}
17791
17792/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017793 * "strdisplaywidth()" function
17794 */
17795 static void
17796f_strdisplaywidth(argvars, rettv)
17797 typval_T *argvars;
17798 typval_T *rettv;
17799{
17800 char_u *s = get_tv_string(&argvars[0]);
17801 int col = 0;
17802
17803 if (argvars[1].v_type != VAR_UNKNOWN)
17804 col = get_tv_number(&argvars[1]);
17805
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017806 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017807}
17808
17809/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017810 * "strwidth()" function
17811 */
17812 static void
17813f_strwidth(argvars, rettv)
17814 typval_T *argvars;
17815 typval_T *rettv;
17816{
17817 char_u *s = get_tv_string(&argvars[0]);
17818
17819 rettv->vval.v_number = (varnumber_T)(
17820#ifdef FEAT_MBYTE
17821 mb_string2cells(s, -1)
17822#else
17823 STRLEN(s)
17824#endif
17825 );
17826}
17827
17828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 * "strpart()" function
17830 */
17831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017832f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017833 typval_T *argvars;
17834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835{
17836 char_u *p;
17837 int n;
17838 int len;
17839 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017840 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017842 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 slen = (int)STRLEN(p);
17844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017845 n = get_tv_number_chk(&argvars[1], &error);
17846 if (error)
17847 len = 0;
17848 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017849 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 else
17851 len = slen - n; /* default len: all bytes that are available. */
17852
17853 /*
17854 * Only return the overlap between the specified part and the actual
17855 * string.
17856 */
17857 if (n < 0)
17858 {
17859 len += n;
17860 n = 0;
17861 }
17862 else if (n > slen)
17863 n = slen;
17864 if (len < 0)
17865 len = 0;
17866 else if (n + len > slen)
17867 len = slen - n;
17868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 rettv->v_type = VAR_STRING;
17870 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871}
17872
17873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874 * "strridx()" function
17875 */
17876 static void
17877f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 typval_T *argvars;
17879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017880{
17881 char_u buf[NUMBUFLEN];
17882 char_u *needle;
17883 char_u *haystack;
17884 char_u *rest;
17885 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017886 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017888 needle = get_tv_string_chk(&argvars[1]);
17889 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017890
17891 rettv->vval.v_number = -1;
17892 if (needle == NULL || haystack == NULL)
17893 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017894
17895 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017896 if (argvars[2].v_type != VAR_UNKNOWN)
17897 {
17898 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017899 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017900 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017901 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017902 }
17903 else
17904 end_idx = haystack_len;
17905
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017907 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017909 lastmatch = haystack + end_idx;
17910 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017912 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 for (rest = haystack; *rest != '\0'; ++rest)
17914 {
17915 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017916 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017917 break;
17918 lastmatch = rest;
17919 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017921
17922 if (lastmatch == NULL)
17923 rettv->vval.v_number = -1;
17924 else
17925 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17926}
17927
17928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 * "strtrans()" function
17930 */
17931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017932f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 typval_T *argvars;
17934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 rettv->v_type = VAR_STRING;
17937 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938}
17939
17940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 * "submatch()" function
17942 */
17943 static void
17944f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017945 typval_T *argvars;
17946 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017947{
Bram Moolenaar41571762014-04-02 19:00:58 +020017948 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017949 int no;
17950 int retList = 0;
17951
17952 no = (int)get_tv_number_chk(&argvars[0], &error);
17953 if (error)
17954 return;
17955 error = FALSE;
17956 if (argvars[1].v_type != VAR_UNKNOWN)
17957 retList = get_tv_number_chk(&argvars[1], &error);
17958 if (error)
17959 return;
17960
17961 if (retList == 0)
17962 {
17963 rettv->v_type = VAR_STRING;
17964 rettv->vval.v_string = reg_submatch(no);
17965 }
17966 else
17967 {
17968 rettv->v_type = VAR_LIST;
17969 rettv->vval.v_list = reg_submatch_list(no);
17970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017971}
17972
17973/*
17974 * "substitute()" function
17975 */
17976 static void
17977f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017978 typval_T *argvars;
17979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017980{
17981 char_u patbuf[NUMBUFLEN];
17982 char_u subbuf[NUMBUFLEN];
17983 char_u flagsbuf[NUMBUFLEN];
17984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017985 char_u *str = get_tv_string_chk(&argvars[0]);
17986 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17987 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17988 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17989
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017991 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17992 rettv->vval.v_string = NULL;
17993 else
17994 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995}
17996
17997/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017998 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018001f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004{
18005 int id = 0;
18006#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018007 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 long col;
18009 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018010 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018012 lnum = get_tv_lnum(argvars); /* -1 on type error */
18013 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18014 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018016 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018017 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018018 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019#endif
18020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018021 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022}
18023
18024/*
18025 * "synIDattr(id, what [, mode])" function
18026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018028f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031{
18032 char_u *p = NULL;
18033#ifdef FEAT_SYN_HL
18034 int id;
18035 char_u *what;
18036 char_u *mode;
18037 char_u modebuf[NUMBUFLEN];
18038 int modec;
18039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018040 id = get_tv_number(&argvars[0]);
18041 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018042 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018046 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 modec = 0; /* replace invalid with current */
18048 }
18049 else
18050 {
18051#ifdef FEAT_GUI
18052 if (gui.in_use)
18053 modec = 'g';
18054 else
18055#endif
18056 if (t_colors > 1)
18057 modec = 'c';
18058 else
18059 modec = 't';
18060 }
18061
18062
18063 switch (TOLOWER_ASC(what[0]))
18064 {
18065 case 'b':
18066 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18067 p = highlight_color(id, what, modec);
18068 else /* bold */
18069 p = highlight_has_attr(id, HL_BOLD, modec);
18070 break;
18071
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018072 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 p = highlight_color(id, what, modec);
18074 break;
18075
18076 case 'i':
18077 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18078 p = highlight_has_attr(id, HL_INVERSE, modec);
18079 else /* italic */
18080 p = highlight_has_attr(id, HL_ITALIC, modec);
18081 break;
18082
18083 case 'n': /* name */
18084 p = get_highlight_name(NULL, id - 1);
18085 break;
18086
18087 case 'r': /* reverse */
18088 p = highlight_has_attr(id, HL_INVERSE, modec);
18089 break;
18090
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018091 case 's':
18092 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18093 p = highlight_color(id, what, modec);
18094 else /* standout */
18095 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 break;
18097
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018098 case 'u':
18099 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18100 /* underline */
18101 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18102 else
18103 /* undercurl */
18104 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 break;
18106 }
18107
18108 if (p != NULL)
18109 p = vim_strsave(p);
18110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 rettv->v_type = VAR_STRING;
18112 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113}
18114
18115/*
18116 * "synIDtrans(id)" function
18117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018119f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018120 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122{
18123 int id;
18124
18125#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018126 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127
18128 if (id > 0)
18129 id = syn_get_final_id(id);
18130 else
18131#endif
18132 id = 0;
18133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018138 * "synconcealed(lnum, col)" function
18139 */
18140 static void
18141f_synconcealed(argvars, rettv)
18142 typval_T *argvars UNUSED;
18143 typval_T *rettv;
18144{
18145#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18146 long lnum;
18147 long col;
18148 int syntax_flags = 0;
18149 int cchar;
18150 int matchid = 0;
18151 char_u str[NUMBUFLEN];
18152#endif
18153
18154 rettv->v_type = VAR_LIST;
18155 rettv->vval.v_list = NULL;
18156
18157#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18158 lnum = get_tv_lnum(argvars); /* -1 on type error */
18159 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18160
18161 vim_memset(str, NUL, sizeof(str));
18162
18163 if (rettv_list_alloc(rettv) != FAIL)
18164 {
18165 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18166 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18167 && curwin->w_p_cole > 0)
18168 {
18169 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18170 syntax_flags = get_syntax_info(&matchid);
18171
18172 /* get the conceal character */
18173 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18174 {
18175 cchar = syn_get_sub_char();
18176 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18177 cchar = lcs_conceal;
18178 if (cchar != NUL)
18179 {
18180# ifdef FEAT_MBYTE
18181 if (has_mbyte)
18182 (*mb_char2bytes)(cchar, str);
18183 else
18184# endif
18185 str[0] = cchar;
18186 }
18187 }
18188 }
18189
18190 list_append_number(rettv->vval.v_list,
18191 (syntax_flags & HL_CONCEAL) != 0);
18192 /* -1 to auto-determine strlen */
18193 list_append_string(rettv->vval.v_list, str, -1);
18194 list_append_number(rettv->vval.v_list, matchid);
18195 }
18196#endif
18197}
18198
18199/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018200 * "synstack(lnum, col)" function
18201 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018202 static void
18203f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018204 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018205 typval_T *rettv;
18206{
18207#ifdef FEAT_SYN_HL
18208 long lnum;
18209 long col;
18210 int i;
18211 int id;
18212#endif
18213
18214 rettv->v_type = VAR_LIST;
18215 rettv->vval.v_list = NULL;
18216
18217#ifdef FEAT_SYN_HL
18218 lnum = get_tv_lnum(argvars); /* -1 on type error */
18219 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18220
18221 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018222 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018223 && rettv_list_alloc(rettv) != FAIL)
18224 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018225 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018226 for (i = 0; ; ++i)
18227 {
18228 id = syn_get_stack_item(i);
18229 if (id < 0)
18230 break;
18231 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18232 break;
18233 }
18234 }
18235#endif
18236}
18237
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018239get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018240 typval_T *argvars;
18241 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018242 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018244 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018246 char_u *infile = NULL;
18247 char_u buf[NUMBUFLEN];
18248 int err = FALSE;
18249 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018250 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018252 rettv->v_type = VAR_STRING;
18253 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018254 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018255 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018256
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018257 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018258 {
18259 /*
18260 * Write the string to a temp file, to be used for input of the shell
18261 * command.
18262 */
18263 if ((infile = vim_tempname('i')) == NULL)
18264 {
18265 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018266 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018267 }
18268
18269 fd = mch_fopen((char *)infile, WRITEBIN);
18270 if (fd == NULL)
18271 {
18272 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018273 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018274 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018275 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018276 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018277 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18278 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018279 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018280 else
18281 {
18282 p = get_tv_string_buf_chk(&argvars[1], buf);
18283 if (p == NULL)
18284 {
18285 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018286 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018287 }
18288 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18289 err = TRUE;
18290 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018291 if (fclose(fd) != 0)
18292 err = TRUE;
18293 if (err)
18294 {
18295 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018296 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018297 }
18298 }
18299
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018300 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018302 int len;
18303 listitem_T *li;
18304 char_u *s = NULL;
18305 char_u *start;
18306 char_u *end;
18307 char_u *p;
18308 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018310 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18311 SHELL_SILENT | SHELL_COOKED, &len);
18312 if (res == NULL)
18313 goto errret;
18314
18315 list = list_alloc();
18316 if (list == NULL)
18317 goto errret;
18318
18319 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018321 start = res + i;
18322 for (end = start; i < len && *end != NL; ++end)
18323 ++i;
18324
18325 s = vim_strnsave(start, (int)(end - start));
18326 if (s == NULL)
18327 goto errret;
18328
18329 for (p = s, end = s + (end - start); p < end; ++p)
18330 if (*p == NUL)
18331 *p = NL;
18332
18333 li = listitem_alloc();
18334 if (li == NULL)
18335 {
18336 vim_free(s);
18337 goto errret;
18338 }
18339 li->li_tv.v_type = VAR_STRING;
18340 li->li_tv.vval.v_string = s;
18341 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018343
18344 rettv->v_type = VAR_LIST;
18345 rettv->vval.v_list = list;
18346 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018348 else
18349 {
18350 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18351 SHELL_SILENT | SHELL_COOKED, NULL);
18352#ifdef USE_CR
18353 /* translate <CR> into <NL> */
18354 if (res != NULL)
18355 {
18356 char_u *s;
18357
18358 for (s = res; *s; ++s)
18359 {
18360 if (*s == CAR)
18361 *s = NL;
18362 }
18363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364#else
18365# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018366 /* translate <CR><NL> into <NL> */
18367 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018369 char_u *s, *d;
18370
18371 d = res;
18372 for (s = res; *s; ++s)
18373 {
18374 if (s[0] == CAR && s[1] == NL)
18375 ++s;
18376 *d++ = *s;
18377 }
18378 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380# endif
18381#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018382 rettv->vval.v_string = res;
18383 res = NULL;
18384 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018385
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018386errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018387 if (infile != NULL)
18388 {
18389 mch_remove(infile);
18390 vim_free(infile);
18391 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018392 if (res != NULL)
18393 vim_free(res);
18394 if (list != NULL)
18395 list_free(list, TRUE);
18396}
18397
18398/*
18399 * "system()" function
18400 */
18401 static void
18402f_system(argvars, rettv)
18403 typval_T *argvars;
18404 typval_T *rettv;
18405{
18406 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18407}
18408
18409/*
18410 * "systemlist()" function
18411 */
18412 static void
18413f_systemlist(argvars, rettv)
18414 typval_T *argvars;
18415 typval_T *rettv;
18416{
18417 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018421 * "tabpagebuflist()" function
18422 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018423 static void
18424f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018425 typval_T *argvars UNUSED;
18426 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018427{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018428#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018429 tabpage_T *tp;
18430 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018431
18432 if (argvars[0].v_type == VAR_UNKNOWN)
18433 wp = firstwin;
18434 else
18435 {
18436 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18437 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018438 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018439 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018440 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018441 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018442 for (; wp != NULL; wp = wp->w_next)
18443 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018444 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018445 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018446 }
18447#endif
18448}
18449
18450
18451/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018452 * "tabpagenr()" function
18453 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018454 static void
18455f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018456 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018457 typval_T *rettv;
18458{
18459 int nr = 1;
18460#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018461 char_u *arg;
18462
18463 if (argvars[0].v_type != VAR_UNKNOWN)
18464 {
18465 arg = get_tv_string_chk(&argvars[0]);
18466 nr = 0;
18467 if (arg != NULL)
18468 {
18469 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018470 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018471 else
18472 EMSG2(_(e_invexpr2), arg);
18473 }
18474 }
18475 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018476 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018477#endif
18478 rettv->vval.v_number = nr;
18479}
18480
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018481
18482#ifdef FEAT_WINDOWS
18483static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18484
18485/*
18486 * Common code for tabpagewinnr() and winnr().
18487 */
18488 static int
18489get_winnr(tp, argvar)
18490 tabpage_T *tp;
18491 typval_T *argvar;
18492{
18493 win_T *twin;
18494 int nr = 1;
18495 win_T *wp;
18496 char_u *arg;
18497
18498 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18499 if (argvar->v_type != VAR_UNKNOWN)
18500 {
18501 arg = get_tv_string_chk(argvar);
18502 if (arg == NULL)
18503 nr = 0; /* type error; errmsg already given */
18504 else if (STRCMP(arg, "$") == 0)
18505 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18506 else if (STRCMP(arg, "#") == 0)
18507 {
18508 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18509 if (twin == NULL)
18510 nr = 0;
18511 }
18512 else
18513 {
18514 EMSG2(_(e_invexpr2), arg);
18515 nr = 0;
18516 }
18517 }
18518
18519 if (nr > 0)
18520 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18521 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018522 {
18523 if (wp == NULL)
18524 {
18525 /* didn't find it in this tabpage */
18526 nr = 0;
18527 break;
18528 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018529 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018530 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018531 return nr;
18532}
18533#endif
18534
18535/*
18536 * "tabpagewinnr()" function
18537 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018538 static void
18539f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018540 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018541 typval_T *rettv;
18542{
18543 int nr = 1;
18544#ifdef FEAT_WINDOWS
18545 tabpage_T *tp;
18546
18547 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18548 if (tp == NULL)
18549 nr = 0;
18550 else
18551 nr = get_winnr(tp, &argvars[1]);
18552#endif
18553 rettv->vval.v_number = nr;
18554}
18555
18556
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018557/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018558 * "tagfiles()" function
18559 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018560 static void
18561f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018562 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018563 typval_T *rettv;
18564{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018565 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018566 tagname_T tn;
18567 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018568
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018569 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018570 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018571 fname = alloc(MAXPATHL);
18572 if (fname == NULL)
18573 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018575 for (first = TRUE; ; first = FALSE)
18576 if (get_tagfname(&tn, first, fname) == FAIL
18577 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018578 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018579 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018580 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018581}
18582
18583/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018584 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018585 */
18586 static void
18587f_taglist(argvars, rettv)
18588 typval_T *argvars;
18589 typval_T *rettv;
18590{
18591 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018592
18593 tag_pattern = get_tv_string(&argvars[0]);
18594
18595 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018596 if (*tag_pattern == NUL)
18597 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018599 if (rettv_list_alloc(rettv) == OK)
18600 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018601}
18602
18603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 * "tempname()" function
18605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018607f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018608 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610{
18611 static int x = 'A';
18612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 rettv->v_type = VAR_STRING;
18614 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615
18616 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18617 * names. Skip 'I' and 'O', they are used for shell redirection. */
18618 do
18619 {
18620 if (x == 'Z')
18621 x = '0';
18622 else if (x == '9')
18623 x = 'A';
18624 else
18625 {
18626#ifdef EBCDIC
18627 if (x == 'I')
18628 x = 'J';
18629 else if (x == 'R')
18630 x = 'S';
18631 else
18632#endif
18633 ++x;
18634 }
18635 } while (x == 'I' || x == 'O');
18636}
18637
18638/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018639 * "test(list)" function: Just checking the walls...
18640 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018641 static void
18642f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018643 typval_T *argvars UNUSED;
18644 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018645{
18646 /* Used for unit testing. Change the code below to your liking. */
18647#if 0
18648 listitem_T *li;
18649 list_T *l;
18650 char_u *bad, *good;
18651
18652 if (argvars[0].v_type != VAR_LIST)
18653 return;
18654 l = argvars[0].vval.v_list;
18655 if (l == NULL)
18656 return;
18657 li = l->lv_first;
18658 if (li == NULL)
18659 return;
18660 bad = get_tv_string(&li->li_tv);
18661 li = li->li_next;
18662 if (li == NULL)
18663 return;
18664 good = get_tv_string(&li->li_tv);
18665 rettv->vval.v_number = test_edit_score(bad, good);
18666#endif
18667}
18668
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018669#ifdef FEAT_FLOAT
18670/*
18671 * "tan()" function
18672 */
18673 static void
18674f_tan(argvars, rettv)
18675 typval_T *argvars;
18676 typval_T *rettv;
18677{
18678 float_T f;
18679
18680 rettv->v_type = VAR_FLOAT;
18681 if (get_float_arg(argvars, &f) == OK)
18682 rettv->vval.v_float = tan(f);
18683 else
18684 rettv->vval.v_float = 0.0;
18685}
18686
18687/*
18688 * "tanh()" function
18689 */
18690 static void
18691f_tanh(argvars, rettv)
18692 typval_T *argvars;
18693 typval_T *rettv;
18694{
18695 float_T f;
18696
18697 rettv->v_type = VAR_FLOAT;
18698 if (get_float_arg(argvars, &f) == OK)
18699 rettv->vval.v_float = tanh(f);
18700 else
18701 rettv->vval.v_float = 0.0;
18702}
18703#endif
18704
Bram Moolenaard52d9742005-08-21 22:20:28 +000018705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 * "tolower(string)" function
18707 */
18708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018709f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 typval_T *argvars;
18711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712{
18713 char_u *p;
18714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 p = vim_strsave(get_tv_string(&argvars[0]));
18716 rettv->v_type = VAR_STRING;
18717 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
18719 if (p != NULL)
18720 while (*p != NUL)
18721 {
18722#ifdef FEAT_MBYTE
18723 int l;
18724
18725 if (enc_utf8)
18726 {
18727 int c, lc;
18728
18729 c = utf_ptr2char(p);
18730 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018731 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 /* TODO: reallocate string when byte count changes. */
18733 if (utf_char2len(lc) == l)
18734 utf_char2bytes(lc, p);
18735 p += l;
18736 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018737 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 p += l; /* skip multi-byte character */
18739 else
18740#endif
18741 {
18742 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18743 ++p;
18744 }
18745 }
18746}
18747
18748/*
18749 * "toupper(string)" function
18750 */
18751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 typval_T *argvars;
18754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018756 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018757 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758}
18759
18760/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018761 * "tr(string, fromstr, tostr)" function
18762 */
18763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 typval_T *argvars;
18766 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018767{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018768 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018769 char_u *fromstr;
18770 char_u *tostr;
18771 char_u *p;
18772#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018773 int inlen;
18774 int fromlen;
18775 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018776 int idx;
18777 char_u *cpstr;
18778 int cplen;
18779 int first = TRUE;
18780#endif
18781 char_u buf[NUMBUFLEN];
18782 char_u buf2[NUMBUFLEN];
18783 garray_T ga;
18784
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018785 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018786 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18787 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018788
18789 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 rettv->v_type = VAR_STRING;
18791 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018792 if (fromstr == NULL || tostr == NULL)
18793 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018794 ga_init2(&ga, (int)sizeof(char), 80);
18795
18796#ifdef FEAT_MBYTE
18797 if (!has_mbyte)
18798#endif
18799 /* not multi-byte: fromstr and tostr must be the same length */
18800 if (STRLEN(fromstr) != STRLEN(tostr))
18801 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018802#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018803error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018804#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018805 EMSG2(_(e_invarg2), fromstr);
18806 ga_clear(&ga);
18807 return;
18808 }
18809
18810 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018811 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018812 {
18813#ifdef FEAT_MBYTE
18814 if (has_mbyte)
18815 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018816 inlen = (*mb_ptr2len)(in_str);
18817 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018818 cplen = inlen;
18819 idx = 0;
18820 for (p = fromstr; *p != NUL; p += fromlen)
18821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018822 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018823 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018824 {
18825 for (p = tostr; *p != NUL; p += tolen)
18826 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018827 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018828 if (idx-- == 0)
18829 {
18830 cplen = tolen;
18831 cpstr = p;
18832 break;
18833 }
18834 }
18835 if (*p == NUL) /* tostr is shorter than fromstr */
18836 goto error;
18837 break;
18838 }
18839 ++idx;
18840 }
18841
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018842 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018843 {
18844 /* Check that fromstr and tostr have the same number of
18845 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018846 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018847 first = FALSE;
18848 for (p = tostr; *p != NUL; p += tolen)
18849 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018850 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018851 --idx;
18852 }
18853 if (idx != 0)
18854 goto error;
18855 }
18856
18857 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018858 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018859 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018860
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018861 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018862 }
18863 else
18864#endif
18865 {
18866 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018867 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018868 if (p != NULL)
18869 ga_append(&ga, tostr[p - fromstr]);
18870 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018871 ga_append(&ga, *in_str);
18872 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018873 }
18874 }
18875
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018876 /* add a terminating NUL */
18877 ga_grow(&ga, 1);
18878 ga_append(&ga, NUL);
18879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018881}
18882
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018883#ifdef FEAT_FLOAT
18884/*
18885 * "trunc({float})" function
18886 */
18887 static void
18888f_trunc(argvars, rettv)
18889 typval_T *argvars;
18890 typval_T *rettv;
18891{
18892 float_T f;
18893
18894 rettv->v_type = VAR_FLOAT;
18895 if (get_float_arg(argvars, &f) == OK)
18896 /* trunc() is not in C90, use floor() or ceil() instead. */
18897 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18898 else
18899 rettv->vval.v_float = 0.0;
18900}
18901#endif
18902
Bram Moolenaar8299df92004-07-10 09:47:34 +000018903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 * "type(expr)" function
18905 */
18906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018908 typval_T *argvars;
18909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018911 int n;
18912
18913 switch (argvars[0].v_type)
18914 {
18915 case VAR_NUMBER: n = 0; break;
18916 case VAR_STRING: n = 1; break;
18917 case VAR_FUNC: n = 2; break;
18918 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018919 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018920#ifdef FEAT_FLOAT
18921 case VAR_FLOAT: n = 5; break;
18922#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018923 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18924 }
18925 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
18928/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018929 * "undofile(name)" function
18930 */
18931 static void
18932f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018933 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018934 typval_T *rettv;
18935{
18936 rettv->v_type = VAR_STRING;
18937#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018938 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018939 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018940
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018941 if (*fname == NUL)
18942 {
18943 /* If there is no file name there will be no undo file. */
18944 rettv->vval.v_string = NULL;
18945 }
18946 else
18947 {
18948 char_u *ffname = FullName_save(fname, FALSE);
18949
18950 if (ffname != NULL)
18951 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18952 vim_free(ffname);
18953 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018954 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018955#else
18956 rettv->vval.v_string = NULL;
18957#endif
18958}
18959
18960/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018961 * "undotree()" function
18962 */
18963 static void
18964f_undotree(argvars, rettv)
18965 typval_T *argvars UNUSED;
18966 typval_T *rettv;
18967{
18968 if (rettv_dict_alloc(rettv) == OK)
18969 {
18970 dict_T *dict = rettv->vval.v_dict;
18971 list_T *list;
18972
Bram Moolenaar730cde92010-06-27 05:18:54 +020018973 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018974 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018975 dict_add_nr_str(dict, "save_last",
18976 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018977 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18978 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018979 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018980
18981 list = list_alloc();
18982 if (list != NULL)
18983 {
18984 u_eval_tree(curbuf->b_u_oldhead, list);
18985 dict_add_list(dict, "entries", list);
18986 }
18987 }
18988}
18989
18990/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018991 * "values(dict)" function
18992 */
18993 static void
18994f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018995 typval_T *argvars;
18996 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018997{
18998 dict_list(argvars, rettv, 1);
18999}
19000
19001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 * "virtcol(string)" function
19003 */
19004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019005f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019006 typval_T *argvars;
19007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008{
19009 colnr_T vcol = 0;
19010 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019011 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019013 fp = var2fpos(&argvars[0], FALSE, &fnum);
19014 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19015 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 {
19017 getvvcol(curwin, fp, NULL, NULL, &vcol);
19018 ++vcol;
19019 }
19020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022}
19023
19024/*
19025 * "visualmode()" function
19026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019029 typval_T *argvars UNUSED;
19030 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 char_u str[2];
19033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019034 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 str[0] = curbuf->b_visual_mode_eval;
19036 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019037 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038
19039 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019040 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042}
19043
19044/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019045 * "wildmenumode()" function
19046 */
19047 static void
19048f_wildmenumode(argvars, rettv)
19049 typval_T *argvars UNUSED;
19050 typval_T *rettv UNUSED;
19051{
19052#ifdef FEAT_WILDMENU
19053 if (wild_menu_showing)
19054 rettv->vval.v_number = 1;
19055#endif
19056}
19057
19058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 * "winbufnr(nr)" function
19060 */
19061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019062f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 typval_T *argvars;
19064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065{
19066 win_T *wp;
19067
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019068 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019070 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
19075/*
19076 * "wincol()" function
19077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019080 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082{
19083 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085}
19086
19087/*
19088 * "winheight(nr)" function
19089 */
19090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019091f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019092 typval_T *argvars;
19093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094{
19095 win_T *wp;
19096
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019097 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019101 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102}
19103
19104/*
19105 * "winline()" function
19106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019108f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111{
19112 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114}
19115
19116/*
19117 * "winnr()" function
19118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019120f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123{
19124 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019125
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019127 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
19132/*
19133 * "winrestcmd()" function
19134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
19140#ifdef FEAT_WINDOWS
19141 win_T *wp;
19142 int winnr = 1;
19143 garray_T ga;
19144 char_u buf[50];
19145
19146 ga_init2(&ga, (int)sizeof(char), 70);
19147 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19148 {
19149 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19150 ga_concat(&ga, buf);
19151# ifdef FEAT_VERTSPLIT
19152 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19153 ga_concat(&ga, buf);
19154# endif
19155 ++winnr;
19156 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019157 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164}
19165
19166/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019167 * "winrestview()" function
19168 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019169 static void
19170f_winrestview(argvars, rettv)
19171 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019172 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019173{
19174 dict_T *dict;
19175
19176 if (argvars[0].v_type != VAR_DICT
19177 || (dict = argvars[0].vval.v_dict) == NULL)
19178 EMSG(_(e_invarg));
19179 else
19180 {
19181 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19182 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19183#ifdef FEAT_VIRTUALEDIT
19184 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19185#endif
19186 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019187 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019188
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019189 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019190#ifdef FEAT_DIFF
19191 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19192#endif
19193 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19194 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19195
19196 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019197 win_new_height(curwin, curwin->w_height);
19198# ifdef FEAT_VERTSPLIT
19199 win_new_width(curwin, W_WIDTH(curwin));
19200# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019201 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019202
19203 if (curwin->w_topline == 0)
19204 curwin->w_topline = 1;
19205 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19206 curwin->w_topline = curbuf->b_ml.ml_line_count;
19207#ifdef FEAT_DIFF
19208 check_topfill(curwin, TRUE);
19209#endif
19210 }
19211}
19212
19213/*
19214 * "winsaveview()" function
19215 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019216 static void
19217f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019218 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019219 typval_T *rettv;
19220{
19221 dict_T *dict;
19222
Bram Moolenaara800b422010-06-27 01:15:55 +020019223 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019224 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019225 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019226
19227 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19228 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19229#ifdef FEAT_VIRTUALEDIT
19230 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19231#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019232 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019233 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19234
19235 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19236#ifdef FEAT_DIFF
19237 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19238#endif
19239 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19240 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19241}
19242
19243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 * "winwidth(nr)" function
19245 */
19246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019247f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 typval_T *argvars;
19249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250{
19251 win_T *wp;
19252
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019253 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 else
19257#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019258 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261#endif
19262}
19263
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019265 * Write list of strings to file
19266 */
19267 static int
19268write_list(fd, list, binary)
19269 FILE *fd;
19270 list_T *list;
19271 int binary;
19272{
19273 listitem_T *li;
19274 int c;
19275 int ret = OK;
19276 char_u *s;
19277
19278 for (li = list->lv_first; li != NULL; li = li->li_next)
19279 {
19280 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19281 {
19282 if (*s == '\n')
19283 c = putc(NUL, fd);
19284 else
19285 c = putc(*s, fd);
19286 if (c == EOF)
19287 {
19288 ret = FAIL;
19289 break;
19290 }
19291 }
19292 if (!binary || li->li_next != NULL)
19293 if (putc('\n', fd) == EOF)
19294 {
19295 ret = FAIL;
19296 break;
19297 }
19298 if (ret == FAIL)
19299 {
19300 EMSG(_(e_write));
19301 break;
19302 }
19303 }
19304 return ret;
19305}
19306
19307/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019308 * "writefile()" function
19309 */
19310 static void
19311f_writefile(argvars, rettv)
19312 typval_T *argvars;
19313 typval_T *rettv;
19314{
19315 int binary = FALSE;
19316 char_u *fname;
19317 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019318 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019319
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019320 if (check_restricted() || check_secure())
19321 return;
19322
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019323 if (argvars[0].v_type != VAR_LIST)
19324 {
19325 EMSG2(_(e_listarg), "writefile()");
19326 return;
19327 }
19328 if (argvars[0].vval.v_list == NULL)
19329 return;
19330
19331 if (argvars[2].v_type != VAR_UNKNOWN
19332 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19333 binary = TRUE;
19334
19335 /* Always open the file in binary mode, library functions have a mind of
19336 * their own about CR-LF conversion. */
19337 fname = get_tv_string(&argvars[1]);
19338 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19339 {
19340 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19341 ret = -1;
19342 }
19343 else
19344 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019345 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19346 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019347 fclose(fd);
19348 }
19349
19350 rettv->vval.v_number = ret;
19351}
19352
19353/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019354 * "xor(expr, expr)" function
19355 */
19356 static void
19357f_xor(argvars, rettv)
19358 typval_T *argvars;
19359 typval_T *rettv;
19360{
19361 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19362 ^ get_tv_number_chk(&argvars[1], NULL);
19363}
19364
19365
19366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019368 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 */
19370 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019371var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019373 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019374 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019376 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019378 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379
Bram Moolenaara5525202006-03-02 22:52:09 +000019380 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019381 if (varp->v_type == VAR_LIST)
19382 {
19383 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019384 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019385 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019386 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019387
19388 l = varp->vval.v_list;
19389 if (l == NULL)
19390 return NULL;
19391
19392 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019393 pos.lnum = list_find_nr(l, 0L, &error);
19394 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019395 return NULL; /* invalid line number */
19396
19397 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019398 pos.col = list_find_nr(l, 1L, &error);
19399 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019400 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019401 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019402
19403 /* We accept "$" for the column number: last column. */
19404 li = list_find(l, 1L);
19405 if (li != NULL && li->li_tv.v_type == VAR_STRING
19406 && li->li_tv.vval.v_string != NULL
19407 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19408 pos.col = len + 1;
19409
Bram Moolenaara5525202006-03-02 22:52:09 +000019410 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019411 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019412 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019413 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019414
Bram Moolenaara5525202006-03-02 22:52:09 +000019415#ifdef FEAT_VIRTUALEDIT
19416 /* Get the virtual offset. Defaults to zero. */
19417 pos.coladd = list_find_nr(l, 2L, &error);
19418 if (error)
19419 pos.coladd = 0;
19420#endif
19421
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019422 return &pos;
19423 }
19424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019425 name = get_tv_string_chk(varp);
19426 if (name == NULL)
19427 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019428 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019430 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19431 {
19432 if (VIsual_active)
19433 return &VIsual;
19434 return &curwin->w_cursor;
19435 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019436 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019438 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19440 return NULL;
19441 return pp;
19442 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019443
19444#ifdef FEAT_VIRTUALEDIT
19445 pos.coladd = 0;
19446#endif
19447
Bram Moolenaar477933c2007-07-17 14:32:23 +000019448 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019449 {
19450 pos.col = 0;
19451 if (name[1] == '0') /* "w0": first visible line */
19452 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019453 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019454 pos.lnum = curwin->w_topline;
19455 return &pos;
19456 }
19457 else if (name[1] == '$') /* "w$": last visible line */
19458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019459 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019460 pos.lnum = curwin->w_botline - 1;
19461 return &pos;
19462 }
19463 }
19464 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019466 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 {
19468 pos.lnum = curbuf->b_ml.ml_line_count;
19469 pos.col = 0;
19470 }
19471 else
19472 {
19473 pos.lnum = curwin->w_cursor.lnum;
19474 pos.col = (colnr_T)STRLEN(ml_get_curline());
19475 }
19476 return &pos;
19477 }
19478 return NULL;
19479}
19480
19481/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019482 * Convert list in "arg" into a position and optional file number.
19483 * When "fnump" is NULL there is no file number, only 3 items.
19484 * Note that the column is passed on as-is, the caller may want to decrement
19485 * it to use 1 for the first column.
19486 * Return FAIL when conversion is not possible, doesn't check the position for
19487 * validity.
19488 */
19489 static int
19490list2fpos(arg, posp, fnump)
19491 typval_T *arg;
19492 pos_T *posp;
19493 int *fnump;
19494{
19495 list_T *l = arg->vval.v_list;
19496 long i = 0;
19497 long n;
19498
Bram Moolenaarbde35262006-07-23 20:12:24 +000019499 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19500 * when "fnump" isn't NULL and "coladd" is optional. */
19501 if (arg->v_type != VAR_LIST
19502 || l == NULL
19503 || l->lv_len < (fnump == NULL ? 2 : 3)
19504 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019505 return FAIL;
19506
19507 if (fnump != NULL)
19508 {
19509 n = list_find_nr(l, i++, NULL); /* fnum */
19510 if (n < 0)
19511 return FAIL;
19512 if (n == 0)
19513 n = curbuf->b_fnum; /* current buffer */
19514 *fnump = n;
19515 }
19516
19517 n = list_find_nr(l, i++, NULL); /* lnum */
19518 if (n < 0)
19519 return FAIL;
19520 posp->lnum = n;
19521
19522 n = list_find_nr(l, i++, NULL); /* col */
19523 if (n < 0)
19524 return FAIL;
19525 posp->col = n;
19526
19527#ifdef FEAT_VIRTUALEDIT
19528 n = list_find_nr(l, i, NULL);
19529 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019530 posp->coladd = 0;
19531 else
19532 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019533#endif
19534
19535 return OK;
19536}
19537
19538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 * Get the length of an environment variable name.
19540 * Advance "arg" to the first character after the name.
19541 * Return 0 for error.
19542 */
19543 static int
19544get_env_len(arg)
19545 char_u **arg;
19546{
19547 char_u *p;
19548 int len;
19549
19550 for (p = *arg; vim_isIDc(*p); ++p)
19551 ;
19552 if (p == *arg) /* no name found */
19553 return 0;
19554
19555 len = (int)(p - *arg);
19556 *arg = p;
19557 return len;
19558}
19559
19560/*
19561 * Get the length of the name of a function or internal variable.
19562 * "arg" is advanced to the first non-white character after the name.
19563 * Return 0 if something is wrong.
19564 */
19565 static int
19566get_id_len(arg)
19567 char_u **arg;
19568{
19569 char_u *p;
19570 int len;
19571
19572 /* Find the end of the name. */
19573 for (p = *arg; eval_isnamec(*p); ++p)
19574 ;
19575 if (p == *arg) /* no name found */
19576 return 0;
19577
19578 len = (int)(p - *arg);
19579 *arg = skipwhite(p);
19580
19581 return len;
19582}
19583
19584/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019585 * Get the length of the name of a variable or function.
19586 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019588 * Return -1 if curly braces expansion failed.
19589 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 * If the name contains 'magic' {}'s, expand them and return the
19591 * expanded name in an allocated string via 'alias' - caller must free.
19592 */
19593 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019594get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 char_u **arg;
19596 char_u **alias;
19597 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019598 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599{
19600 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 char_u *p;
19602 char_u *expr_start;
19603 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604
19605 *alias = NULL; /* default to no alias */
19606
19607 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19608 && (*arg)[2] == (int)KE_SNR)
19609 {
19610 /* hard coded <SNR>, already translated */
19611 *arg += 3;
19612 return get_id_len(arg) + 3;
19613 }
19614 len = eval_fname_script(*arg);
19615 if (len > 0)
19616 {
19617 /* literal "<SID>", "s:" or "<SNR>" */
19618 *arg += len;
19619 }
19620
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019624 p = find_name_end(*arg, &expr_start, &expr_end,
19625 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 if (expr_start != NULL)
19627 {
19628 char_u *temp_string;
19629
19630 if (!evaluate)
19631 {
19632 len += (int)(p - *arg);
19633 *arg = skipwhite(p);
19634 return len;
19635 }
19636
19637 /*
19638 * Include any <SID> etc in the expanded string:
19639 * Thus the -len here.
19640 */
19641 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19642 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019643 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 *alias = temp_string;
19645 *arg = skipwhite(p);
19646 return (int)STRLEN(temp_string);
19647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648
19649 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019650 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 EMSG2(_(e_invexpr2), *arg);
19652
19653 return len;
19654}
19655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656/*
19657 * Find the end of a variable or function name, taking care of magic braces.
19658 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19659 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019660 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 * Return a pointer to just after the name. Equal to "arg" if there is no
19662 * valid name.
19663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019665find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 char_u *arg;
19667 char_u **expr_start;
19668 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019669 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 int mb_nest = 0;
19672 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 char_u *p;
19674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677 *expr_start = NULL;
19678 *expr_end = NULL;
19679 }
19680
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019681 /* Quick check for valid starting character. */
19682 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19683 return arg;
19684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685 for (p = arg; *p != NUL
19686 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019687 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019688 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019689 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019690 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019692 if (*p == '\'')
19693 {
19694 /* skip over 'string' to avoid counting [ and ] inside it. */
19695 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19696 ;
19697 if (*p == NUL)
19698 break;
19699 }
19700 else if (*p == '"')
19701 {
19702 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19703 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19704 if (*p == '\\' && p[1] != NUL)
19705 ++p;
19706 if (*p == NUL)
19707 break;
19708 }
19709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019710 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 if (*p == '[')
19713 ++br_nest;
19714 else if (*p == ']')
19715 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 if (*p == '{')
19721 {
19722 mb_nest++;
19723 if (expr_start != NULL && *expr_start == NULL)
19724 *expr_start = p;
19725 }
19726 else if (*p == '}')
19727 {
19728 mb_nest--;
19729 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19730 *expr_end = p;
19731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 }
19734
19735 return p;
19736}
19737
19738/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019739 * Expands out the 'magic' {}'s in a variable/function name.
19740 * Note that this can call itself recursively, to deal with
19741 * constructs like foo{bar}{baz}{bam}
19742 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19743 * "in_start" ^
19744 * "expr_start" ^
19745 * "expr_end" ^
19746 * "in_end" ^
19747 *
19748 * Returns a new allocated string, which the caller must free.
19749 * Returns NULL for failure.
19750 */
19751 static char_u *
19752make_expanded_name(in_start, expr_start, expr_end, in_end)
19753 char_u *in_start;
19754 char_u *expr_start;
19755 char_u *expr_end;
19756 char_u *in_end;
19757{
19758 char_u c1;
19759 char_u *retval = NULL;
19760 char_u *temp_result;
19761 char_u *nextcmd = NULL;
19762
19763 if (expr_end == NULL || in_end == NULL)
19764 return NULL;
19765 *expr_start = NUL;
19766 *expr_end = NUL;
19767 c1 = *in_end;
19768 *in_end = NUL;
19769
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019770 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019771 if (temp_result != NULL && nextcmd == NULL)
19772 {
19773 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19774 + (in_end - expr_end) + 1));
19775 if (retval != NULL)
19776 {
19777 STRCPY(retval, in_start);
19778 STRCAT(retval, temp_result);
19779 STRCAT(retval, expr_end + 1);
19780 }
19781 }
19782 vim_free(temp_result);
19783
19784 *in_end = c1; /* put char back for error messages */
19785 *expr_start = '{';
19786 *expr_end = '}';
19787
19788 if (retval != NULL)
19789 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019790 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019791 if (expr_start != NULL)
19792 {
19793 /* Further expansion! */
19794 temp_result = make_expanded_name(retval, expr_start,
19795 expr_end, temp_result);
19796 vim_free(retval);
19797 retval = temp_result;
19798 }
19799 }
19800
19801 return retval;
19802}
19803
19804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019806 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 */
19808 static int
19809eval_isnamec(c)
19810 int c;
19811{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019812 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19813}
19814
19815/*
19816 * Return TRUE if character "c" can be used as the first character in a
19817 * variable or function name (excluding '{' and '}').
19818 */
19819 static int
19820eval_isnamec1(c)
19821 int c;
19822{
19823 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824}
19825
19826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 * Set number v: variable to "val".
19828 */
19829 void
19830set_vim_var_nr(idx, val)
19831 int idx;
19832 long val;
19833{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019834 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835}
19836
19837/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019838 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 */
19840 long
19841get_vim_var_nr(idx)
19842 int idx;
19843{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019844 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845}
19846
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019847/*
19848 * Get string v: variable value. Uses a static buffer, can only be used once.
19849 */
19850 char_u *
19851get_vim_var_str(idx)
19852 int idx;
19853{
19854 return get_tv_string(&vimvars[idx].vv_tv);
19855}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019856
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019858 * Get List v: variable value. Caller must take care of reference count when
19859 * needed.
19860 */
19861 list_T *
19862get_vim_var_list(idx)
19863 int idx;
19864{
19865 return vimvars[idx].vv_list;
19866}
19867
19868/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019869 * Set v:char to character "c".
19870 */
19871 void
19872set_vim_var_char(c)
19873 int c;
19874{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019875 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019876
19877#ifdef FEAT_MBYTE
19878 if (has_mbyte)
19879 buf[(*mb_char2bytes)(c, buf)] = NUL;
19880 else
19881#endif
19882 {
19883 buf[0] = c;
19884 buf[1] = NUL;
19885 }
19886 set_vim_var_string(VV_CHAR, buf, -1);
19887}
19888
19889/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019890 * Set v:count to "count" and v:count1 to "count1".
19891 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 */
19893 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019894set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 long count;
19896 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019897 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019899 if (set_prevcount)
19900 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019901 vimvars[VV_COUNT].vv_nr = count;
19902 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903}
19904
19905/*
19906 * Set string v: variable to a copy of "val".
19907 */
19908 void
19909set_vim_var_string(idx, val, len)
19910 int idx;
19911 char_u *val;
19912 int len; /* length of "val" to use or -1 (whole string) */
19913{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019914 /* Need to do this (at least) once, since we can't initialize a union.
19915 * Will always be invoked when "v:progname" is set. */
19916 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19917
Bram Moolenaare9a41262005-01-15 22:18:47 +000019918 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019920 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019922 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019924 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925}
19926
19927/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019928 * Set List v: variable to "val".
19929 */
19930 void
19931set_vim_var_list(idx, val)
19932 int idx;
19933 list_T *val;
19934{
19935 list_unref(vimvars[idx].vv_list);
19936 vimvars[idx].vv_list = val;
19937 if (val != NULL)
19938 ++val->lv_refcount;
19939}
19940
19941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 * Set v:register if needed.
19943 */
19944 void
19945set_reg_var(c)
19946 int c;
19947{
19948 char_u regname;
19949
19950 if (c == 0 || c == ' ')
19951 regname = '"';
19952 else
19953 regname = c;
19954 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019955 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 set_vim_var_string(VV_REG, &regname, 1);
19957}
19958
19959/*
19960 * Get or set v:exception. If "oldval" == NULL, return the current value.
19961 * Otherwise, restore the value to "oldval" and return NULL.
19962 * Must always be called in pairs to save and restore v:exception! Does not
19963 * take care of memory allocations.
19964 */
19965 char_u *
19966v_exception(oldval)
19967 char_u *oldval;
19968{
19969 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019970 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971
Bram Moolenaare9a41262005-01-15 22:18:47 +000019972 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 return NULL;
19974}
19975
19976/*
19977 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19978 * Otherwise, restore the value to "oldval" and return NULL.
19979 * Must always be called in pairs to save and restore v:throwpoint! Does not
19980 * take care of memory allocations.
19981 */
19982 char_u *
19983v_throwpoint(oldval)
19984 char_u *oldval;
19985{
19986 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019987 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988
Bram Moolenaare9a41262005-01-15 22:18:47 +000019989 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 return NULL;
19991}
19992
19993#if defined(FEAT_AUTOCMD) || defined(PROTO)
19994/*
19995 * Set v:cmdarg.
19996 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19997 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19998 * Must always be called in pairs!
19999 */
20000 char_u *
20001set_cmdarg(eap, oldarg)
20002 exarg_T *eap;
20003 char_u *oldarg;
20004{
20005 char_u *oldval;
20006 char_u *newval;
20007 unsigned len;
20008
Bram Moolenaare9a41262005-01-15 22:18:47 +000020009 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020010 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020012 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020013 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020014 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 }
20016
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020017 if (eap->force_bin == FORCE_BIN)
20018 len = 6;
20019 else if (eap->force_bin == FORCE_NOBIN)
20020 len = 8;
20021 else
20022 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020023
20024 if (eap->read_edit)
20025 len += 7;
20026
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020027 if (eap->force_ff != 0)
20028 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20029# ifdef FEAT_MBYTE
20030 if (eap->force_enc != 0)
20031 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020032 if (eap->bad_char != 0)
20033 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020034# endif
20035
20036 newval = alloc(len + 1);
20037 if (newval == NULL)
20038 return NULL;
20039
20040 if (eap->force_bin == FORCE_BIN)
20041 sprintf((char *)newval, " ++bin");
20042 else if (eap->force_bin == FORCE_NOBIN)
20043 sprintf((char *)newval, " ++nobin");
20044 else
20045 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020046
20047 if (eap->read_edit)
20048 STRCAT(newval, " ++edit");
20049
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020050 if (eap->force_ff != 0)
20051 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20052 eap->cmd + eap->force_ff);
20053# ifdef FEAT_MBYTE
20054 if (eap->force_enc != 0)
20055 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20056 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020057 if (eap->bad_char == BAD_KEEP)
20058 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20059 else if (eap->bad_char == BAD_DROP)
20060 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20061 else if (eap->bad_char != 0)
20062 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020063# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020064 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020065 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066}
20067#endif
20068
20069/*
20070 * Get the value of internal variable "name".
20071 * Return OK or FAIL.
20072 */
20073 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020074get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 char_u *name;
20076 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020078 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020079 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080{
20081 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 typval_T *tv = NULL;
20083 typval_T atv;
20084 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086
20087 /* truncate the name, so that we can use strcmp() */
20088 cc = name[len];
20089 name[len] = NUL;
20090
20091 /*
20092 * Check for "b:changedtick".
20093 */
20094 if (STRCMP(name, "b:changedtick") == 0)
20095 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020096 atv.v_type = VAR_NUMBER;
20097 atv.vval.v_number = curbuf->b_changedtick;
20098 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 }
20100
20101 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 * Check for user-defined variables.
20103 */
20104 else
20105 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020106 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020108 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 }
20110
Bram Moolenaare9a41262005-01-15 22:18:47 +000020111 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020113 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 ret = FAIL;
20116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020118 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119
20120 name[len] = cc;
20121
20122 return ret;
20123}
20124
20125/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020126 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20127 * Also handle function call with Funcref variable: func(expr)
20128 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20129 */
20130 static int
20131handle_subscript(arg, rettv, evaluate, verbose)
20132 char_u **arg;
20133 typval_T *rettv;
20134 int evaluate; /* do more than finding the end */
20135 int verbose; /* give error messages */
20136{
20137 int ret = OK;
20138 dict_T *selfdict = NULL;
20139 char_u *s;
20140 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020141 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020142
20143 while (ret == OK
20144 && (**arg == '['
20145 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020146 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020147 && !vim_iswhite(*(*arg - 1)))
20148 {
20149 if (**arg == '(')
20150 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020151 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020152 if (evaluate)
20153 {
20154 functv = *rettv;
20155 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020156
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020157 /* Invoke the function. Recursive! */
20158 s = functv.vval.v_string;
20159 }
20160 else
20161 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020162 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020163 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20164 &len, evaluate, selfdict);
20165
20166 /* Clear the funcref afterwards, so that deleting it while
20167 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020168 if (evaluate)
20169 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020170
20171 /* Stop the expression evaluation when immediately aborting on
20172 * error, or when an interrupt occurred or an exception was thrown
20173 * but not caught. */
20174 if (aborting())
20175 {
20176 if (ret == OK)
20177 clear_tv(rettv);
20178 ret = FAIL;
20179 }
20180 dict_unref(selfdict);
20181 selfdict = NULL;
20182 }
20183 else /* **arg == '[' || **arg == '.' */
20184 {
20185 dict_unref(selfdict);
20186 if (rettv->v_type == VAR_DICT)
20187 {
20188 selfdict = rettv->vval.v_dict;
20189 if (selfdict != NULL)
20190 ++selfdict->dv_refcount;
20191 }
20192 else
20193 selfdict = NULL;
20194 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20195 {
20196 clear_tv(rettv);
20197 ret = FAIL;
20198 }
20199 }
20200 }
20201 dict_unref(selfdict);
20202 return ret;
20203}
20204
20205/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020206 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020207 * value).
20208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020211{
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020213}
20214
20215/*
20216 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 * The string "s" must have been allocated, it is consumed.
20218 * Return NULL for out of memory, the variable otherwise.
20219 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 char_u *s;
20223{
Bram Moolenaar33570922005-01-25 22:26:29 +000020224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 rettv = alloc_tv();
20227 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 rettv->v_type = VAR_STRING;
20230 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 }
20232 else
20233 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235}
20236
20237/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020238 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020240 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020241free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
20244 if (varp != NULL)
20245 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020246 switch (varp->v_type)
20247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020248 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020249 func_unref(varp->vval.v_string);
20250 /*FALLTHROUGH*/
20251 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020252 vim_free(varp->vval.v_string);
20253 break;
20254 case VAR_LIST:
20255 list_unref(varp->vval.v_list);
20256 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020257 case VAR_DICT:
20258 dict_unref(varp->vval.v_dict);
20259 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020260 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020261#ifdef FEAT_FLOAT
20262 case VAR_FLOAT:
20263#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020264 case VAR_UNKNOWN:
20265 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020266 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020267 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020268 break;
20269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 vim_free(varp);
20271 }
20272}
20273
20274/*
20275 * Free the memory for a variable value and set the value to NULL or 0.
20276 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020277 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
20281 if (varp != NULL)
20282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020285 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020286 func_unref(varp->vval.v_string);
20287 /*FALLTHROUGH*/
20288 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020289 vim_free(varp->vval.v_string);
20290 varp->vval.v_string = NULL;
20291 break;
20292 case VAR_LIST:
20293 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020294 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020295 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020296 case VAR_DICT:
20297 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020298 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020299 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301 varp->vval.v_number = 0;
20302 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020303#ifdef FEAT_FLOAT
20304 case VAR_FLOAT:
20305 varp->vval.v_float = 0.0;
20306 break;
20307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020308 case VAR_UNKNOWN:
20309 break;
20310 default:
20311 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020313 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 }
20315}
20316
20317/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 * Set the value of a variable to NULL without freeing items.
20319 */
20320 static void
20321init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020322 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323{
20324 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020326}
20327
20328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 * Get the number value of a variable.
20330 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020331 * For incompatible types, return 0.
20332 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20333 * caller of incompatible types: it sets *denote to TRUE if "denote"
20334 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 */
20336 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020337get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020340 int error = FALSE;
20341
20342 return get_tv_number_chk(varp, &error); /* return 0L on error */
20343}
20344
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020345 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020346get_tv_number_chk(varp, denote)
20347 typval_T *varp;
20348 int *denote;
20349{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020350 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020352 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020354 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020355 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020356#ifdef FEAT_FLOAT
20357 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020358 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020359 break;
20360#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020361 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020362 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020363 break;
20364 case VAR_STRING:
20365 if (varp->vval.v_string != NULL)
20366 vim_str2nr(varp->vval.v_string, NULL, NULL,
20367 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020368 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020369 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020370 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020371 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020372 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020373 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020374 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020376 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020379 if (denote == NULL) /* useful for values that must be unsigned */
20380 n = -1;
20381 else
20382 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020383 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384}
20385
20386/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020387 * Get the lnum from the first argument.
20388 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020389 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 */
20391 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020393 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394{
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 linenr_T lnum;
20397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020398 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 if (lnum == 0) /* no valid number, try using line() */
20400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 rettv.v_type = VAR_NUMBER;
20402 f_line(argvars, &rettv);
20403 lnum = rettv.vval.v_number;
20404 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
20406 return lnum;
20407}
20408
20409/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020410 * Get the lnum from the first argument.
20411 * Also accepts "$", then "buf" is used.
20412 * Returns 0 on error.
20413 */
20414 static linenr_T
20415get_tv_lnum_buf(argvars, buf)
20416 typval_T *argvars;
20417 buf_T *buf;
20418{
20419 if (argvars[0].v_type == VAR_STRING
20420 && argvars[0].vval.v_string != NULL
20421 && argvars[0].vval.v_string[0] == '$'
20422 && buf != NULL)
20423 return buf->b_ml.ml_line_count;
20424 return get_tv_number_chk(&argvars[0], NULL);
20425}
20426
20427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 * Get the string value of a variable.
20429 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020430 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20431 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 * If the String variable has never been set, return an empty string.
20433 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020434 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20435 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 */
20437 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440{
20441 static char_u mybuf[NUMBUFLEN];
20442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020443 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444}
20445
20446 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020448 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020449 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020451 char_u *res = get_tv_string_buf_chk(varp, buf);
20452
20453 return res != NULL ? res : (char_u *)"";
20454}
20455
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020456 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020457get_tv_string_chk(varp)
20458 typval_T *varp;
20459{
20460 static char_u mybuf[NUMBUFLEN];
20461
20462 return get_tv_string_buf_chk(varp, mybuf);
20463}
20464
20465 static char_u *
20466get_tv_string_buf_chk(varp, buf)
20467 typval_T *varp;
20468 char_u *buf;
20469{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020470 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020472 case VAR_NUMBER:
20473 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20474 return buf;
20475 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020476 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020477 break;
20478 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020479 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020480 break;
20481 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020482 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020483 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020484#ifdef FEAT_FLOAT
20485 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020486 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020487 break;
20488#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020489 case VAR_STRING:
20490 if (varp->vval.v_string != NULL)
20491 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020492 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020493 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020494 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020495 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498}
20499
20500/*
20501 * Find variable "name" in the list of variables.
20502 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020503 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020504 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020507 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020508find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020510 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020511 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020514 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515
Bram Moolenaara7043832005-01-21 11:56:39 +000020516 ht = find_var_ht(name, &varname);
20517 if (htp != NULL)
20518 *htp = ht;
20519 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020521 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522}
20523
20524/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020525 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020526 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020528 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020529find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020530 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020531 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020532 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020533 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020534{
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 hashitem_T *hi;
20536
20537 if (*varname == NUL)
20538 {
20539 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020540 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020542 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 case 'g': return &globvars_var;
20544 case 'v': return &vimvars_var;
20545 case 'b': return &curbuf->b_bufvar;
20546 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020547#ifdef FEAT_WINDOWS
20548 case 't': return &curtab->tp_winvar;
20549#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020550 case 'l': return current_funccal == NULL
20551 ? NULL : &current_funccal->l_vars_var;
20552 case 'a': return current_funccal == NULL
20553 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020554 }
20555 return NULL;
20556 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020557
20558 hi = hash_find(ht, varname);
20559 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020560 {
20561 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020562 * worked find the variable again. Don't auto-load a script if it was
20563 * loaded already, otherwise it would be loaded every time when
20564 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020565 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020566 {
20567 /* Note: script_autoload() may make "hi" invalid. It must either
20568 * be obtained again or not used. */
20569 if (!script_autoload(varname, FALSE) || aborting())
20570 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020571 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020572 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020573 if (HASHITEM_EMPTY(hi))
20574 return NULL;
20575 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020576 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020577}
20578
20579/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020581 * Set "varname" to the start of name without ':'.
20582 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020584find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 char_u *name;
20586 char_u **varname;
20587{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020588 hashitem_T *hi;
20589
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 if (name[1] != ':')
20591 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020592 /* The name must not start with a colon or #. */
20593 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 return NULL;
20595 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020596
20597 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020598 hi = hash_find(&compat_hashtab, name);
20599 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020600 return &compat_hashtab;
20601
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020603 return &globvarht; /* global variable */
20604 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 }
20606 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020607 if (*name == 'g') /* global variable */
20608 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020609 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20610 */
20611 if (vim_strchr(name + 2, ':') != NULL
20612 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020613 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020615 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020617 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020618#ifdef FEAT_WINDOWS
20619 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020620 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020622 if (*name == 'v') /* v: variable */
20623 return &vimvarht;
20624 if (*name == 'a' && current_funccal != NULL) /* function argument */
20625 return &current_funccal->l_avars.dv_hashtab;
20626 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20627 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 if (*name == 's' /* script variable */
20629 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20630 return &SCRIPT_VARS(current_SID);
20631 return NULL;
20632}
20633
20634/*
20635 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020636 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 * Returns NULL when it doesn't exist.
20638 */
20639 char_u *
20640get_var_value(name)
20641 char_u *name;
20642{
Bram Moolenaar33570922005-01-25 22:26:29 +000020643 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020645 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 if (v == NULL)
20647 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020648 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649}
20650
20651/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020652 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 * sourcing this script and when executing functions defined in the script.
20654 */
20655 void
20656new_script_vars(id)
20657 scid_T id;
20658{
Bram Moolenaara7043832005-01-21 11:56:39 +000020659 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020660 hashtab_T *ht;
20661 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020662
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20664 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020665 /* Re-allocating ga_data means that an ht_array pointing to
20666 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020667 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020668 for (i = 1; i <= ga_scripts.ga_len; ++i)
20669 {
20670 ht = &SCRIPT_VARS(i);
20671 if (ht->ht_mask == HT_INIT_SIZE - 1)
20672 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020673 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020674 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020675 }
20676
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 while (ga_scripts.ga_len < id)
20678 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020679 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020680 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020681 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 }
20684 }
20685}
20686
20687/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020688 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20689 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 */
20691 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020692init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020693 dict_T *dict;
20694 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020695 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696{
Bram Moolenaar33570922005-01-25 22:26:29 +000020697 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020698 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020699 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020700 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020701 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020702 dict_var->di_tv.vval.v_dict = dict;
20703 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020704 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020705 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20706 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707}
20708
20709/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020710 * Unreference a dictionary initialized by init_var_dict().
20711 */
20712 void
20713unref_var_dict(dict)
20714 dict_T *dict;
20715{
20716 /* Now the dict needs to be freed if no one else is using it, go back to
20717 * normal reference counting. */
20718 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20719 dict_unref(dict);
20720}
20721
20722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020724 * Frees all allocated variables and the value they contain.
20725 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 */
20727 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020728vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020729 hashtab_T *ht;
20730{
20731 vars_clear_ext(ht, TRUE);
20732}
20733
20734/*
20735 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20736 */
20737 static void
20738vars_clear_ext(ht, free_val)
20739 hashtab_T *ht;
20740 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741{
Bram Moolenaara7043832005-01-21 11:56:39 +000020742 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020743 hashitem_T *hi;
20744 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745
Bram Moolenaar33570922005-01-25 22:26:29 +000020746 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020747 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020748 for (hi = ht->ht_array; todo > 0; ++hi)
20749 {
20750 if (!HASHITEM_EMPTY(hi))
20751 {
20752 --todo;
20753
Bram Moolenaar33570922005-01-25 22:26:29 +000020754 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020755 * ht_array might change then. hash_clear() takes care of it
20756 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020757 v = HI2DI(hi);
20758 if (free_val)
20759 clear_tv(&v->di_tv);
20760 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20761 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020762 }
20763 }
20764 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020765 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766}
20767
Bram Moolenaara7043832005-01-21 11:56:39 +000020768/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020769 * Delete a variable from hashtab "ht" at item "hi".
20770 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020773delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020774 hashtab_T *ht;
20775 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776{
Bram Moolenaar33570922005-01-25 22:26:29 +000020777 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020778
20779 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020780 clear_tv(&di->di_tv);
20781 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782}
20783
20784/*
20785 * List the value of one internal variable.
20786 */
20787 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020788list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020789 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020791 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020793 char_u *tofree;
20794 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020796
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020797 current_copyID += COPYID_INC;
20798 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020800 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020801 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802}
20803
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020805list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 char_u *prefix;
20807 char_u *name;
20808 int type;
20809 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020810 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
Bram Moolenaar31859182007-08-14 20:41:13 +000020812 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20813 msg_start();
20814 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 if (name != NULL) /* "a:" vars don't have a name stored */
20816 msg_puts(name);
20817 msg_putchar(' ');
20818 msg_advance(22);
20819 if (type == VAR_NUMBER)
20820 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020821 else if (type == VAR_FUNC)
20822 msg_putchar('*');
20823 else if (type == VAR_LIST)
20824 {
20825 msg_putchar('[');
20826 if (*string == '[')
20827 ++string;
20828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020829 else if (type == VAR_DICT)
20830 {
20831 msg_putchar('{');
20832 if (*string == '{')
20833 ++string;
20834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 else
20836 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020837
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020839
20840 if (type == VAR_FUNC)
20841 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020842 if (*first)
20843 {
20844 msg_clr_eos();
20845 *first = FALSE;
20846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847}
20848
20849/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020850 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 * If the variable already exists, the value is updated.
20852 * Otherwise the variable is created.
20853 */
20854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020855set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020857 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859{
Bram Moolenaar33570922005-01-25 22:26:29 +000020860 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020862 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020864 ht = find_var_ht(name, &varname);
20865 if (ht == NULL || *varname == NUL)
20866 {
20867 EMSG2(_(e_illvar), name);
20868 return;
20869 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020870 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020871
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020872 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20873 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020874
Bram Moolenaar33570922005-01-25 22:26:29 +000020875 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020877 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020878 if (var_check_ro(v->di_flags, name)
20879 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020880 return;
20881 if (v->di_tv.v_type != tv->v_type
20882 && !((v->di_tv.v_type == VAR_STRING
20883 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020885 || tv->v_type == VAR_NUMBER))
20886#ifdef FEAT_FLOAT
20887 && !((v->di_tv.v_type == VAR_NUMBER
20888 || v->di_tv.v_type == VAR_FLOAT)
20889 && (tv->v_type == VAR_NUMBER
20890 || tv->v_type == VAR_FLOAT))
20891#endif
20892 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020893 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020894 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020895 return;
20896 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020897
20898 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020899 * Handle setting internal v: variables separately: we don't change
20900 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020901 */
20902 if (ht == &vimvarht)
20903 {
20904 if (v->di_tv.v_type == VAR_STRING)
20905 {
20906 vim_free(v->di_tv.vval.v_string);
20907 if (copy || tv->v_type != VAR_STRING)
20908 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20909 else
20910 {
20911 /* Take over the string to avoid an extra alloc/free. */
20912 v->di_tv.vval.v_string = tv->vval.v_string;
20913 tv->vval.v_string = NULL;
20914 }
20915 }
20916 else if (v->di_tv.v_type != VAR_NUMBER)
20917 EMSG2(_(e_intern2), "set_var()");
20918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020919 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020920 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020921 if (STRCMP(varname, "searchforward") == 0)
20922 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020923#ifdef FEAT_SEARCH_EXTRA
20924 else if (STRCMP(varname, "hlsearch") == 0)
20925 {
20926 no_hlsearch = !v->di_tv.vval.v_number;
20927 redraw_all_later(SOME_VALID);
20928 }
20929#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020930 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020931 return;
20932 }
20933
20934 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 }
20936 else /* add a new variable */
20937 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020938 /* Can't add "v:" variable. */
20939 if (ht == &vimvarht)
20940 {
20941 EMSG2(_(e_illvar), name);
20942 return;
20943 }
20944
Bram Moolenaar92124a32005-06-17 22:03:40 +000020945 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020946 if (!valid_varname(varname))
20947 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020949 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20950 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020951 if (v == NULL)
20952 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020956 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 return;
20958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020959 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020962 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020963 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020964 else
20965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020966 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020967 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970}
20971
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020972/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020973 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020974 * Also give an error message.
20975 */
20976 static int
20977var_check_ro(flags, name)
20978 int flags;
20979 char_u *name;
20980{
20981 if (flags & DI_FLAGS_RO)
20982 {
20983 EMSG2(_(e_readonlyvar), name);
20984 return TRUE;
20985 }
20986 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20987 {
20988 EMSG2(_(e_readonlysbx), name);
20989 return TRUE;
20990 }
20991 return FALSE;
20992}
20993
20994/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020995 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20996 * Also give an error message.
20997 */
20998 static int
20999var_check_fixed(flags, name)
21000 int flags;
21001 char_u *name;
21002{
21003 if (flags & DI_FLAGS_FIX)
21004 {
21005 EMSG2(_("E795: Cannot delete variable %s"), name);
21006 return TRUE;
21007 }
21008 return FALSE;
21009}
21010
21011/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021012 * Check if a funcref is assigned to a valid variable name.
21013 * Return TRUE and give an error if not.
21014 */
21015 static int
21016var_check_func_name(name, new_var)
21017 char_u *name; /* points to start of variable name */
21018 int new_var; /* TRUE when creating the variable */
21019{
21020 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
21021 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21022 ? name[2] : name[0]))
21023 {
21024 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21025 name);
21026 return TRUE;
21027 }
21028 /* Don't allow hiding a function. When "v" is not NULL we might be
21029 * assigning another function to the same var, the type is checked
21030 * below. */
21031 if (new_var && function_exists(name))
21032 {
21033 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21034 name);
21035 return TRUE;
21036 }
21037 return FALSE;
21038}
21039
21040/*
21041 * Check if a variable name is valid.
21042 * Return FALSE and give an error if not.
21043 */
21044 static int
21045valid_varname(varname)
21046 char_u *varname;
21047{
21048 char_u *p;
21049
21050 for (p = varname; *p != NUL; ++p)
21051 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21052 && *p != AUTOLOAD_CHAR)
21053 {
21054 EMSG2(_(e_illvar), varname);
21055 return FALSE;
21056 }
21057 return TRUE;
21058}
21059
21060/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021061 * Return TRUE if typeval "tv" is set to be locked (immutable).
21062 * Also give an error message, using "name".
21063 */
21064 static int
21065tv_check_lock(lock, name)
21066 int lock;
21067 char_u *name;
21068{
21069 if (lock & VAR_LOCKED)
21070 {
21071 EMSG2(_("E741: Value is locked: %s"),
21072 name == NULL ? (char_u *)_("Unknown") : name);
21073 return TRUE;
21074 }
21075 if (lock & VAR_FIXED)
21076 {
21077 EMSG2(_("E742: Cannot change value of %s"),
21078 name == NULL ? (char_u *)_("Unknown") : name);
21079 return TRUE;
21080 }
21081 return FALSE;
21082}
21083
21084/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021087 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021088 * It is OK for "from" and "to" to point to the same item. This is used to
21089 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021090 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021091 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021092copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021093 typval_T *from;
21094 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021096 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021097 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021098 switch (from->v_type)
21099 {
21100 case VAR_NUMBER:
21101 to->vval.v_number = from->vval.v_number;
21102 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021103#ifdef FEAT_FLOAT
21104 case VAR_FLOAT:
21105 to->vval.v_float = from->vval.v_float;
21106 break;
21107#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021108 case VAR_STRING:
21109 case VAR_FUNC:
21110 if (from->vval.v_string == NULL)
21111 to->vval.v_string = NULL;
21112 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021113 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021114 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021115 if (from->v_type == VAR_FUNC)
21116 func_ref(to->vval.v_string);
21117 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021118 break;
21119 case VAR_LIST:
21120 if (from->vval.v_list == NULL)
21121 to->vval.v_list = NULL;
21122 else
21123 {
21124 to->vval.v_list = from->vval.v_list;
21125 ++to->vval.v_list->lv_refcount;
21126 }
21127 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021128 case VAR_DICT:
21129 if (from->vval.v_dict == NULL)
21130 to->vval.v_dict = NULL;
21131 else
21132 {
21133 to->vval.v_dict = from->vval.v_dict;
21134 ++to->vval.v_dict->dv_refcount;
21135 }
21136 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021137 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021139 break;
21140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142
21143/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021144 * Make a copy of an item.
21145 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021146 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21147 * reference to an already copied list/dict can be used.
21148 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021149 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021150 static int
21151item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021152 typval_T *from;
21153 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021154 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021155 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021156{
21157 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021158 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021159
Bram Moolenaar33570922005-01-25 22:26:29 +000021160 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021161 {
21162 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021163 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021164 }
21165 ++recurse;
21166
21167 switch (from->v_type)
21168 {
21169 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021170#ifdef FEAT_FLOAT
21171 case VAR_FLOAT:
21172#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021173 case VAR_STRING:
21174 case VAR_FUNC:
21175 copy_tv(from, to);
21176 break;
21177 case VAR_LIST:
21178 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021179 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021180 if (from->vval.v_list == NULL)
21181 to->vval.v_list = NULL;
21182 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21183 {
21184 /* use the copy made earlier */
21185 to->vval.v_list = from->vval.v_list->lv_copylist;
21186 ++to->vval.v_list->lv_refcount;
21187 }
21188 else
21189 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21190 if (to->vval.v_list == NULL)
21191 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021192 break;
21193 case VAR_DICT:
21194 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021195 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021196 if (from->vval.v_dict == NULL)
21197 to->vval.v_dict = NULL;
21198 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21199 {
21200 /* use the copy made earlier */
21201 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21202 ++to->vval.v_dict->dv_refcount;
21203 }
21204 else
21205 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21206 if (to->vval.v_dict == NULL)
21207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021208 break;
21209 default:
21210 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021212 }
21213 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021214 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021215}
21216
21217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 * ":echo expr1 ..." print each argument separated with a space, add a
21219 * newline at the end.
21220 * ":echon expr1 ..." print each argument plain.
21221 */
21222 void
21223ex_echo(eap)
21224 exarg_T *eap;
21225{
21226 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021227 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021228 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 char_u *p;
21230 int needclr = TRUE;
21231 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021232 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233
21234 if (eap->skip)
21235 ++emsg_skip;
21236 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21237 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021238 /* If eval1() causes an error message the text from the command may
21239 * still need to be cleared. E.g., "echo 22,44". */
21240 need_clr_eos = needclr;
21241
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 {
21245 /*
21246 * Report the invalid expression unless the expression evaluation
21247 * has been cancelled due to an aborting error, an interrupt, or an
21248 * exception.
21249 */
21250 if (!aborting())
21251 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021252 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 break;
21254 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021255 need_clr_eos = FALSE;
21256
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 if (!eap->skip)
21258 {
21259 if (atstart)
21260 {
21261 atstart = FALSE;
21262 /* Call msg_start() after eval1(), evaluating the expression
21263 * may cause a message to appear. */
21264 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021265 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021266 /* Mark the saved text as finishing the line, so that what
21267 * follows is displayed on a new line when scrolling back
21268 * at the more prompt. */
21269 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 }
21273 else if (eap->cmdidx == CMD_echo)
21274 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021275 current_copyID += COPYID_INC;
21276 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021277 if (p != NULL)
21278 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021280 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021282 if (*p != TAB && needclr)
21283 {
21284 /* remove any text still there from the command */
21285 msg_clr_eos();
21286 needclr = FALSE;
21287 }
21288 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 }
21290 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021291 {
21292#ifdef FEAT_MBYTE
21293 if (has_mbyte)
21294 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021295 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021296
21297 (void)msg_outtrans_len_attr(p, i, echo_attr);
21298 p += i - 1;
21299 }
21300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021302 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021305 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021307 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 arg = skipwhite(arg);
21309 }
21310 eap->nextcmd = check_nextcmd(arg);
21311
21312 if (eap->skip)
21313 --emsg_skip;
21314 else
21315 {
21316 /* remove text that may still be there from the command */
21317 if (needclr)
21318 msg_clr_eos();
21319 if (eap->cmdidx == CMD_echo)
21320 msg_end();
21321 }
21322}
21323
21324/*
21325 * ":echohl {name}".
21326 */
21327 void
21328ex_echohl(eap)
21329 exarg_T *eap;
21330{
21331 int id;
21332
21333 id = syn_name2id(eap->arg);
21334 if (id == 0)
21335 echo_attr = 0;
21336 else
21337 echo_attr = syn_id2attr(id);
21338}
21339
21340/*
21341 * ":execute expr1 ..." execute the result of an expression.
21342 * ":echomsg expr1 ..." Print a message
21343 * ":echoerr expr1 ..." Print an error
21344 * Each gets spaces around each argument and a newline at the end for
21345 * echo commands
21346 */
21347 void
21348ex_execute(eap)
21349 exarg_T *eap;
21350{
21351 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 int ret = OK;
21354 char_u *p;
21355 garray_T ga;
21356 int len;
21357 int save_did_emsg;
21358
21359 ga_init2(&ga, 1, 80);
21360
21361 if (eap->skip)
21362 ++emsg_skip;
21363 while (*arg != NUL && *arg != '|' && *arg != '\n')
21364 {
21365 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021366 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 {
21368 /*
21369 * Report the invalid expression unless the expression evaluation
21370 * has been cancelled due to an aborting error, an interrupt, or an
21371 * exception.
21372 */
21373 if (!aborting())
21374 EMSG2(_(e_invexpr2), p);
21375 ret = FAIL;
21376 break;
21377 }
21378
21379 if (!eap->skip)
21380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 len = (int)STRLEN(p);
21383 if (ga_grow(&ga, len + 2) == FAIL)
21384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 ret = FAIL;
21387 break;
21388 }
21389 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 ga.ga_len += len;
21393 }
21394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021395 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 arg = skipwhite(arg);
21397 }
21398
21399 if (ret != FAIL && ga.ga_data != NULL)
21400 {
21401 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021404 out_flush();
21405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 else if (eap->cmdidx == CMD_echoerr)
21407 {
21408 /* We don't want to abort following commands, restore did_emsg. */
21409 save_did_emsg = did_emsg;
21410 EMSG((char_u *)ga.ga_data);
21411 if (!force_abort)
21412 did_emsg = save_did_emsg;
21413 }
21414 else if (eap->cmdidx == CMD_execute)
21415 do_cmdline((char_u *)ga.ga_data,
21416 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21417 }
21418
21419 ga_clear(&ga);
21420
21421 if (eap->skip)
21422 --emsg_skip;
21423
21424 eap->nextcmd = check_nextcmd(arg);
21425}
21426
21427/*
21428 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21429 * "arg" points to the "&" or '+' when called, to "option" when returning.
21430 * Returns NULL when no option name found. Otherwise pointer to the char
21431 * after the option name.
21432 */
21433 static char_u *
21434find_option_end(arg, opt_flags)
21435 char_u **arg;
21436 int *opt_flags;
21437{
21438 char_u *p = *arg;
21439
21440 ++p;
21441 if (*p == 'g' && p[1] == ':')
21442 {
21443 *opt_flags = OPT_GLOBAL;
21444 p += 2;
21445 }
21446 else if (*p == 'l' && p[1] == ':')
21447 {
21448 *opt_flags = OPT_LOCAL;
21449 p += 2;
21450 }
21451 else
21452 *opt_flags = 0;
21453
21454 if (!ASCII_ISALPHA(*p))
21455 return NULL;
21456 *arg = p;
21457
21458 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21459 p += 4; /* termcap option */
21460 else
21461 while (ASCII_ISALPHA(*p))
21462 ++p;
21463 return p;
21464}
21465
21466/*
21467 * ":function"
21468 */
21469 void
21470ex_function(eap)
21471 exarg_T *eap;
21472{
21473 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021474 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 int j;
21476 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021478 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 char_u *name = NULL;
21480 char_u *p;
21481 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021482 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 garray_T newargs;
21484 garray_T newlines;
21485 int varargs = FALSE;
21486 int mustend = FALSE;
21487 int flags = 0;
21488 ufunc_T *fp;
21489 int indent;
21490 int nesting;
21491 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021492 dictitem_T *v;
21493 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494 static int func_nr = 0; /* number for nameless function */
21495 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021496 hashtab_T *ht;
21497 int todo;
21498 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021499 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500
21501 /*
21502 * ":function" without argument: list functions.
21503 */
21504 if (ends_excmd(*eap->arg))
21505 {
21506 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021507 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021508 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021509 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 {
21511 if (!HASHITEM_EMPTY(hi))
21512 {
21513 --todo;
21514 fp = HI2UF(hi);
21515 if (!isdigit(*fp->uf_name))
21516 list_func_head(fp, FALSE);
21517 }
21518 }
21519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 eap->nextcmd = check_nextcmd(eap->arg);
21521 return;
21522 }
21523
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021524 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021525 * ":function /pat": list functions matching pattern.
21526 */
21527 if (*eap->arg == '/')
21528 {
21529 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21530 if (!eap->skip)
21531 {
21532 regmatch_T regmatch;
21533
21534 c = *p;
21535 *p = NUL;
21536 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21537 *p = c;
21538 if (regmatch.regprog != NULL)
21539 {
21540 regmatch.rm_ic = p_ic;
21541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021542 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021543 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21544 {
21545 if (!HASHITEM_EMPTY(hi))
21546 {
21547 --todo;
21548 fp = HI2UF(hi);
21549 if (!isdigit(*fp->uf_name)
21550 && vim_regexec(&regmatch, fp->uf_name, 0))
21551 list_func_head(fp, FALSE);
21552 }
21553 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021554 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021555 }
21556 }
21557 if (*p == '/')
21558 ++p;
21559 eap->nextcmd = check_nextcmd(p);
21560 return;
21561 }
21562
21563 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021564 * Get the function name. There are these situations:
21565 * func normal function name
21566 * "name" == func, "fudi.fd_dict" == NULL
21567 * dict.func new dictionary entry
21568 * "name" == NULL, "fudi.fd_dict" set,
21569 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21570 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021571 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021572 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21573 * dict.func existing dict entry that's not a Funcref
21574 * "name" == NULL, "fudi.fd_dict" set,
21575 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 name = trans_function_name(&p, eap->skip, 0, &fudi);
21579 paren = (vim_strchr(p, '(') != NULL);
21580 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 {
21582 /*
21583 * Return on an invalid expression in braces, unless the expression
21584 * evaluation has been cancelled due to an aborting error, an
21585 * interrupt, or an exception.
21586 */
21587 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021589 if (!eap->skip && fudi.fd_newkey != NULL)
21590 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 else
21595 eap->skip = TRUE;
21596 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021597
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 /* An error in a function call during evaluation of an expression in magic
21599 * braces should not cause the function not to be defined. */
21600 saved_did_emsg = did_emsg;
21601 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602
21603 /*
21604 * ":function func" with only function name: list function.
21605 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 {
21608 if (!ends_excmd(*skipwhite(p)))
21609 {
21610 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
21613 eap->nextcmd = check_nextcmd(p);
21614 if (eap->nextcmd != NULL)
21615 *p = NUL;
21616 if (!eap->skip && !got_int)
21617 {
21618 fp = find_func(name);
21619 if (fp != NULL)
21620 {
21621 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021622 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021624 if (FUNCLINE(fp, j) == NULL)
21625 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 msg_putchar('\n');
21627 msg_outnum((long)(j + 1));
21628 if (j < 9)
21629 msg_putchar(' ');
21630 if (j < 99)
21631 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021632 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 out_flush(); /* show a line at a time */
21634 ui_breakcheck();
21635 }
21636 if (!got_int)
21637 {
21638 msg_putchar('\n');
21639 msg_puts((char_u *)" endfunction");
21640 }
21641 }
21642 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021643 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 }
21647
21648 /*
21649 * ":function name(arg1, arg2)" Define function.
21650 */
21651 p = skipwhite(p);
21652 if (*p != '(')
21653 {
21654 if (!eap->skip)
21655 {
21656 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 }
21659 /* attempt to continue by skipping some text */
21660 if (vim_strchr(p, '(') != NULL)
21661 p = vim_strchr(p, '(');
21662 }
21663 p = skipwhite(p + 1);
21664
21665 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21666 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21667
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021668 if (!eap->skip)
21669 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021670 /* Check the name of the function. Unless it's a dictionary function
21671 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021672 if (name != NULL)
21673 arg = name;
21674 else
21675 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021676 if (arg != NULL && (fudi.fd_di == NULL
21677 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021678 {
21679 if (*arg == K_SPECIAL)
21680 j = 3;
21681 else
21682 j = 0;
21683 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21684 : eval_isnamec(arg[j])))
21685 ++j;
21686 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021687 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021688 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021689 /* Disallow using the g: dict. */
21690 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21691 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021692 }
21693
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 /*
21695 * Isolate the arguments: "arg1, arg2, ...)"
21696 */
21697 while (*p != ')')
21698 {
21699 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21700 {
21701 varargs = TRUE;
21702 p += 3;
21703 mustend = TRUE;
21704 }
21705 else
21706 {
21707 arg = p;
21708 while (ASCII_ISALNUM(*p) || *p == '_')
21709 ++p;
21710 if (arg == p || isdigit(*arg)
21711 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21712 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21713 {
21714 if (!eap->skip)
21715 EMSG2(_("E125: Illegal argument: %s"), arg);
21716 break;
21717 }
21718 if (ga_grow(&newargs, 1) == FAIL)
21719 goto erret;
21720 c = *p;
21721 *p = NUL;
21722 arg = vim_strsave(arg);
21723 if (arg == NULL)
21724 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021725
21726 /* Check for duplicate argument name. */
21727 for (i = 0; i < newargs.ga_len; ++i)
21728 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21729 {
21730 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021731 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021732 goto erret;
21733 }
21734
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21736 *p = c;
21737 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 if (*p == ',')
21739 ++p;
21740 else
21741 mustend = TRUE;
21742 }
21743 p = skipwhite(p);
21744 if (mustend && *p != ')')
21745 {
21746 if (!eap->skip)
21747 EMSG2(_(e_invarg2), eap->arg);
21748 break;
21749 }
21750 }
21751 ++p; /* skip the ')' */
21752
Bram Moolenaare9a41262005-01-15 22:18:47 +000021753 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 for (;;)
21755 {
21756 p = skipwhite(p);
21757 if (STRNCMP(p, "range", 5) == 0)
21758 {
21759 flags |= FC_RANGE;
21760 p += 5;
21761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021762 else if (STRNCMP(p, "dict", 4) == 0)
21763 {
21764 flags |= FC_DICT;
21765 p += 4;
21766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 else if (STRNCMP(p, "abort", 5) == 0)
21768 {
21769 flags |= FC_ABORT;
21770 p += 5;
21771 }
21772 else
21773 break;
21774 }
21775
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021776 /* When there is a line break use what follows for the function body.
21777 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21778 if (*p == '\n')
21779 line_arg = p + 1;
21780 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 EMSG(_(e_trailing));
21782
21783 /*
21784 * Read the body of the function, until ":endfunction" is found.
21785 */
21786 if (KeyTyped)
21787 {
21788 /* Check if the function already exists, don't let the user type the
21789 * whole function before telling him it doesn't work! For a script we
21790 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 if (!eap->skip && !eap->forceit)
21792 {
21793 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21794 EMSG(_(e_funcdict));
21795 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021796 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021799 if (!eap->skip && did_emsg)
21800 goto erret;
21801
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 msg_putchar('\n'); /* don't overwrite the function name */
21803 cmdline_row = msg_row;
21804 }
21805
21806 indent = 2;
21807 nesting = 0;
21808 for (;;)
21809 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021810 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021811 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021812 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021813 saved_wait_return = FALSE;
21814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021816 sourcing_lnum_off = sourcing_lnum;
21817
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021818 if (line_arg != NULL)
21819 {
21820 /* Use eap->arg, split up in parts by line breaks. */
21821 theline = line_arg;
21822 p = vim_strchr(theline, '\n');
21823 if (p == NULL)
21824 line_arg += STRLEN(line_arg);
21825 else
21826 {
21827 *p = NUL;
21828 line_arg = p + 1;
21829 }
21830 }
21831 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 theline = getcmdline(':', 0L, indent);
21833 else
21834 theline = eap->getline(':', eap->cookie, indent);
21835 if (KeyTyped)
21836 lines_left = Rows - 1;
21837 if (theline == NULL)
21838 {
21839 EMSG(_("E126: Missing :endfunction"));
21840 goto erret;
21841 }
21842
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021843 /* Detect line continuation: sourcing_lnum increased more than one. */
21844 if (sourcing_lnum > sourcing_lnum_off + 1)
21845 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21846 else
21847 sourcing_lnum_off = 0;
21848
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 if (skip_until != NULL)
21850 {
21851 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21852 * don't check for ":endfunc". */
21853 if (STRCMP(theline, skip_until) == 0)
21854 {
21855 vim_free(skip_until);
21856 skip_until = NULL;
21857 }
21858 }
21859 else
21860 {
21861 /* skip ':' and blanks*/
21862 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21863 ;
21864
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021865 /* Check for "endfunction". */
21866 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021868 if (line_arg == NULL)
21869 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 break;
21871 }
21872
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021873 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 * at "end". */
21875 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21876 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021877 else if (STRNCMP(p, "if", 2) == 0
21878 || STRNCMP(p, "wh", 2) == 0
21879 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 || STRNCMP(p, "try", 3) == 0)
21881 indent += 2;
21882
21883 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021884 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021886 if (*p == '!')
21887 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 p += eval_fname_script(p);
21889 if (ASCII_ISALPHA(*p))
21890 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021891 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 if (*skipwhite(p) == '(')
21893 {
21894 ++nesting;
21895 indent += 2;
21896 }
21897 }
21898 }
21899
21900 /* Check for ":append" or ":insert". */
21901 p = skip_range(p, NULL);
21902 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21903 || (p[0] == 'i'
21904 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21905 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21906 skip_until = vim_strsave((char_u *)".");
21907
21908 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21909 arg = skipwhite(skiptowhite(p));
21910 if (arg[0] == '<' && arg[1] =='<'
21911 && ((p[0] == 'p' && p[1] == 'y'
21912 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21913 || (p[0] == 'p' && p[1] == 'e'
21914 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21915 || (p[0] == 't' && p[1] == 'c'
21916 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021917 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21918 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21920 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021921 || (p[0] == 'm' && p[1] == 'z'
21922 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 ))
21924 {
21925 /* ":python <<" continues until a dot, like ":append" */
21926 p = skipwhite(arg + 2);
21927 if (*p == NUL)
21928 skip_until = vim_strsave((char_u *)".");
21929 else
21930 skip_until = vim_strsave(p);
21931 }
21932 }
21933
21934 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021935 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021936 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021937 if (line_arg == NULL)
21938 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021940 }
21941
21942 /* Copy the line to newly allocated memory. get_one_sourceline()
21943 * allocates 250 bytes per line, this saves 80% on average. The cost
21944 * is an extra alloc/free. */
21945 p = vim_strsave(theline);
21946 if (p != NULL)
21947 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021948 if (line_arg == NULL)
21949 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021950 theline = p;
21951 }
21952
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021953 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21954
21955 /* Add NULL lines for continuation lines, so that the line count is
21956 * equal to the index in the growarray. */
21957 while (sourcing_lnum_off-- > 0)
21958 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021959
21960 /* Check for end of eap->arg. */
21961 if (line_arg != NULL && *line_arg == NUL)
21962 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 }
21964
21965 /* Don't define the function when skipping commands or when an error was
21966 * detected. */
21967 if (eap->skip || did_emsg)
21968 goto erret;
21969
21970 /*
21971 * If there are no errors, add the function
21972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021973 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021974 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021975 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021976 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021977 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021978 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021979 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021980 goto erret;
21981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021983 fp = find_func(name);
21984 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021986 if (!eap->forceit)
21987 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021988 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021989 goto erret;
21990 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021991 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021992 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021993 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021994 name);
21995 goto erret;
21996 }
21997 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021998 ga_clear_strings(&(fp->uf_args));
21999 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022000 vim_free(name);
22001 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 }
22004 else
22005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022006 char numbuf[20];
22007
22008 fp = NULL;
22009 if (fudi.fd_newkey == NULL && !eap->forceit)
22010 {
22011 EMSG(_(e_funcdict));
22012 goto erret;
22013 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022014 if (fudi.fd_di == NULL)
22015 {
22016 /* Can't add a function to a locked dictionary */
22017 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22018 goto erret;
22019 }
22020 /* Can't change an existing function if it is locked */
22021 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22022 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022023
22024 /* Give the function a sequential number. Can only be used with a
22025 * Funcref! */
22026 vim_free(name);
22027 sprintf(numbuf, "%d", ++func_nr);
22028 name = vim_strsave((char_u *)numbuf);
22029 if (name == NULL)
22030 goto erret;
22031 }
22032
22033 if (fp == NULL)
22034 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022035 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022036 {
22037 int slen, plen;
22038 char_u *scriptname;
22039
22040 /* Check that the autoload name matches the script name. */
22041 j = FAIL;
22042 if (sourcing_name != NULL)
22043 {
22044 scriptname = autoload_name(name);
22045 if (scriptname != NULL)
22046 {
22047 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022048 plen = (int)STRLEN(p);
22049 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022050 if (slen > plen && fnamecmp(p,
22051 sourcing_name + slen - plen) == 0)
22052 j = OK;
22053 vim_free(scriptname);
22054 }
22055 }
22056 if (j == FAIL)
22057 {
22058 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22059 goto erret;
22060 }
22061 }
22062
22063 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 if (fp == NULL)
22065 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022066
22067 if (fudi.fd_dict != NULL)
22068 {
22069 if (fudi.fd_di == NULL)
22070 {
22071 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022072 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022073 if (fudi.fd_di == NULL)
22074 {
22075 vim_free(fp);
22076 goto erret;
22077 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022078 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22079 {
22080 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022081 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022082 goto erret;
22083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022084 }
22085 else
22086 /* overwrite existing dict entry */
22087 clear_tv(&fudi.fd_di->di_tv);
22088 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022089 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022090 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022091 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022092
22093 /* behave like "dict" was used */
22094 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022095 }
22096
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022098 STRCPY(fp->uf_name, name);
22099 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022101 fp->uf_args = newargs;
22102 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022103#ifdef FEAT_PROFILE
22104 fp->uf_tml_count = NULL;
22105 fp->uf_tml_total = NULL;
22106 fp->uf_tml_self = NULL;
22107 fp->uf_profiling = FALSE;
22108 if (prof_def_func())
22109 func_do_profile(fp);
22110#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022111 fp->uf_varargs = varargs;
22112 fp->uf_flags = flags;
22113 fp->uf_calls = 0;
22114 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022115 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116
22117erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 ga_clear_strings(&newargs);
22119 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022120ret_free:
22121 vim_free(skip_until);
22122 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022125 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126}
22127
22128/*
22129 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022130 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022132 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022133 * TFN_INT: internal function name OK
22134 * TFN_QUIET: be quiet
22135 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 * Advances "pp" to just after the function name (if no error).
22137 */
22138 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022139trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 char_u **pp;
22141 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022142 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022145 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 char_u *start;
22147 char_u *end;
22148 int lead;
22149 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022152
22153 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022156
22157 /* Check for hard coded <SNR>: already translated function ID (from a user
22158 * command). */
22159 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22160 && (*pp)[2] == (int)KE_SNR)
22161 {
22162 *pp += 3;
22163 len = get_id_len(pp) + 3;
22164 return vim_strnsave(start, len);
22165 }
22166
22167 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22168 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022170 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022172
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022173 /* Note that TFN_ flags use the same values as GLV_ flags. */
22174 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022175 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022176 if (end == start)
22177 {
22178 if (!skip)
22179 EMSG(_("E129: Function name required"));
22180 goto theend;
22181 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022182 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022183 {
22184 /*
22185 * Report an invalid expression in braces, unless the expression
22186 * evaluation has been cancelled due to an aborting error, an
22187 * interrupt, or an exception.
22188 */
22189 if (!aborting())
22190 {
22191 if (end != NULL)
22192 EMSG2(_(e_invarg2), start);
22193 }
22194 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022195 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022196 goto theend;
22197 }
22198
22199 if (lv.ll_tv != NULL)
22200 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022201 if (fdp != NULL)
22202 {
22203 fdp->fd_dict = lv.ll_dict;
22204 fdp->fd_newkey = lv.ll_newkey;
22205 lv.ll_newkey = NULL;
22206 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022207 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022208 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22209 {
22210 name = vim_strsave(lv.ll_tv->vval.v_string);
22211 *pp = end;
22212 }
22213 else
22214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022215 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22216 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022217 EMSG(_(e_funcref));
22218 else
22219 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022220 name = NULL;
22221 }
22222 goto theend;
22223 }
22224
22225 if (lv.ll_name == NULL)
22226 {
22227 /* Error found, but continue after the function name. */
22228 *pp = end;
22229 goto theend;
22230 }
22231
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022232 /* Check if the name is a Funcref. If so, use the value. */
22233 if (lv.ll_exp_name != NULL)
22234 {
22235 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022236 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022237 if (name == lv.ll_exp_name)
22238 name = NULL;
22239 }
22240 else
22241 {
22242 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022243 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022244 if (name == *pp)
22245 name = NULL;
22246 }
22247 if (name != NULL)
22248 {
22249 name = vim_strsave(name);
22250 *pp = end;
22251 goto theend;
22252 }
22253
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022254 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022255 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022256 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022257 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22258 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22259 {
22260 /* When there was "s:" already or the name expanded to get a
22261 * leading "s:" then remove it. */
22262 lv.ll_name += 2;
22263 len -= 2;
22264 lead = 2;
22265 }
22266 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022267 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022268 {
22269 if (lead == 2) /* skip over "s:" */
22270 lv.ll_name += 2;
22271 len = (int)(end - lv.ll_name);
22272 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022273
22274 /*
22275 * Copy the function name to allocated memory.
22276 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22277 * Accept <SNR>123_name() outside a script.
22278 */
22279 if (skip)
22280 lead = 0; /* do nothing */
22281 else if (lead > 0)
22282 {
22283 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022284 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22285 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022286 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022287 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022288 if (current_SID <= 0)
22289 {
22290 EMSG(_(e_usingsid));
22291 goto theend;
22292 }
22293 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22294 lead += (int)STRLEN(sid_buf);
22295 }
22296 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022297 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022298 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022299 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022300 goto theend;
22301 }
22302 name = alloc((unsigned)(len + lead + 1));
22303 if (name != NULL)
22304 {
22305 if (lead > 0)
22306 {
22307 name[0] = K_SPECIAL;
22308 name[1] = KS_EXTRA;
22309 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022310 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022311 STRCPY(name + 3, sid_buf);
22312 }
22313 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22314 name[len + lead] = NUL;
22315 }
22316 *pp = end;
22317
22318theend:
22319 clear_lval(&lv);
22320 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321}
22322
22323/*
22324 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22325 * Return 2 if "p" starts with "s:".
22326 * Return 0 otherwise.
22327 */
22328 static int
22329eval_fname_script(p)
22330 char_u *p;
22331{
22332 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22333 || STRNICMP(p + 1, "SNR>", 4) == 0))
22334 return 5;
22335 if (p[0] == 's' && p[1] == ':')
22336 return 2;
22337 return 0;
22338}
22339
22340/*
22341 * Return TRUE if "p" starts with "<SID>" or "s:".
22342 * Only works if eval_fname_script() returned non-zero for "p"!
22343 */
22344 static int
22345eval_fname_sid(p)
22346 char_u *p;
22347{
22348 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22349}
22350
22351/*
22352 * List the head of the function: "name(arg1, arg2)".
22353 */
22354 static void
22355list_func_head(fp, indent)
22356 ufunc_T *fp;
22357 int indent;
22358{
22359 int j;
22360
22361 msg_start();
22362 if (indent)
22363 MSG_PUTS(" ");
22364 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022365 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 {
22367 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022368 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022371 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022373 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 {
22375 if (j)
22376 MSG_PUTS(", ");
22377 msg_puts(FUNCARG(fp, j));
22378 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022379 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 {
22381 if (j)
22382 MSG_PUTS(", ");
22383 MSG_PUTS("...");
22384 }
22385 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022386 if (fp->uf_flags & FC_ABORT)
22387 MSG_PUTS(" abort");
22388 if (fp->uf_flags & FC_RANGE)
22389 MSG_PUTS(" range");
22390 if (fp->uf_flags & FC_DICT)
22391 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022392 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022393 if (p_verbose > 0)
22394 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395}
22396
22397/*
22398 * Find a function by name, return pointer to it in ufuncs.
22399 * Return NULL for unknown function.
22400 */
22401 static ufunc_T *
22402find_func(name)
22403 char_u *name;
22404{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022405 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022407 hi = hash_find(&func_hashtab, name);
22408 if (!HASHITEM_EMPTY(hi))
22409 return HI2UF(hi);
22410 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411}
22412
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022413#if defined(EXITFREE) || defined(PROTO)
22414 void
22415free_all_functions()
22416{
22417 hashitem_T *hi;
22418
22419 /* Need to start all over every time, because func_free() may change the
22420 * hash table. */
22421 while (func_hashtab.ht_used > 0)
22422 for (hi = func_hashtab.ht_array; ; ++hi)
22423 if (!HASHITEM_EMPTY(hi))
22424 {
22425 func_free(HI2UF(hi));
22426 break;
22427 }
22428}
22429#endif
22430
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022431 int
22432translated_function_exists(name)
22433 char_u *name;
22434{
22435 if (builtin_function(name))
22436 return find_internal_func(name) >= 0;
22437 return find_func(name) != NULL;
22438}
22439
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022440/*
22441 * Return TRUE if a function "name" exists.
22442 */
22443 static int
22444function_exists(name)
22445 char_u *name;
22446{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022447 char_u *nm = name;
22448 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022449 int n = FALSE;
22450
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022451 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22452 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022453 nm = skipwhite(nm);
22454
22455 /* Only accept "funcname", "funcname ", "funcname (..." and
22456 * "funcname(...", not "funcname!...". */
22457 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022458 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022459 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460 return n;
22461}
22462
Bram Moolenaara1544c02013-05-30 12:35:52 +020022463 char_u *
22464get_expanded_name(name, check)
22465 char_u *name;
22466 int check;
22467{
22468 char_u *nm = name;
22469 char_u *p;
22470
22471 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22472
22473 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022474 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022475 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022476
Bram Moolenaara1544c02013-05-30 12:35:52 +020022477 vim_free(p);
22478 return NULL;
22479}
22480
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022481/*
22482 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022483 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022484 */
22485 static int
22486builtin_function(name)
22487 char_u *name;
22488{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022489 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22490 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022491}
22492
Bram Moolenaar05159a02005-02-26 23:04:13 +000022493#if defined(FEAT_PROFILE) || defined(PROTO)
22494/*
22495 * Start profiling function "fp".
22496 */
22497 static void
22498func_do_profile(fp)
22499 ufunc_T *fp;
22500{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022501 int len = fp->uf_lines.ga_len;
22502
22503 if (len == 0)
22504 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022505 fp->uf_tm_count = 0;
22506 profile_zero(&fp->uf_tm_self);
22507 profile_zero(&fp->uf_tm_total);
22508 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022509 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022510 if (fp->uf_tml_total == NULL)
22511 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022512 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022513 if (fp->uf_tml_self == NULL)
22514 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022515 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022516 fp->uf_tml_idx = -1;
22517 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22518 || fp->uf_tml_self == NULL)
22519 return; /* out of memory */
22520
22521 fp->uf_profiling = TRUE;
22522}
22523
22524/*
22525 * Dump the profiling results for all functions in file "fd".
22526 */
22527 void
22528func_dump_profile(fd)
22529 FILE *fd;
22530{
22531 hashitem_T *hi;
22532 int todo;
22533 ufunc_T *fp;
22534 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022535 ufunc_T **sorttab;
22536 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022537
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022538 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022539 if (todo == 0)
22540 return; /* nothing to dump */
22541
Bram Moolenaar73830342005-02-28 22:48:19 +000022542 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22543
Bram Moolenaar05159a02005-02-26 23:04:13 +000022544 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22545 {
22546 if (!HASHITEM_EMPTY(hi))
22547 {
22548 --todo;
22549 fp = HI2UF(hi);
22550 if (fp->uf_profiling)
22551 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022552 if (sorttab != NULL)
22553 sorttab[st_len++] = fp;
22554
Bram Moolenaar05159a02005-02-26 23:04:13 +000022555 if (fp->uf_name[0] == K_SPECIAL)
22556 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22557 else
22558 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22559 if (fp->uf_tm_count == 1)
22560 fprintf(fd, "Called 1 time\n");
22561 else
22562 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22563 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22564 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22565 fprintf(fd, "\n");
22566 fprintf(fd, "count total (s) self (s)\n");
22567
22568 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22569 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022570 if (FUNCLINE(fp, i) == NULL)
22571 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022572 prof_func_line(fd, fp->uf_tml_count[i],
22573 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022574 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22575 }
22576 fprintf(fd, "\n");
22577 }
22578 }
22579 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022580
22581 if (sorttab != NULL && st_len > 0)
22582 {
22583 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22584 prof_total_cmp);
22585 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22586 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22587 prof_self_cmp);
22588 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22589 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022590
22591 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592}
Bram Moolenaar73830342005-02-28 22:48:19 +000022593
22594 static void
22595prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22596 FILE *fd;
22597 ufunc_T **sorttab;
22598 int st_len;
22599 char *title;
22600 int prefer_self; /* when equal print only self time */
22601{
22602 int i;
22603 ufunc_T *fp;
22604
22605 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22606 fprintf(fd, "count total (s) self (s) function\n");
22607 for (i = 0; i < 20 && i < st_len; ++i)
22608 {
22609 fp = sorttab[i];
22610 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22611 prefer_self);
22612 if (fp->uf_name[0] == K_SPECIAL)
22613 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22614 else
22615 fprintf(fd, " %s()\n", fp->uf_name);
22616 }
22617 fprintf(fd, "\n");
22618}
22619
22620/*
22621 * Print the count and times for one function or function line.
22622 */
22623 static void
22624prof_func_line(fd, count, total, self, prefer_self)
22625 FILE *fd;
22626 int count;
22627 proftime_T *total;
22628 proftime_T *self;
22629 int prefer_self; /* when equal print only self time */
22630{
22631 if (count > 0)
22632 {
22633 fprintf(fd, "%5d ", count);
22634 if (prefer_self && profile_equal(total, self))
22635 fprintf(fd, " ");
22636 else
22637 fprintf(fd, "%s ", profile_msg(total));
22638 if (!prefer_self && profile_equal(total, self))
22639 fprintf(fd, " ");
22640 else
22641 fprintf(fd, "%s ", profile_msg(self));
22642 }
22643 else
22644 fprintf(fd, " ");
22645}
22646
22647/*
22648 * Compare function for total time sorting.
22649 */
22650 static int
22651#ifdef __BORLANDC__
22652_RTLENTRYF
22653#endif
22654prof_total_cmp(s1, s2)
22655 const void *s1;
22656 const void *s2;
22657{
22658 ufunc_T *p1, *p2;
22659
22660 p1 = *(ufunc_T **)s1;
22661 p2 = *(ufunc_T **)s2;
22662 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22663}
22664
22665/*
22666 * Compare function for self time sorting.
22667 */
22668 static int
22669#ifdef __BORLANDC__
22670_RTLENTRYF
22671#endif
22672prof_self_cmp(s1, s2)
22673 const void *s1;
22674 const void *s2;
22675{
22676 ufunc_T *p1, *p2;
22677
22678 p1 = *(ufunc_T **)s1;
22679 p2 = *(ufunc_T **)s2;
22680 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22681}
22682
Bram Moolenaar05159a02005-02-26 23:04:13 +000022683#endif
22684
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022685/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022686 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022687 * Return TRUE if a package was loaded.
22688 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022689 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022690script_autoload(name, reload)
22691 char_u *name;
22692 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022693{
22694 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022695 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022696 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022697 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022698
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022699 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022700 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022701 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022702 return FALSE;
22703
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022704 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022705
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022706 /* Find the name in the list of previously loaded package names. Skip
22707 * "autoload/", it's always the same. */
22708 for (i = 0; i < ga_loaded.ga_len; ++i)
22709 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22710 break;
22711 if (!reload && i < ga_loaded.ga_len)
22712 ret = FALSE; /* was loaded already */
22713 else
22714 {
22715 /* Remember the name if it wasn't loaded already. */
22716 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22717 {
22718 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22719 tofree = NULL;
22720 }
22721
22722 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022723 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022724 ret = TRUE;
22725 }
22726
22727 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022728 return ret;
22729}
22730
22731/*
22732 * Return the autoload script name for a function or variable name.
22733 * Returns NULL when out of memory.
22734 */
22735 static char_u *
22736autoload_name(name)
22737 char_u *name;
22738{
22739 char_u *p;
22740 char_u *scriptname;
22741
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022742 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022743 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22744 if (scriptname == NULL)
22745 return FALSE;
22746 STRCPY(scriptname, "autoload/");
22747 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022748 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022749 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022750 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022751 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022752 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022753}
22754
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22756
22757/*
22758 * Function given to ExpandGeneric() to obtain the list of user defined
22759 * function names.
22760 */
22761 char_u *
22762get_user_func_name(xp, idx)
22763 expand_T *xp;
22764 int idx;
22765{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022766 static long_u done;
22767 static hashitem_T *hi;
22768 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769
22770 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022772 done = 0;
22773 hi = func_hashtab.ht_array;
22774 }
22775 if (done < func_hashtab.ht_used)
22776 {
22777 if (done++ > 0)
22778 ++hi;
22779 while (HASHITEM_EMPTY(hi))
22780 ++hi;
22781 fp = HI2UF(hi);
22782
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022783 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022784 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022785
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22787 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788
22789 cat_func_name(IObuff, fp);
22790 if (xp->xp_context != EXPAND_USER_FUNC)
22791 {
22792 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022793 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 STRCAT(IObuff, ")");
22795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 return IObuff;
22797 }
22798 return NULL;
22799}
22800
22801#endif /* FEAT_CMDL_COMPL */
22802
22803/*
22804 * Copy the function name of "fp" to buffer "buf".
22805 * "buf" must be able to hold the function name plus three bytes.
22806 * Takes care of script-local function names.
22807 */
22808 static void
22809cat_func_name(buf, fp)
22810 char_u *buf;
22811 ufunc_T *fp;
22812{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 {
22815 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022816 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 }
22818 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022819 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820}
22821
22822/*
22823 * ":delfunction {name}"
22824 */
22825 void
22826ex_delfunction(eap)
22827 exarg_T *eap;
22828{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 char_u *p;
22831 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833
22834 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022835 name = trans_function_name(&p, eap->skip, 0, &fudi);
22836 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022838 {
22839 if (fudi.fd_dict != NULL && !eap->skip)
22840 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 if (!ends_excmd(*skipwhite(p)))
22844 {
22845 vim_free(name);
22846 EMSG(_(e_trailing));
22847 return;
22848 }
22849 eap->nextcmd = check_nextcmd(p);
22850 if (eap->nextcmd != NULL)
22851 *p = NUL;
22852
22853 if (!eap->skip)
22854 fp = find_func(name);
22855 vim_free(name);
22856
22857 if (!eap->skip)
22858 {
22859 if (fp == NULL)
22860 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022861 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 return;
22863 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022864 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 {
22866 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22867 return;
22868 }
22869
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022870 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022872 /* Delete the dict item that refers to the function, it will
22873 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022874 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022876 else
22877 func_free(fp);
22878 }
22879}
22880
22881/*
22882 * Free a function and remove it from the list of functions.
22883 */
22884 static void
22885func_free(fp)
22886 ufunc_T *fp;
22887{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022888 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889
22890 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022891 ga_clear_strings(&(fp->uf_args));
22892 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022893#ifdef FEAT_PROFILE
22894 vim_free(fp->uf_tml_count);
22895 vim_free(fp->uf_tml_total);
22896 vim_free(fp->uf_tml_self);
22897#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022899 /* remove the function from the function hashtable */
22900 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22901 if (HASHITEM_EMPTY(hi))
22902 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022903 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022904 hash_remove(&func_hashtab, hi);
22905
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022906 vim_free(fp);
22907}
22908
22909/*
22910 * Unreference a Function: decrement the reference count and free it when it
22911 * becomes zero. Only for numbered functions.
22912 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022913 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022914func_unref(name)
22915 char_u *name;
22916{
22917 ufunc_T *fp;
22918
22919 if (name != NULL && isdigit(*name))
22920 {
22921 fp = find_func(name);
22922 if (fp == NULL)
22923 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022924 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022925 {
22926 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022927 * when "uf_calls" becomes zero. */
22928 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022929 func_free(fp);
22930 }
22931 }
22932}
22933
22934/*
22935 * Count a reference to a Function.
22936 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022937 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022938func_ref(name)
22939 char_u *name;
22940{
22941 ufunc_T *fp;
22942
22943 if (name != NULL && isdigit(*name))
22944 {
22945 fp = find_func(name);
22946 if (fp == NULL)
22947 EMSG2(_(e_intern2), "func_ref()");
22948 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022949 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 }
22951}
22952
22953/*
22954 * Call a user function.
22955 */
22956 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022957call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 ufunc_T *fp; /* pointer to function */
22959 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022960 typval_T *argvars; /* arguments */
22961 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 linenr_T firstline; /* first line of range */
22963 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965{
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 char_u *save_sourcing_name;
22967 linenr_T save_sourcing_lnum;
22968 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022969 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 int save_did_emsg;
22971 static int depth = 0;
22972 dictitem_T *v;
22973 int fixvar_idx = 0; /* index in fixvar[] */
22974 int i;
22975 int ai;
22976 char_u numbuf[NUMBUFLEN];
22977 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022978#ifdef FEAT_PROFILE
22979 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022980 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982
22983 /* If depth of calling is getting too high, don't execute the function */
22984 if (depth >= p_mfd)
22985 {
22986 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022987 rettv->v_type = VAR_NUMBER;
22988 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 return;
22990 }
22991 ++depth;
22992
22993 line_breakcheck(); /* check for CTRL-C hit */
22994
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022995 fc = (funccall_T *)alloc(sizeof(funccall_T));
22996 fc->caller = current_funccal;
22997 current_funccal = fc;
22998 fc->func = fp;
22999 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023000 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023001 fc->linenr = 0;
23002 fc->returned = FALSE;
23003 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023005 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23006 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007
Bram Moolenaar33570922005-01-25 22:26:29 +000023008 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023009 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023010 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23011 * each argument variable and saves a lot of time.
23012 */
23013 /*
23014 * Init l: variables.
23015 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023016 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023017 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023018 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023019 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23020 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023021 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023022 name = v->di_key;
23023 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023025 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023026 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023027 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023028 v->di_tv.vval.v_dict = selfdict;
23029 ++selfdict->dv_refcount;
23030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023031
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 /*
23033 * Init a: variables.
23034 * Set a:0 to "argcount".
23035 * Set a:000 to a list with room for the "..." arguments.
23036 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023037 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023038 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023039 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023040 /* Use "name" to avoid a warning from some compiler that checks the
23041 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023042 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023043 name = v->di_key;
23044 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023045 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023046 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023048 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023049 v->di_tv.vval.v_list = &fc->l_varlist;
23050 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23051 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23052 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023053
23054 /*
23055 * Set a:firstline to "firstline" and a:lastline to "lastline".
23056 * Set a:name to named arguments.
23057 * Set a:N to the "..." arguments.
23058 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023059 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023061 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 (varnumber_T)lastline);
23063 for (i = 0; i < argcount; ++i)
23064 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023065 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023066 if (ai < 0)
23067 /* named argument a:name */
23068 name = FUNCARG(fp, i);
23069 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023070 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 /* "..." argument a:1, a:2, etc. */
23072 sprintf((char *)numbuf, "%d", ai + 1);
23073 name = numbuf;
23074 }
23075 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23076 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023077 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023078 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23079 }
23080 else
23081 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023082 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23083 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 if (v == NULL)
23085 break;
23086 v->di_flags = DI_FLAGS_RO;
23087 }
23088 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023089 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023090
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023091 /* Note: the values are copied directly to avoid alloc/free.
23092 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023093 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023094 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023095
23096 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23097 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023098 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23099 fc->l_listitems[ai].li_tv = argvars[i];
23100 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023101 }
23102 }
23103
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 /* Don't redraw while executing the function. */
23105 ++RedrawingDisabled;
23106 save_sourcing_name = sourcing_name;
23107 save_sourcing_lnum = sourcing_lnum;
23108 sourcing_lnum = 1;
23109 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023110 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 if (sourcing_name != NULL)
23112 {
23113 if (save_sourcing_name != NULL
23114 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23115 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23116 else
23117 STRCPY(sourcing_name, "function ");
23118 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23119
23120 if (p_verbose >= 12)
23121 {
23122 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023123 verbose_enter_scroll();
23124
Bram Moolenaar555b2802005-05-19 21:08:39 +000023125 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 if (p_verbose >= 14)
23127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023129 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023130 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023131 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132
23133 msg_puts((char_u *)"(");
23134 for (i = 0; i < argcount; ++i)
23135 {
23136 if (i > 0)
23137 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023138 if (argvars[i].v_type == VAR_NUMBER)
23139 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 else
23141 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023142 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23143 if (s != NULL)
23144 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023145 if (vim_strsize(s) > MSG_BUF_CLEN)
23146 {
23147 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23148 s = buf;
23149 }
23150 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023151 vim_free(tofree);
23152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 }
23154 }
23155 msg_puts((char_u *)")");
23156 }
23157 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023158
23159 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 --no_wait_return;
23161 }
23162 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023163#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023164 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023165 {
23166 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23167 func_do_profile(fp);
23168 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023169 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023170 {
23171 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023172 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023173 profile_zero(&fp->uf_tm_children);
23174 }
23175 script_prof_save(&wait_start);
23176 }
23177#endif
23178
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023180 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 save_did_emsg = did_emsg;
23182 did_emsg = FALSE;
23183
23184 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023185 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23187
23188 --RedrawingDisabled;
23189
23190 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023191 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023193 clear_tv(rettv);
23194 rettv->v_type = VAR_NUMBER;
23195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 }
23197
Bram Moolenaar05159a02005-02-26 23:04:13 +000023198#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023199 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023200 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023201 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023202 profile_end(&call_start);
23203 profile_sub_wait(&wait_start, &call_start);
23204 profile_add(&fp->uf_tm_total, &call_start);
23205 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023206 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023207 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023208 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23209 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023210 }
23211 }
23212#endif
23213
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 /* when being verbose, mention the return value */
23215 if (p_verbose >= 12)
23216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023218 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023221 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023222 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023223 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023224 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023227 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023228 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023229 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023230 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023231
Bram Moolenaar555b2802005-05-19 21:08:39 +000023232 /* The value may be very long. Skip the middle part, so that we
23233 * have some idea how it starts and ends. smsg() would always
23234 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023235 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023236 if (s != NULL)
23237 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023238 if (vim_strsize(s) > MSG_BUF_CLEN)
23239 {
23240 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23241 s = buf;
23242 }
23243 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023244 vim_free(tofree);
23245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 }
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
23253 vim_free(sourcing_name);
23254 sourcing_name = save_sourcing_name;
23255 sourcing_lnum = save_sourcing_lnum;
23256 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023257#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023258 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259 script_prof_restore(&wait_start);
23260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261
23262 if (p_verbose >= 12 && sourcing_name != NULL)
23263 {
23264 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023265 verbose_enter_scroll();
23266
Bram Moolenaar555b2802005-05-19 21:08:39 +000023267 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023269
23270 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 --no_wait_return;
23272 }
23273
23274 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023275 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023277
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023278 /* If the a:000 list and the l: and a: dicts are not referenced we can
23279 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023280 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23281 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23282 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23283 {
23284 free_funccal(fc, FALSE);
23285 }
23286 else
23287 {
23288 hashitem_T *hi;
23289 listitem_T *li;
23290 int todo;
23291
23292 /* "fc" is still in use. This can happen when returning "a:000" or
23293 * assigning "l:" to a global variable.
23294 * Link "fc" in the list for garbage collection later. */
23295 fc->caller = previous_funccal;
23296 previous_funccal = fc;
23297
23298 /* Make a copy of the a: variables, since we didn't do that above. */
23299 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23300 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23301 {
23302 if (!HASHITEM_EMPTY(hi))
23303 {
23304 --todo;
23305 v = HI2DI(hi);
23306 copy_tv(&v->di_tv, &v->di_tv);
23307 }
23308 }
23309
23310 /* Make a copy of the a:000 items, since we didn't do that above. */
23311 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23312 copy_tv(&li->li_tv, &li->li_tv);
23313 }
23314}
23315
23316/*
23317 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023318 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023319 */
23320 static int
23321can_free_funccal(fc, copyID)
23322 funccall_T *fc;
23323 int copyID;
23324{
23325 return (fc->l_varlist.lv_copyID != copyID
23326 && fc->l_vars.dv_copyID != copyID
23327 && fc->l_avars.dv_copyID != copyID);
23328}
23329
23330/*
23331 * Free "fc" and what it contains.
23332 */
23333 static void
23334free_funccal(fc, free_val)
23335 funccall_T *fc;
23336 int free_val; /* a: vars were allocated */
23337{
23338 listitem_T *li;
23339
23340 /* The a: variables typevals may not have been allocated, only free the
23341 * allocated variables. */
23342 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23343
23344 /* free all l: variables */
23345 vars_clear(&fc->l_vars.dv_hashtab);
23346
23347 /* Free the a:000 variables if they were allocated. */
23348 if (free_val)
23349 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23350 clear_tv(&li->li_tv);
23351
23352 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353}
23354
23355/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023356 * Add a number variable "name" to dict "dp" with value "nr".
23357 */
23358 static void
23359add_nr_var(dp, v, name, nr)
23360 dict_T *dp;
23361 dictitem_T *v;
23362 char *name;
23363 varnumber_T nr;
23364{
23365 STRCPY(v->di_key, name);
23366 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23367 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23368 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023369 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023370 v->di_tv.vval.v_number = nr;
23371}
23372
23373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 * ":return [expr]"
23375 */
23376 void
23377ex_return(eap)
23378 exarg_T *eap;
23379{
23380 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 int returning = FALSE;
23383
23384 if (current_funccal == NULL)
23385 {
23386 EMSG(_("E133: :return not inside a function"));
23387 return;
23388 }
23389
23390 if (eap->skip)
23391 ++emsg_skip;
23392
23393 eap->nextcmd = NULL;
23394 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023395 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 {
23397 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023398 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023400 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 }
23402 /* It's safer to return also on error. */
23403 else if (!eap->skip)
23404 {
23405 /*
23406 * Return unless the expression evaluation has been cancelled due to an
23407 * aborting error, an interrupt, or an exception.
23408 */
23409 if (!aborting())
23410 returning = do_return(eap, FALSE, TRUE, NULL);
23411 }
23412
23413 /* When skipping or the return gets pending, advance to the next command
23414 * in this line (!returning). Otherwise, ignore the rest of the line.
23415 * Following lines will be ignored by get_func_line(). */
23416 if (returning)
23417 eap->nextcmd = NULL;
23418 else if (eap->nextcmd == NULL) /* no argument */
23419 eap->nextcmd = check_nextcmd(arg);
23420
23421 if (eap->skip)
23422 --emsg_skip;
23423}
23424
23425/*
23426 * Return from a function. Possibly makes the return pending. Also called
23427 * for a pending return at the ":endtry" or after returning from an extra
23428 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023430 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 * FALSE when the return gets pending.
23432 */
23433 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023434do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 exarg_T *eap;
23436 int reanimate;
23437 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023438 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439{
23440 int idx;
23441 struct condstack *cstack = eap->cstack;
23442
23443 if (reanimate)
23444 /* Undo the return. */
23445 current_funccal->returned = FALSE;
23446
23447 /*
23448 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23449 * not in its finally clause (which then is to be executed next) is found.
23450 * In this case, make the ":return" pending for execution at the ":endtry".
23451 * Otherwise, return normally.
23452 */
23453 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23454 if (idx >= 0)
23455 {
23456 cstack->cs_pending[idx] = CSTP_RETURN;
23457
23458 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023459 /* A pending return again gets pending. "rettv" points to an
23460 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023462 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 else
23464 {
23465 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023466 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023468 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023470 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 {
23472 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023473 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023474 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 else
23476 EMSG(_(e_outofmem));
23477 }
23478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023479 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480
23481 if (reanimate)
23482 {
23483 /* The pending return value could be overwritten by a ":return"
23484 * without argument in a finally clause; reset the default
23485 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023486 current_funccal->rettv->v_type = VAR_NUMBER;
23487 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 }
23489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023490 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 }
23492 else
23493 {
23494 current_funccal->returned = TRUE;
23495
23496 /* If the return is carried out now, store the return value. For
23497 * a return immediately after reanimation, the value is already
23498 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023499 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023501 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023502 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023504 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 }
23506 }
23507
23508 return idx < 0;
23509}
23510
23511/*
23512 * Free the variable with a pending return value.
23513 */
23514 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023515discard_pending_return(rettv)
23516 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517{
Bram Moolenaar33570922005-01-25 22:26:29 +000023518 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519}
23520
23521/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023522 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 * is an allocated string. Used by report_pending() for verbose messages.
23524 */
23525 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023526get_return_cmd(rettv)
23527 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023529 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023530 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023531 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023533 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023534 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023535 if (s == NULL)
23536 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023537
23538 STRCPY(IObuff, ":return ");
23539 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23540 if (STRLEN(s) + 8 >= IOSIZE)
23541 STRCPY(IObuff + IOSIZE - 4, "...");
23542 vim_free(tofree);
23543 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544}
23545
23546/*
23547 * Get next function line.
23548 * Called by do_cmdline() to get the next line.
23549 * Returns allocated string, or NULL for end of function.
23550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 char_u *
23552get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023553 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023555 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556{
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023558 ufunc_T *fp = fcp->func;
23559 char_u *retval;
23560 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561
23562 /* If breakpoints have been added/deleted need to check for it. */
23563 if (fcp->dbg_tick != debug_tick)
23564 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023565 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 sourcing_lnum);
23567 fcp->dbg_tick = debug_tick;
23568 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023569#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023570 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023571 func_line_end(cookie);
23572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573
Bram Moolenaar05159a02005-02-26 23:04:13 +000023574 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023575 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23576 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 retval = NULL;
23578 else
23579 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023580 /* Skip NULL lines (continuation lines). */
23581 while (fcp->linenr < gap->ga_len
23582 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23583 ++fcp->linenr;
23584 if (fcp->linenr >= gap->ga_len)
23585 retval = NULL;
23586 else
23587 {
23588 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23589 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023590#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023591 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023592 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023593#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 }
23596
23597 /* Did we encounter a breakpoint? */
23598 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23599 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023600 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023602 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 sourcing_lnum);
23604 fcp->dbg_tick = debug_tick;
23605 }
23606
23607 return retval;
23608}
23609
Bram Moolenaar05159a02005-02-26 23:04:13 +000023610#if defined(FEAT_PROFILE) || defined(PROTO)
23611/*
23612 * Called when starting to read a function line.
23613 * "sourcing_lnum" must be correct!
23614 * When skipping lines it may not actually be executed, but we won't find out
23615 * until later and we need to store the time now.
23616 */
23617 void
23618func_line_start(cookie)
23619 void *cookie;
23620{
23621 funccall_T *fcp = (funccall_T *)cookie;
23622 ufunc_T *fp = fcp->func;
23623
23624 if (fp->uf_profiling && sourcing_lnum >= 1
23625 && sourcing_lnum <= fp->uf_lines.ga_len)
23626 {
23627 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023628 /* Skip continuation lines. */
23629 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23630 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023631 fp->uf_tml_execed = FALSE;
23632 profile_start(&fp->uf_tml_start);
23633 profile_zero(&fp->uf_tml_children);
23634 profile_get_wait(&fp->uf_tml_wait);
23635 }
23636}
23637
23638/*
23639 * Called when actually executing a function line.
23640 */
23641 void
23642func_line_exec(cookie)
23643 void *cookie;
23644{
23645 funccall_T *fcp = (funccall_T *)cookie;
23646 ufunc_T *fp = fcp->func;
23647
23648 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23649 fp->uf_tml_execed = TRUE;
23650}
23651
23652/*
23653 * Called when done with a function line.
23654 */
23655 void
23656func_line_end(cookie)
23657 void *cookie;
23658{
23659 funccall_T *fcp = (funccall_T *)cookie;
23660 ufunc_T *fp = fcp->func;
23661
23662 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23663 {
23664 if (fp->uf_tml_execed)
23665 {
23666 ++fp->uf_tml_count[fp->uf_tml_idx];
23667 profile_end(&fp->uf_tml_start);
23668 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023669 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023670 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23671 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023672 }
23673 fp->uf_tml_idx = -1;
23674 }
23675}
23676#endif
23677
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678/*
23679 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023680 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 */
23682 int
23683func_has_ended(cookie)
23684 void *cookie;
23685{
Bram Moolenaar33570922005-01-25 22:26:29 +000023686 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687
23688 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23689 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 || fcp->returned);
23692}
23693
23694/*
23695 * return TRUE if cookie indicates a function which "abort"s on errors.
23696 */
23697 int
23698func_has_abort(cookie)
23699 void *cookie;
23700{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023701 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702}
23703
23704#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23705typedef enum
23706{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023707 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23708 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23709 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710} var_flavour_T;
23711
23712static var_flavour_T var_flavour __ARGS((char_u *varname));
23713
23714 static var_flavour_T
23715var_flavour(varname)
23716 char_u *varname;
23717{
23718 char_u *p = varname;
23719
23720 if (ASCII_ISUPPER(*p))
23721 {
23722 while (*(++p))
23723 if (ASCII_ISLOWER(*p))
23724 return VAR_FLAVOUR_SESSION;
23725 return VAR_FLAVOUR_VIMINFO;
23726 }
23727 else
23728 return VAR_FLAVOUR_DEFAULT;
23729}
23730#endif
23731
23732#if defined(FEAT_VIMINFO) || defined(PROTO)
23733/*
23734 * Restore global vars that start with a capital from the viminfo file
23735 */
23736 int
23737read_viminfo_varlist(virp, writing)
23738 vir_T *virp;
23739 int writing;
23740{
23741 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023742 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023743 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744
23745 if (!writing && (find_viminfo_parameter('!') != NULL))
23746 {
23747 tab = vim_strchr(virp->vir_line + 1, '\t');
23748 if (tab != NULL)
23749 {
23750 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023751 switch (*tab)
23752 {
23753 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023754#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023755 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023756#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023757 case 'D': type = VAR_DICT; break;
23758 case 'L': type = VAR_LIST; break;
23759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760
23761 tab = vim_strchr(tab, '\t');
23762 if (tab != NULL)
23763 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023764 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023765 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023766 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023768#ifdef FEAT_FLOAT
23769 else if (type == VAR_FLOAT)
23770 (void)string2float(tab + 1, &tv.vval.v_float);
23771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023773 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023774 if (type == VAR_DICT || type == VAR_LIST)
23775 {
23776 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23777
23778 if (etv == NULL)
23779 /* Failed to parse back the dict or list, use it as a
23780 * string. */
23781 tv.v_type = VAR_STRING;
23782 else
23783 {
23784 vim_free(tv.vval.v_string);
23785 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023786 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023787 }
23788 }
23789
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023790 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023791
23792 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023793 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023794 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23795 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 }
23797 }
23798 }
23799
23800 return viminfo_readline(virp);
23801}
23802
23803/*
23804 * Write global vars that start with a capital to the viminfo file
23805 */
23806 void
23807write_viminfo_varlist(fp)
23808 FILE *fp;
23809{
Bram Moolenaar33570922005-01-25 22:26:29 +000023810 hashitem_T *hi;
23811 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023812 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023813 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023814 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023815 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023816 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817
23818 if (find_viminfo_parameter('!') == NULL)
23819 return;
23820
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023821 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023822
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023823 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023824 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023826 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023828 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023829 this_var = HI2DI(hi);
23830 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023833 {
23834 case VAR_STRING: s = "STR"; break;
23835 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023836#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023837 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023838#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023839 case VAR_DICT: s = "DIC"; break;
23840 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023841 default: continue;
23842 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023843 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023844 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023845 if (p != NULL)
23846 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023847 vim_free(tofree);
23848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 }
23850 }
23851}
23852#endif
23853
23854#if defined(FEAT_SESSION) || defined(PROTO)
23855 int
23856store_session_globals(fd)
23857 FILE *fd;
23858{
Bram Moolenaar33570922005-01-25 22:26:29 +000023859 hashitem_T *hi;
23860 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023861 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 char_u *p, *t;
23863
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023864 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023865 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023867 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023869 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023870 this_var = HI2DI(hi);
23871 if ((this_var->di_tv.v_type == VAR_NUMBER
23872 || this_var->di_tv.v_type == VAR_STRING)
23873 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023874 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023875 /* Escape special characters with a backslash. Turn a LF and
23876 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023877 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023878 (char_u *)"\\\"\n\r");
23879 if (p == NULL) /* out of memory */
23880 break;
23881 for (t = p; *t != NUL; ++t)
23882 if (*t == '\n')
23883 *t = 'n';
23884 else if (*t == '\r')
23885 *t = 'r';
23886 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023887 this_var->di_key,
23888 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23889 : ' ',
23890 p,
23891 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23892 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023893 || put_eol(fd) == FAIL)
23894 {
23895 vim_free(p);
23896 return FAIL;
23897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 vim_free(p);
23899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023900#ifdef FEAT_FLOAT
23901 else if (this_var->di_tv.v_type == VAR_FLOAT
23902 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23903 {
23904 float_T f = this_var->di_tv.vval.v_float;
23905 int sign = ' ';
23906
23907 if (f < 0)
23908 {
23909 f = -f;
23910 sign = '-';
23911 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023912 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023913 this_var->di_key, sign, f) < 0)
23914 || put_eol(fd) == FAIL)
23915 return FAIL;
23916 }
23917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 }
23919 }
23920 return OK;
23921}
23922#endif
23923
Bram Moolenaar661b1822005-07-28 22:36:45 +000023924/*
23925 * Display script name where an item was last set.
23926 * Should only be invoked when 'verbose' is non-zero.
23927 */
23928 void
23929last_set_msg(scriptID)
23930 scid_T scriptID;
23931{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023932 char_u *p;
23933
Bram Moolenaar661b1822005-07-28 22:36:45 +000023934 if (scriptID != 0)
23935 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023936 p = home_replace_save(NULL, get_scriptname(scriptID));
23937 if (p != NULL)
23938 {
23939 verbose_enter();
23940 MSG_PUTS(_("\n\tLast set from "));
23941 MSG_PUTS(p);
23942 vim_free(p);
23943 verbose_leave();
23944 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023945 }
23946}
23947
Bram Moolenaard812df62008-11-09 12:46:09 +000023948/*
23949 * List v:oldfiles in a nice way.
23950 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023951 void
23952ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023953 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023954{
23955 list_T *l = vimvars[VV_OLDFILES].vv_list;
23956 listitem_T *li;
23957 int nr = 0;
23958
23959 if (l == NULL)
23960 msg((char_u *)_("No old files"));
23961 else
23962 {
23963 msg_start();
23964 msg_scroll = TRUE;
23965 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23966 {
23967 msg_outnum((long)++nr);
23968 MSG_PUTS(": ");
23969 msg_outtrans(get_tv_string(&li->li_tv));
23970 msg_putchar('\n');
23971 out_flush(); /* output one line at a time */
23972 ui_breakcheck();
23973 }
23974 /* Assume "got_int" was set to truncate the listing. */
23975 got_int = FALSE;
23976
23977#ifdef FEAT_BROWSE_CMD
23978 if (cmdmod.browse)
23979 {
23980 quit_more = FALSE;
23981 nr = prompt_for_number(FALSE);
23982 msg_starthere();
23983 if (nr > 0)
23984 {
23985 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23986 (long)nr);
23987
23988 if (p != NULL)
23989 {
23990 p = expand_env_save(p);
23991 eap->arg = p;
23992 eap->cmdidx = CMD_edit;
23993 cmdmod.browse = FALSE;
23994 do_exedit(eap, NULL);
23995 vim_free(p);
23996 }
23997 }
23998 }
23999#endif
24000 }
24001}
24002
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003#endif /* FEAT_EVAL */
24004
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024006#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007
24008#ifdef WIN3264
24009/*
24010 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24011 */
24012static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24013static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24014static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24015
24016/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024017 * Get the short path (8.3) for the filename in "fnamep".
24018 * Only works for a valid file name.
24019 * When the path gets longer "fnamep" is changed and the allocated buffer
24020 * is put in "bufp".
24021 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24022 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 */
24024 static int
24025get_short_pathname(fnamep, bufp, fnamelen)
24026 char_u **fnamep;
24027 char_u **bufp;
24028 int *fnamelen;
24029{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024030 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 char_u *newbuf;
24032
24033 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 l = GetShortPathName(*fnamep, *fnamep, len);
24035 if (l > len - 1)
24036 {
24037 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024038 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 newbuf = vim_strnsave(*fnamep, l);
24040 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024041 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042
24043 vim_free(*bufp);
24044 *fnamep = *bufp = newbuf;
24045
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024046 /* Really should always succeed, as the buffer is big enough. */
24047 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 }
24049
24050 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024051 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052}
24053
24054/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024055 * Get the short path (8.3) for the filename in "fname". The converted
24056 * path is returned in "bufp".
24057 *
24058 * Some of the directories specified in "fname" may not exist. This function
24059 * will shorten the existing directories at the beginning of the path and then
24060 * append the remaining non-existing path.
24061 *
24062 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024063 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024064 * bufp - Pointer to an allocated buffer for the filename.
24065 * fnamelen - Length of the filename pointed to by fname
24066 *
24067 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 */
24069 static int
24070shortpath_for_invalid_fname(fname, bufp, fnamelen)
24071 char_u **fname;
24072 char_u **bufp;
24073 int *fnamelen;
24074{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024075 char_u *short_fname, *save_fname, *pbuf_unused;
24076 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024078 int old_len, len;
24079 int new_len, sfx_len;
24080 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
24082 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024083 old_len = *fnamelen;
24084 save_fname = vim_strnsave(*fname, old_len);
24085 pbuf_unused = NULL;
24086 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024088 endp = save_fname + old_len - 1; /* Find the end of the copy */
24089 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024091 /*
24092 * Try shortening the supplied path till it succeeds by removing one
24093 * directory at a time from the tail of the path.
24094 */
24095 len = 0;
24096 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024098 /* go back one path-separator */
24099 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24100 --endp;
24101 if (endp <= save_fname)
24102 break; /* processed the complete path */
24103
24104 /*
24105 * Replace the path separator with a NUL and try to shorten the
24106 * resulting path.
24107 */
24108 ch = *endp;
24109 *endp = 0;
24110 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024111 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024112 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24113 {
24114 retval = FAIL;
24115 goto theend;
24116 }
24117 *endp = ch; /* preserve the string */
24118
24119 if (len > 0)
24120 break; /* successfully shortened the path */
24121
24122 /* failed to shorten the path. Skip the path separator */
24123 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
24125
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024126 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024128 /*
24129 * Succeeded in shortening the path. Now concatenate the shortened
24130 * path with the remaining path at the tail.
24131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024133 /* Compute the length of the new path. */
24134 sfx_len = (int)(save_endp - endp) + 1;
24135 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024137 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024139 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024141 /* There is not enough space in the currently allocated string,
24142 * copy it to a buffer big enough. */
24143 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024145 {
24146 retval = FAIL;
24147 goto theend;
24148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 }
24150 else
24151 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024152 /* Transfer short_fname to the main buffer (it's big enough),
24153 * unless get_short_pathname() did its work in-place. */
24154 *fname = *bufp = save_fname;
24155 if (short_fname != save_fname)
24156 vim_strncpy(save_fname, short_fname, len);
24157 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024159
24160 /* concat the not-shortened part of the path */
24161 vim_strncpy(*fname + len, endp, sfx_len);
24162 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024164
24165theend:
24166 vim_free(pbuf_unused);
24167 vim_free(save_fname);
24168
24169 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170}
24171
24172/*
24173 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024174 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 */
24176 static int
24177shortpath_for_partial(fnamep, bufp, fnamelen)
24178 char_u **fnamep;
24179 char_u **bufp;
24180 int *fnamelen;
24181{
24182 int sepcount, len, tflen;
24183 char_u *p;
24184 char_u *pbuf, *tfname;
24185 int hasTilde;
24186
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024187 /* Count up the path separators from the RHS.. so we know which part
24188 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024190 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 if (vim_ispathsep(*p))
24192 ++sepcount;
24193
24194 /* Need full path first (use expand_env() to remove a "~/") */
24195 hasTilde = (**fnamep == '~');
24196 if (hasTilde)
24197 pbuf = tfname = expand_env_save(*fnamep);
24198 else
24199 pbuf = tfname = FullName_save(*fnamep, FALSE);
24200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024201 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024203 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24204 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205
24206 if (len == 0)
24207 {
24208 /* Don't have a valid filename, so shorten the rest of the
24209 * path if we can. This CAN give us invalid 8.3 filenames, but
24210 * there's not a lot of point in guessing what it might be.
24211 */
24212 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024213 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24214 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 }
24216
24217 /* Count the paths backward to find the beginning of the desired string. */
24218 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024219 {
24220#ifdef FEAT_MBYTE
24221 if (has_mbyte)
24222 p -= mb_head_off(tfname, p);
24223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 if (vim_ispathsep(*p))
24225 {
24226 if (sepcount == 0 || (hasTilde && sepcount == 1))
24227 break;
24228 else
24229 sepcount --;
24230 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 if (hasTilde)
24233 {
24234 --p;
24235 if (p >= tfname)
24236 *p = '~';
24237 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 }
24240 else
24241 ++p;
24242
24243 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24244 vim_free(*bufp);
24245 *fnamelen = (int)STRLEN(p);
24246 *bufp = pbuf;
24247 *fnamep = p;
24248
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024249 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250}
24251#endif /* WIN3264 */
24252
24253/*
24254 * Adjust a filename, according to a string of modifiers.
24255 * *fnamep must be NUL terminated when called. When returning, the length is
24256 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024257 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 * When there is an error, *fnamep is set to NULL.
24259 */
24260 int
24261modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24262 char_u *src; /* string with modifiers */
24263 int *usedlen; /* characters after src that are used */
24264 char_u **fnamep; /* file name so far */
24265 char_u **bufp; /* buffer for allocated file name or NULL */
24266 int *fnamelen; /* length of fnamep */
24267{
24268 int valid = 0;
24269 char_u *tail;
24270 char_u *s, *p, *pbuf;
24271 char_u dirname[MAXPATHL];
24272 int c;
24273 int has_fullname = 0;
24274#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024275 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276 int has_shortname = 0;
24277#endif
24278
24279repeat:
24280 /* ":p" - full path/file_name */
24281 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24282 {
24283 has_fullname = 1;
24284
24285 valid |= VALID_PATH;
24286 *usedlen += 2;
24287
24288 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24289 if ((*fnamep)[0] == '~'
24290#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24291 && ((*fnamep)[1] == '/'
24292# ifdef BACKSLASH_IN_FILENAME
24293 || (*fnamep)[1] == '\\'
24294# endif
24295 || (*fnamep)[1] == NUL)
24296
24297#endif
24298 )
24299 {
24300 *fnamep = expand_env_save(*fnamep);
24301 vim_free(*bufp); /* free any allocated file name */
24302 *bufp = *fnamep;
24303 if (*fnamep == NULL)
24304 return -1;
24305 }
24306
24307 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024308 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 {
24310 if (vim_ispathsep(*p)
24311 && p[1] == '.'
24312 && (p[2] == NUL
24313 || vim_ispathsep(p[2])
24314 || (p[2] == '.'
24315 && (p[3] == NUL || vim_ispathsep(p[3])))))
24316 break;
24317 }
24318
24319 /* FullName_save() is slow, don't use it when not needed. */
24320 if (*p != NUL || !vim_isAbsName(*fnamep))
24321 {
24322 *fnamep = FullName_save(*fnamep, *p != NUL);
24323 vim_free(*bufp); /* free any allocated file name */
24324 *bufp = *fnamep;
24325 if (*fnamep == NULL)
24326 return -1;
24327 }
24328
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024329#ifdef WIN3264
24330# if _WIN32_WINNT >= 0x0500
24331 if (vim_strchr(*fnamep, '~') != NULL)
24332 {
24333 /* Expand 8.3 filename to full path. Needed to make sure the same
24334 * file does not have two different names.
24335 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24336 p = alloc(_MAX_PATH + 1);
24337 if (p != NULL)
24338 {
24339 if (GetLongPathName(*fnamep, p, MAXPATHL))
24340 {
24341 vim_free(*bufp);
24342 *bufp = *fnamep = p;
24343 }
24344 else
24345 vim_free(p);
24346 }
24347 }
24348# endif
24349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 /* Append a path separator to a directory. */
24351 if (mch_isdir(*fnamep))
24352 {
24353 /* Make room for one or two extra characters. */
24354 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24355 vim_free(*bufp); /* free any allocated file name */
24356 *bufp = *fnamep;
24357 if (*fnamep == NULL)
24358 return -1;
24359 add_pathsep(*fnamep);
24360 }
24361 }
24362
24363 /* ":." - path relative to the current directory */
24364 /* ":~" - path relative to the home directory */
24365 /* ":8" - shortname path - postponed till after */
24366 while (src[*usedlen] == ':'
24367 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24368 {
24369 *usedlen += 2;
24370 if (c == '8')
24371 {
24372#ifdef WIN3264
24373 has_shortname = 1; /* Postpone this. */
24374#endif
24375 continue;
24376 }
24377 pbuf = NULL;
24378 /* Need full path first (use expand_env() to remove a "~/") */
24379 if (!has_fullname)
24380 {
24381 if (c == '.' && **fnamep == '~')
24382 p = pbuf = expand_env_save(*fnamep);
24383 else
24384 p = pbuf = FullName_save(*fnamep, FALSE);
24385 }
24386 else
24387 p = *fnamep;
24388
24389 has_fullname = 0;
24390
24391 if (p != NULL)
24392 {
24393 if (c == '.')
24394 {
24395 mch_dirname(dirname, MAXPATHL);
24396 s = shorten_fname(p, dirname);
24397 if (s != NULL)
24398 {
24399 *fnamep = s;
24400 if (pbuf != NULL)
24401 {
24402 vim_free(*bufp); /* free any allocated file name */
24403 *bufp = pbuf;
24404 pbuf = NULL;
24405 }
24406 }
24407 }
24408 else
24409 {
24410 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24411 /* Only replace it when it starts with '~' */
24412 if (*dirname == '~')
24413 {
24414 s = vim_strsave(dirname);
24415 if (s != NULL)
24416 {
24417 *fnamep = s;
24418 vim_free(*bufp);
24419 *bufp = s;
24420 }
24421 }
24422 }
24423 vim_free(pbuf);
24424 }
24425 }
24426
24427 tail = gettail(*fnamep);
24428 *fnamelen = (int)STRLEN(*fnamep);
24429
24430 /* ":h" - head, remove "/file_name", can be repeated */
24431 /* Don't remove the first "/" or "c:\" */
24432 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24433 {
24434 valid |= VALID_HEAD;
24435 *usedlen += 2;
24436 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024437 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024438 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 *fnamelen = (int)(tail - *fnamep);
24440#ifdef VMS
24441 if (*fnamelen > 0)
24442 *fnamelen += 1; /* the path separator is part of the path */
24443#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024444 if (*fnamelen == 0)
24445 {
24446 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24447 p = vim_strsave((char_u *)".");
24448 if (p == NULL)
24449 return -1;
24450 vim_free(*bufp);
24451 *bufp = *fnamep = tail = p;
24452 *fnamelen = 1;
24453 }
24454 else
24455 {
24456 while (tail > s && !after_pathsep(s, tail))
24457 mb_ptr_back(*fnamep, tail);
24458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 }
24460
24461 /* ":8" - shortname */
24462 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24463 {
24464 *usedlen += 2;
24465#ifdef WIN3264
24466 has_shortname = 1;
24467#endif
24468 }
24469
24470#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024471 /*
24472 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 */
24474 if (has_shortname)
24475 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024476 /* Copy the string if it is shortened by :h and when it wasn't copied
24477 * yet, because we are going to change it in place. Avoids changing
24478 * the buffer name for "%:8". */
24479 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 {
24481 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024482 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 return -1;
24484 vim_free(*bufp);
24485 *bufp = *fnamep = p;
24486 }
24487
24488 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024489 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 if (!has_fullname && !vim_isAbsName(*fnamep))
24491 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024492 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493 return -1;
24494 }
24495 else
24496 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024497 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498
Bram Moolenaardc935552011-08-17 15:23:23 +020024499 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024501 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 return -1;
24503
24504 if (l == 0)
24505 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024506 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024508 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 return -1;
24510 }
24511 *fnamelen = l;
24512 }
24513 }
24514#endif /* WIN3264 */
24515
24516 /* ":t" - tail, just the basename */
24517 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24518 {
24519 *usedlen += 2;
24520 *fnamelen -= (int)(tail - *fnamep);
24521 *fnamep = tail;
24522 }
24523
24524 /* ":e" - extension, can be repeated */
24525 /* ":r" - root, without extension, can be repeated */
24526 while (src[*usedlen] == ':'
24527 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24528 {
24529 /* find a '.' in the tail:
24530 * - for second :e: before the current fname
24531 * - otherwise: The last '.'
24532 */
24533 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24534 s = *fnamep - 2;
24535 else
24536 s = *fnamep + *fnamelen - 1;
24537 for ( ; s > tail; --s)
24538 if (s[0] == '.')
24539 break;
24540 if (src[*usedlen + 1] == 'e') /* :e */
24541 {
24542 if (s > tail)
24543 {
24544 *fnamelen += (int)(*fnamep - (s + 1));
24545 *fnamep = s + 1;
24546#ifdef VMS
24547 /* cut version from the extension */
24548 s = *fnamep + *fnamelen - 1;
24549 for ( ; s > *fnamep; --s)
24550 if (s[0] == ';')
24551 break;
24552 if (s > *fnamep)
24553 *fnamelen = s - *fnamep;
24554#endif
24555 }
24556 else if (*fnamep <= tail)
24557 *fnamelen = 0;
24558 }
24559 else /* :r */
24560 {
24561 if (s > tail) /* remove one extension */
24562 *fnamelen = (int)(s - *fnamep);
24563 }
24564 *usedlen += 2;
24565 }
24566
24567 /* ":s?pat?foo?" - substitute */
24568 /* ":gs?pat?foo?" - global substitute */
24569 if (src[*usedlen] == ':'
24570 && (src[*usedlen + 1] == 's'
24571 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24572 {
24573 char_u *str;
24574 char_u *pat;
24575 char_u *sub;
24576 int sep;
24577 char_u *flags;
24578 int didit = FALSE;
24579
24580 flags = (char_u *)"";
24581 s = src + *usedlen + 2;
24582 if (src[*usedlen + 1] == 'g')
24583 {
24584 flags = (char_u *)"g";
24585 ++s;
24586 }
24587
24588 sep = *s++;
24589 if (sep)
24590 {
24591 /* find end of pattern */
24592 p = vim_strchr(s, sep);
24593 if (p != NULL)
24594 {
24595 pat = vim_strnsave(s, (int)(p - s));
24596 if (pat != NULL)
24597 {
24598 s = p + 1;
24599 /* find end of substitution */
24600 p = vim_strchr(s, sep);
24601 if (p != NULL)
24602 {
24603 sub = vim_strnsave(s, (int)(p - s));
24604 str = vim_strnsave(*fnamep, *fnamelen);
24605 if (sub != NULL && str != NULL)
24606 {
24607 *usedlen = (int)(p + 1 - src);
24608 s = do_string_sub(str, pat, sub, flags);
24609 if (s != NULL)
24610 {
24611 *fnamep = s;
24612 *fnamelen = (int)STRLEN(s);
24613 vim_free(*bufp);
24614 *bufp = s;
24615 didit = TRUE;
24616 }
24617 }
24618 vim_free(sub);
24619 vim_free(str);
24620 }
24621 vim_free(pat);
24622 }
24623 }
24624 /* after using ":s", repeat all the modifiers */
24625 if (didit)
24626 goto repeat;
24627 }
24628 }
24629
Bram Moolenaar26df0922014-02-23 23:39:13 +010024630 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24631 {
24632 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24633 if (p == NULL)
24634 return -1;
24635 vim_free(*bufp);
24636 *bufp = *fnamep = p;
24637 *fnamelen = (int)STRLEN(p);
24638 *usedlen += 2;
24639 }
24640
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 return valid;
24642}
24643
24644/*
24645 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24646 * "flags" can be "g" to do a global substitute.
24647 * Returns an allocated string, NULL for error.
24648 */
24649 char_u *
24650do_string_sub(str, pat, sub, flags)
24651 char_u *str;
24652 char_u *pat;
24653 char_u *sub;
24654 char_u *flags;
24655{
24656 int sublen;
24657 regmatch_T regmatch;
24658 int i;
24659 int do_all;
24660 char_u *tail;
24661 garray_T ga;
24662 char_u *ret;
24663 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024664 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665
24666 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24667 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024668 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669
24670 ga_init2(&ga, 1, 200);
24671
24672 do_all = (flags[0] == 'g');
24673
24674 regmatch.rm_ic = p_ic;
24675 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24676 if (regmatch.regprog != NULL)
24677 {
24678 tail = str;
24679 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24680 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024681 /* Skip empty match except for first match. */
24682 if (regmatch.startp[0] == regmatch.endp[0])
24683 {
24684 if (zero_width == regmatch.startp[0])
24685 {
24686 /* avoid getting stuck on a match with an empty string */
24687 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24688 ++ga.ga_len;
24689 continue;
24690 }
24691 zero_width = regmatch.startp[0];
24692 }
24693
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 /*
24695 * Get some space for a temporary buffer to do the substitution
24696 * into. It will contain:
24697 * - The text up to where the match is.
24698 * - The substituted text.
24699 * - The text after the match.
24700 */
24701 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24702 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24703 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24704 {
24705 ga_clear(&ga);
24706 break;
24707 }
24708
24709 /* copy the text up to where the match is */
24710 i = (int)(regmatch.startp[0] - tail);
24711 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24712 /* add the substituted text */
24713 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24714 + ga.ga_len + i, TRUE, TRUE, FALSE);
24715 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024716 tail = regmatch.endp[0];
24717 if (*tail == NUL)
24718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 if (!do_all)
24720 break;
24721 }
24722
24723 if (ga.ga_data != NULL)
24724 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24725
Bram Moolenaar473de612013-06-08 18:19:48 +020024726 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 }
24728
24729 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24730 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024731 if (p_cpo == empty_option)
24732 p_cpo = save_cpo;
24733 else
24734 /* Darn, evaluating {sub} expression changed the value. */
24735 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736
24737 return ret;
24738}
24739
24740#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */